Skip to content

Commit d22bf90

Browse files
authored
Merge pull request #41 from PostHog/fix-uri-format
Correct URI generation for next steps; introduce resources explicitly
2 parents f71db68 + e4a5306 commit d22bf90

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

llm-prompts/basic-integration/1.1-edit.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ If a file already has existing integration code for other tools or services, don
1111

1212
For each event, add useful properties, and use your access to the PostHog source code to ensure correctness. You also have access to documentation about creating new events with PostHog. Consider this documentation carefully and follow it closely before adding events. Your integration should be based on documented best practices. Carefully consider how the user project's framework version may impact the correct PostHog integration approach.
1313

14-
Remember that you can find the source code for any dependency in the node_modules directory. This may be necessary to properly populate property names.
14+
Remember that you can find the source code for any dependency in the node_modules directory. This may be necessary to properly populate property names. There are also example project code files available via the PostHog MCP; use these for reference.
1515

1616
Where possible, add calls for PostHog's identify() function on the client side. On the server side, make sure events have a matching distinct ID where relevant. Use the same ID for identify on the client as you use distinct ID on the server. Logins and signups are a great opportunity to identify users. Use the contents of login and signup forms to identify users on submit.
1717

scripts/build-examples-mcp-resources.js

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ const MANIFEST_VERSION = '1.0';
3535
const BUILD_VERSION = process.env.BUILD_VERSION || 'dev';
3636
const URI_SCHEME = 'posthog://';
3737

38+
/**
39+
* URI pattern constants - SINGLE SOURCE OF TRUTH for all URI formats
40+
*/
41+
const URI_PATTERNS = {
42+
workflow: (category, name) => `${URI_SCHEME}workflows/${category}/${name}`,
43+
doc: (id) => `${URI_SCHEME}docs/${id}`,
44+
frameworkDocs: `${URI_SCHEME}docs/frameworks/{framework}`,
45+
examples: `${URI_SCHEME}examples/{framework}`,
46+
};
47+
48+
/**
49+
* Generate a workflow URI from a workflow object
50+
* @param {Object} workflow - Workflow object with category and name properties
51+
* @returns {string} The full URI for the workflow
52+
*/
53+
function getWorkflowUri(workflow) {
54+
return URI_PATTERNS.workflow(workflow.category, workflow.name);
55+
}
56+
3857
/**
3958
* Documentation URLs configuration
4059
* These docs are fetched at runtime by the MCP server
@@ -505,18 +524,23 @@ function discoverPrompts(promptsPath) {
505524
* This generates ALL URIs - the MCP server purely reflects what's here
506525
*/
507526
function generateManifest(discoveredWorkflows, exampleIds, discoveredPrompts) {
527+
// Helper to get next step URI by looking up the workflow
528+
const getNextStepUri = (nextStepId) => {
529+
if (!nextStepId) return undefined;
530+
const nextWorkflow = discoveredWorkflows.find(w => w.id === nextStepId);
531+
return nextWorkflow ? getWorkflowUri(nextWorkflow) : undefined;
532+
};
533+
508534
// Generate workflow resources with hierarchical URIs
509535
const workflows = discoveredWorkflows.map(workflow => ({
510536
id: workflow.id,
511537
name: workflow.title,
512538
description: workflow.description,
513539
file: workflow.file,
514540
order: workflow.order,
515-
uri: `${URI_SCHEME}workflows/${workflow.category}/${workflow.name}`,
541+
uri: getWorkflowUri(workflow),
516542
nextStepId: workflow.nextStepId,
517-
nextStepUri: workflow.nextStepId
518-
? `${URI_SCHEME}workflows/${workflow.category}/${discoveredWorkflows.find(w => w.id === workflow.nextStepId)?.name}`
519-
: undefined,
543+
nextStepUri: getNextStepUri(workflow.nextStepId),
520544
}));
521545

522546
// TEMPORARY: Backward compatibility alias for legacy URI
@@ -531,9 +555,7 @@ function generateManifest(discoveredWorkflows, exampleIds, discoveredPrompts) {
531555
order: beginWorkflow.order,
532556
uri: 'posthog://integration/workflow/setup/begin', // Old hardcoded URI
533557
nextStepId: beginWorkflow.nextStepId,
534-
nextStepUri: beginWorkflow.nextStepId
535-
? `${URI_SCHEME}workflows/${beginWorkflow.category}/${discoveredWorkflows.find(w => w.id === beginWorkflow.nextStepId)?.name}`
536-
: undefined,
558+
nextStepUri: getNextStepUri(beginWorkflow.nextStepId),
537559
});
538560
}
539561

@@ -544,7 +566,7 @@ function generateManifest(discoveredWorkflows, exampleIds, discoveredPrompts) {
544566
id: DOCS_CONFIG.identify.id,
545567
name: DOCS_CONFIG.identify.name,
546568
description: DOCS_CONFIG.identify.description,
547-
uri: `${URI_SCHEME}docs/${DOCS_CONFIG.identify.id}`,
569+
uri: URI_PATTERNS.doc(DOCS_CONFIG.identify.id),
548570
url: DOCS_CONFIG.identify.url,
549571
},
550572
];
@@ -573,8 +595,8 @@ function generateManifest(discoveredWorkflows, exampleIds, discoveredPrompts) {
573595
}
574596

575597
// Add static template mappings for docs and examples
576-
uriMap['docs.frameworks'] = 'posthog://docs/frameworks/{framework}';
577-
uriMap['examples'] = 'posthog://examples/{framework}';
598+
uriMap['docs.frameworks'] = URI_PATTERNS.frameworkDocs;
599+
uriMap['examples'] = URI_PATTERNS.examples;
578600

579601
/**
580602
* Helper to replace template variables in prompt text
@@ -599,7 +621,7 @@ function generateManifest(discoveredWorkflows, exampleIds, discoveredPrompts) {
599621
const templates = [
600622
{
601623
name: 'PostHog example projects',
602-
uriPattern: 'posthog://examples/{framework}',
624+
uriPattern: URI_PATTERNS.examples,
603625
description: 'Example project code showing PostHog integration for various frameworks',
604626
parameterName: 'framework',
605627
items: exampleIds.map(id => ({
@@ -609,7 +631,7 @@ function generateManifest(discoveredWorkflows, exampleIds, discoveredPrompts) {
609631
},
610632
{
611633
name: 'PostHog framework integration documentation',
612-
uriPattern: 'posthog://docs/frameworks/{framework}',
634+
uriPattern: URI_PATTERNS.frameworkDocs,
613635
description: 'PostHog integration documentation for various frameworks',
614636
parameterName: 'framework',
615637
items: Object.values(DOCS_CONFIG.frameworks).map(framework => ({
@@ -669,8 +691,15 @@ function processWorkflowFiles(discoveredWorkflows, outputDir) {
669691
let content = fs.readFileSync(workflow.fullPath, 'utf8');
670692

671693
if (workflow.nextStepId) {
672-
// Generate next step URI
673-
const nextStepUri = `${URI_SCHEME}workflows/${workflow.nextStepId}`;
694+
// Find the next workflow to generate its URI
695+
const nextWorkflow = discoveredWorkflows.find(w => w.id === workflow.nextStepId);
696+
if (!nextWorkflow) {
697+
console.warn(`[WARNING] Next step workflow not found: ${workflow.nextStepId}`);
698+
continue;
699+
}
700+
701+
// Generate next step URI using the single source of truth
702+
const nextStepUri = getWorkflowUri(nextWorkflow);
674703

675704
// Append next step message
676705
content += `\n\n---\n\n**Upon completion, access the following resource to continue:** ${nextStepUri}`;

0 commit comments

Comments
 (0)