:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(visual-review): allow artifacts in mcp apps (#104960)
Paul D'Ambraa228b70c5fc6
diff --git a/docs/published/handbook/engineering/ai/implementing-mcp-tools.md b/docs/published/handbook/engineering/ai/implementing-mcp-tools.mdindex a528f3ebfe8a..43647003c1b9 100644--- a/docs/published/handbook/engineering/ai/implementing-mcp-tools.md+++ b/docs/published/handbook/engineering/ai/implementing-mcp-tools.md@@ -349,6 +349,8 @@ Product teams own their definitions and control which operations are exposed asname fails generation instead of silently dropping the argument at runtime.See "UI apps" in `services/mcp/CONTRIBUTING.md` for the rules.+ A custom UI app can set `resource_domains` when it loads an image, font, script, or stylesheet from an external source. Each value must be a CSP source expression. Declare only the required origin or path.+#### Custom input schemasBy default, tool input schemas are auto-derived from OpenAPI via Orval.diff --git a/products/visual_review/mcp/tools.yaml b/products/visual_review/mcp/tools.yamlindex a6ae81a46a73..a26d1cec507e 100644--- a/products/visual_review/mcp/tools.yaml+++ b/products/visual_review/mcp/tools.yaml@@ -12,6 +12,9 @@ ui_apps:type: customapp_name: PostHog Visual Review Snapshotsdescription: Visual review run snapshots — diff viewer with approve/tolerate actions+ resource_domains:+ - https://s3.us-east-1.amazonaws.com/posthog-cloud-prod-us-east-1-app-assets/visual_review/+ - http://localhost:19000/posthog/visual_review/tools:visual-review-repos-baselines-retrieve:operation: visual_review_repos_baselines_retrievediff --git a/services/mcp/scripts/generate-ui-apps.ts b/services/mcp/scripts/generate-ui-apps.tsindex 6656e65c5817..fddb5ee5dfd2 100644--- a/services/mcp/scripts/generate-ui-apps.ts+++ b/services/mcp/scripts/generate-ui-apps.ts@@ -122,6 +122,7 @@ function resolveCustomApp(raw: Extract<UiAppConfig, { type: 'custom' }>): Resolvtype: 'custom',app_name: raw.app_name,description: raw.description,+ ...(raw.resource_domains ? { resource_domains: raw.resource_domains } : {}),...(raw.render_ui ? { render_ui: raw.render_ui } : {}),}}@@ -341,6 +342,7 @@ interface RegistryEntry {uri: stringname: stringdescription: string+ resourceDomains?: string[]}function toConstName(appKey: string): string {@@ -361,7 +363,7 @@ function generateRegistry(entries: RegistryEntry[], dispatchableKeys: string[]):const appEntries = entries.map((e) =>- ` {\n name: '${e.name}',\n uri: ${e.constName},\n description: '${e.description}',\n appDir: '${e.appDir}',\n }`+ ` {\n name: '${e.name}',\n uri: ${e.constName},\n description: '${e.description}',\n appDir: '${e.appDir}',${e.resourceDomains ? `\n resourceDomains: ${JSON.stringify(e.resourceDomains)},` : ''}\n }`).join(',\n')@@ -392,6 +394,7 @@ export const UI_APPS: Array<{uri: stringdescription: stringappDir: string+ resourceDomains?: string[]}> = [${appEntries},]@@ -519,6 +522,7 @@ function main(): void {uri,name: appConfig.app_name,description: appConfig.description,+ resourceDomains: resolved.resource_domains,})}}diff --git a/services/mcp/scripts/yaml-config-schema.ts b/services/mcp/scripts/yaml-config-schema.tsindex 96567e3552a9..7b2c909074f7 100644--- a/services/mcp/scripts/yaml-config-schema.ts+++ b/services/mcp/scripts/yaml-config-schema.ts@@ -423,6 +423,8 @@ const CustomUiAppSchema = zapp_name: z.string(),/** Short description for the MCP resource. Required for custom apps. */description: z.string(),+ /** Additional CSP resource sources required by this app. */+ resource_domains: z.array(z.string()).optional(),/** Reusable view component that lets the render-ui umbrella app mount this custom app. */render_ui: z.object({@@ -477,6 +479,7 @@ export interface ResolvedCustomUiApp {type: 'custom'app_name: stringdescription: string+ resource_domains?: string[]render_ui?: {component_import: stringview_component: stringdiff --git a/services/mcp/src/hono/resource-catalog.ts b/services/mcp/src/hono/resource-catalog.tsindex aa297f3d6f02..75191952ddbd 100644--- a/services/mcp/src/hono/resource-catalog.ts+++ b/services/mcp/src/hono/resource-catalog.ts@@ -178,13 +178,16 @@ export class ResourceCatalog {for (const app of UI_APPS) {const html = buildAppStubHtml(app.appDir, baseUrl)- const meta = buildUiAppResourceMeta(baseUrl, analyticsBaseUrl)+ const meta = buildUiAppResourceMeta(baseUrl, analyticsBaseUrl, app.resourceDomains)+ // `_meta` goes on the list entry too, because a host that builds the iframe CSP+ // from `resources/list` alone never sees the metadata on the read entry.this.uiAppResources.push({name: app.name,uri: app.uri,mimeType: RESOURCE_MIME_TYPE,description: app.description,+ _meta: meta,})this.uiAppReadEntries.set(app.uri, {uri: app.uri,diff --git a/services/mcp/src/resources/ui-apps.generated.ts b/services/mcp/src/resources/ui-apps.generated.tsindex 7ca4e282b40a..d9b3282ea0c6 100644--- a/services/mcp/src/resources/ui-apps.generated.ts+++ b/services/mcp/src/resources/ui-apps.generated.ts@@ -146,6 +146,7 @@ export const UI_APPS: Array<{uri: stringdescription: stringappDir: string+ resourceDomains?: string[]}> = [{name: 'PostHog Action',@@ -332,6 +333,10 @@ export const UI_APPS: Array<{uri: VISUAL_REVIEW_SNAPSHOTS_RESOURCE_URI,description: 'Visual review run snapshots — diff viewer with approve/tolerate actions',appDir: 'visual-review-snapshots',+ resourceDomains: [+ 'https://s3.us-east-1.amazonaws.com/posthog-cloud-prod-us-east-1-app-assets/visual_review/',+ 'http://localhost:19000/posthog/visual_review/',+ ],},{name: 'PostHog Workflow',diff --git a/services/mcp/src/resources/ui-apps.ts b/services/mcp/src/resources/ui-apps.tsindex 32b23abecc42..48eff304208a 100644--- a/services/mcp/src/resources/ui-apps.ts+++ b/services/mcp/src/resources/ui-apps.ts@@ -48,8 +48,12 @@ export interface UiAppResourceMeta {* are blocked whenever that host differs from the connected server origin* (e.g. mcp.us.posthog.com assets vs mcp.posthog.com origin).*/-export function buildUiAppResourceMeta(baseUrl: string, analyticsBaseUrl: string | undefined): UiAppResourceMeta {- const resourceDomains: string[] = [baseUrl]+export function buildUiAppResourceMeta(+ baseUrl: string,+ analyticsBaseUrl: string | undefined,+ additionalResourceDomains: string[] = []+): UiAppResourceMeta {+ const resourceDomains: string[] = [baseUrl, ...additionalResourceDomains]const connectDomains: string[] = []if (analyticsBaseUrl) {@@ -95,15 +99,16 @@ interface RegisterAppParams {uri: stringdescription: stringappDir: string+ resourceDomains?: string[]}function registerApp(server: McpServer,context: Context,- { name, uri, description, appDir }: RegisterAppParams,+ { name, uri, description, appDir, resourceDomains }: RegisterAppParams,baseUrl: string): void {- const meta = buildUiAppResourceMeta(baseUrl, context.env.POSTHOG_MCP_APPS_ANALYTICS_BASE_URL)+ const meta = buildUiAppResourceMeta(baseUrl, context.env.POSTHOG_MCP_APPS_ANALYTICS_BASE_URL, resourceDomains)const html = buildAppStubHtml(appDir, baseUrl)// `_meta` goes on the registration too, so a host that builds the iframe CSP fromdiff --git a/services/mcp/tests/hono/resource-catalog.test.ts b/services/mcp/tests/hono/resource-catalog.test.tsindex 4b6beac41b67..3245be604722 100644--- a/services/mcp/tests/hono/resource-catalog.test.ts+++ b/services/mcp/tests/hono/resource-catalog.test.ts@@ -155,6 +155,23 @@ describe('ResourceCatalog', () => {expect(list1).toBe(list2)})+ it('lists the Visual Review artifact source in its CSP metadata', async () => {+ vi.mocked(fetchAndExtractEntries).mockResolvedValue([])+ vi.mocked(getPromptsFromManifest).mockResolvedValue([])++ const catalog = new ResourceCatalog(mockEnv, redis)+ await catalog.warmup()++ const listed = catalog+ .getResourcesList()+ .resources.find((r) => r.uri === 'ui://posthog/visual-review-snapshots.html')+ const uiMeta = listed?._meta?.ui as { csp?: { resourceDomains?: string[] } } | undefined++ expect(uiMeta?.csp?.resourceDomains).toContain(+ 'https://s3.us-east-1.amazonaws.com/posthog-cloud-prod-us-east-1-app-assets/visual_review/'+ )+ })+it('revalidates context-mill resources on demand', async () => {vi.mocked(fetchAndExtractEntries).mockResolvedValueOnce([makeEntry('old')])vi.mocked(getPromptsFromManifest).mockResolvedValue([])@@ -209,6 +226,21 @@ describe('ResourceCatalog', () => {})describe('readResource', () => {+ it('includes the Visual Review artifact source in its CSP metadata', async () => {+ vi.mocked(fetchAndExtractEntries).mockResolvedValue([])+ vi.mocked(getPromptsFromManifest).mockResolvedValue([])++ const catalog = new ResourceCatalog(mockEnv, redis)+ await catalog.warmup()++ const result = await catalog.readResource({ uri: 'ui://posthog/visual-review-snapshots.html' })+ const uiMeta = result.contents[0]?._meta?.ui as { csp?: { resourceDomains?: string[] } } | undefined++ expect(uiMeta?.csp?.resourceDomains).toContain(+ 'https://s3.us-east-1.amazonaws.com/posthog-cloud-prod-us-east-1-app-assets/visual_review/'+ )+ })+it('lazy-loads a context-mill body from Redis on first read', async () => {vi.mocked(fetchAndExtractEntries).mockResolvedValue([makeEntry('doc')])vi.mocked(getPromptsFromManifest).mockResolvedValue([])diff --git a/services/mcp/tests/unit/ui-apps.test.ts b/services/mcp/tests/unit/ui-apps.test.tsindex 849a528f65a5..ccd8466adb53 100644--- a/services/mcp/tests/unit/ui-apps.test.ts+++ b/services/mcp/tests/unit/ui-apps.test.ts@@ -156,6 +156,27 @@ describe('ui-apps', () => {expect(registration._meta['openai/widgetCSP'].resource_domains).toContain('https://mcp.posthog.com')})+ it('allows Visual Review artifacts without widening other app CSPs', async () => {+ const { registerUiAppResources } = await import('@/resources/ui-apps')+ const server = createMockServer()+ const context = createMockContext({ MCP_APPS_BASE_URL: 'https://mcp.posthog.com' })++ await registerUiAppResources(server as any, context as any)++ const visualReviewRegistration = server.registerResource.mock.calls.find(+ (call: unknown[]) => call[1] === URI_MAP['visual-review-snapshots']+ )+ const debugRegistration = server.registerResource.mock.calls.find(+ (call: unknown[]) => call[1] === URI_MAP.debug+ )+ const artifactSource =+ 'https://s3.us-east-1.amazonaws.com/posthog-cloud-prod-us-east-1-app-assets/visual_review/'++ expect(visualReviewRegistration![2]._meta.ui.csp.resourceDomains).toContain(artifactSource)+ expect(visualReviewRegistration![2]._meta['openai/widgetCSP'].resource_domains).toContain(artifactSource)+ expect(debugRegistration![2]._meta.ui.csp.resourceDomains).not.toContain(artifactSource)+ })+it('includes analytics URL in CSP when set', async () => {const { registerUiAppResources } = await import('@/resources/ui-apps')const server = createMockServer()