Skip to content

: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.

fix(mcp): declare openai/widgetCSP so ChatGPT allows UI app assets (#69612)

Georgiy Tarasov693711aacbd8
diff --git a/services/mcp/src/hono/resource-catalog.ts b/services/mcp/src/hono/resource-catalog.ts
index acd5bb6638f2..9bb458e44eb8 100644
--- a/services/mcp/src/hono/resource-catalog.ts
+++ b/services/mcp/src/hono/resource-catalog.ts
@@ -10,7 +10,7 @@ import type {
} from '@modelcontextprotocol/sdk/types.js'
import { getPromptsFromManifest } from '@/resources'
-import { buildAppStubHtml } from '@/resources/ui-apps'
+import { buildAppStubHtml, buildUiAppResourceMeta } from '@/resources/ui-apps'
import { UI_APPS } from '@/resources/ui-apps.generated'
import type { Env } from '@/tools/types'
@@ -178,15 +178,7 @@ export class ResourceCatalog {
for (const app of UI_APPS) {
const html = buildAppStubHtml(app.appDir, baseUrl)
-
- const uiMetadata: Record<string, unknown> = {}
- const resourceDomains = [baseUrl]
- const connectDomains: string[] = []
- if (analyticsBaseUrl) {
- connectDomains.push(analyticsBaseUrl)
- resourceDomains.push(analyticsBaseUrl)
- }
- uiMetadata.csp = { connectDomains, resourceDomains }
+ const meta = buildUiAppResourceMeta(baseUrl, analyticsBaseUrl)
this.uiAppResources.push({
name: app.name,
@@ -198,7 +190,7 @@ export class ResourceCatalog {
uri: app.uri,
mimeType: RESOURCE_MIME_TYPE,
text: html,
- _meta: { ui: uiMetadata },
+ _meta: meta,
})
}
}
diff --git a/services/mcp/src/resources/ui-apps.ts b/services/mcp/src/resources/ui-apps.ts
index 3f39e9165587..f48e0eb2e294 100644
--- a/services/mcp/src/resources/ui-apps.ts
+++ b/services/mcp/src/resources/ui-apps.ts
@@ -29,6 +29,43 @@ export function buildAppStubHtml(appDir: string, baseUrl: string): string {
</body></html>`
}
+export interface UiAppResourceMeta {
+ [key: string]: unknown
+ ui: McpUiResourceMeta
+ 'openai/widgetCSP': {
+ connect_domains: string[]
+ resource_domains: string[]
+ }
+}
+
+/**
+ * Build the `_meta` object for a UI app resource, declaring which domains the
+ * host must allow in the app iframe's CSP.
+ *
+ * Declared in two formats: `ui.csp` per the MCP Apps spec (Claude), and
+ * `openai/widgetCSP` for ChatGPT — ChatGPT ignores `ui.csp` and only extends
+ * its iframe CSP with its own key, so without it assets on MCP_APPS_BASE_URL
+ * 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]
+ const connectDomains: string[] = []
+
+ if (analyticsBaseUrl) {
+ connectDomains.push(analyticsBaseUrl)
+ resourceDomains.push(analyticsBaseUrl)
+ }
+
+ return {
+ ui: { csp: { connectDomains, resourceDomains } },
+ 'openai/widgetCSP': {
+ connect_domains: connectDomains,
+ resource_domains: resourceDomains,
+ },
+ }
+}
+
/**
* Registers UI app resources with the MCP server.
* These resources provide interactive visualizations for tool results
@@ -66,22 +103,7 @@ function registerApp(
{ name, uri, description, appDir }: RegisterAppParams,
baseUrl: string
): void {
- const analyticsBaseUrl = context.env.POSTHOG_MCP_APPS_ANALYTICS_BASE_URL
- const uiMetadata: McpUiResourceMeta = {}
-
- const resourceDomains: string[] = [baseUrl]
- const connectDomains: string[] = []
-
- if (analyticsBaseUrl) {
- connectDomains.push(analyticsBaseUrl)
- resourceDomains.push(analyticsBaseUrl)
- }
-
- uiMetadata.csp = {
- connectDomains,
- resourceDomains,
- }
-
+ const meta = buildUiAppResourceMeta(baseUrl, context.env.POSTHOG_MCP_APPS_ANALYTICS_BASE_URL)
const html = buildAppStubHtml(appDir, baseUrl)
server.registerResource(name, uri, { mimeType: RESOURCE_MIME_TYPE, description }, async (uri) => {
@@ -91,7 +113,7 @@ function registerApp(
uri: uri.toString(),
mimeType: RESOURCE_MIME_TYPE,
text: html,
- _meta: { ui: uiMetadata },
+ _meta: meta,
},
],
}
diff --git a/services/mcp/tests/unit/ui-apps.test.ts b/services/mcp/tests/unit/ui-apps.test.ts
index bc5207cbbe5a..6860de0c6600 100644
--- a/services/mcp/tests/unit/ui-apps.test.ts
+++ b/services/mcp/tests/unit/ui-apps.test.ts
@@ -141,6 +141,7 @@ describe('ui-apps', () => {
const result = await handler(new URL('ui://posthog/debug.html'))
expect(result.contents[0]._meta.ui.csp.resourceDomains).toContain('https://mcp.posthog.com')
+ expect(result.contents[0]._meta['openai/widgetCSP'].resource_domains).toContain('https://mcp.posthog.com')
})
it('includes analytics URL in CSP when set', async () => {
@@ -158,6 +159,8 @@ describe('ui-apps', () => {
expect(result.contents[0]._meta.ui.csp.resourceDomains).toContain('https://us.i.posthog.com')
expect(result.contents[0]._meta.ui.csp.connectDomains).toContain('https://us.i.posthog.com')
+ expect(result.contents[0]._meta['openai/widgetCSP'].resource_domains).toContain('https://us.i.posthog.com')
+ expect(result.contents[0]._meta['openai/widgetCSP'].connect_domains).toContain('https://us.i.posthog.com')
})
it('omits analytics URL from CSP when not set', async () => {
@@ -172,6 +175,8 @@ describe('ui-apps', () => {
expect(result.contents[0]._meta.ui.csp.resourceDomains).toEqual(['https://mcp.posthog.com'])
expect(result.contents[0]._meta.ui.csp.connectDomains).toEqual([])
+ expect(result.contents[0]._meta['openai/widgetCSP'].resource_domains).toEqual(['https://mcp.posthog.com'])
+ expect(result.contents[0]._meta['openai/widgetCSP'].connect_domains).toEqual([])
})
it('resource handler returns stub HTML with correct base URL', async () => {