diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/package.json b/dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/package.json index 2b17b465acc8..d052dab7e0cd 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/package.json +++ b/dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/package.json @@ -9,6 +9,7 @@ "typecheck": "tsc --noEmit", "cf-typegen": "wrangler types --include-runtime=false", "test:build": "pnpm install && pnpm build", + "test:build-canary": "pnpm install && pnpm add @modelcontextprotocol/sdk@latest agents@latest && pnpm build", "test:assert": "pnpm typecheck && pnpm test:dev && pnpm test:prod", "test:prod": "TEST_ENV=production playwright test", "test:dev": "TEST_ENV=development playwright test" @@ -27,5 +28,13 @@ "@sentry-internal/test-utils": "file:../../packed/sentry-internal-test-utils-packed.tgz", "typescript": "^6.0.3", "wrangler": "^4.86.0" + }, + "sentryTest": { + "canaryVariants": [ + { + "build-command": "pnpm test:build-canary", + "label": "cloudflare-mcp-agent (latest MCP SDK and agents)" + } + ] } } diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/src/index.ts b/dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/src/index.ts index a55488651188..80e0bf22f22a 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/src/index.ts +++ b/dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/src/index.ts @@ -48,6 +48,24 @@ class MyMCPAgentBase extends McpAgent> { }; }, ); + + // One tool per kind of failure the feedback hint covers. Each goes through a different part of the SDK, so these + // catch SDK and `agents` changes that break the hint. + this.#mcpServer.registerTool('aborted-fetch', {}, async () => { + await fetch('http://localhost:1', { signal: AbortSignal.abort() }); + + return { content: [] }; + }); + + this.#mcpServer.registerTool('returns-error', {}, async () => ({ + isError: true, + content: [{ type: 'text' as const, text: 'Upstream returned 500.' }], + })); + + this.#mcpServer.registerTool('bad-output', { outputSchema: { count: z.number() } }, async () => ({ + content: [{ type: 'text' as const, text: 'not a count' }], + structuredContent: { count: 'not a count' } as unknown as { count: number }, + })); } } diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/tests/feedback.test.ts b/dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/tests/feedback.test.ts index 67ed3a20c4e2..bb61a8222258 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/tests/feedback.test.ts +++ b/dev-packages/e2e-tests/test-applications/cloudflare-mcp-agent/tests/feedback.test.ts @@ -72,3 +72,45 @@ test('McpAgent registers the feedback tool in init() and sends reports', async ( tags: { 'feedback.kind': 'idea', 'feedback.impact': 'friction' }, }); }); + +test('McpAgent adds the feedback hint to every kind of tool failure', async ({ baseURL }) => { + const init = await rpc(baseURL!, undefined, { + jsonrpc: '2.0', + id: 0, + method: 'initialize', + params: { + protocolVersion: '2024-11-05', + capabilities: {}, + clientInfo: { name: 'hint-client', version: '1.0.0' }, + }, + }); + const sessionId = init.headers.get('Mcp-Session-Id')!; + await rpc(baseURL!, sessionId, { jsonrpc: '2.0', method: 'notifications/initialized' }); + + const hint = { + type: 'text', + text: "If this keeps failing after you adjust your request, report it to this server's team with `submit_feedback`.", + }; + const failures = [ + // A thrown error whose `message` has only a getter + ['aborted-fetch', expect.stringContaining('aborted')], + // An error result returned by the tool + ['returns-error', 'Upstream returned 500.'], + // Output the SDK rejects against the tool's `outputSchema` + ['bad-output', expect.stringContaining('Output validation error')], + ] as const; + + for (const [index, [name, text]] of failures.entries()) { + const called = await result( + await rpc(baseURL!, sessionId, { + jsonrpc: '2.0', + id: index + 1, + method: 'tools/call', + params: { name, arguments: {} }, + }), + ); + + expect(called.isError).toBe(true); + expect(called.content).toEqual([{ type: 'text', text }, hint]); + } +}); diff --git a/dev-packages/e2e-tests/test-applications/node-express-mcp-v1/package.json b/dev-packages/e2e-tests/test-applications/node-express-mcp-v1/package.json index 8e17240a05ea..6810e072e908 100644 --- a/dev-packages/e2e-tests/test-applications/node-express-mcp-v1/package.json +++ b/dev-packages/e2e-tests/test-applications/node-express-mcp-v1/package.json @@ -8,6 +8,7 @@ "test": "playwright test", "clean": "npx rimraf node_modules pnpm-lock.yaml", "test:build": "pnpm install && pnpm build", + "test:build-canary": "pnpm install && pnpm add @modelcontextprotocol/sdk@latest && pnpm build", "test:assert": "pnpm test" }, "dependencies": { @@ -26,6 +27,12 @@ }, "type": "module", "sentryTest": { - "optional": true + "optional": true, + "canaryVariants": [ + { + "build-command": "pnpm test:build-canary", + "label": "node-express-mcp-v1 (latest MCP SDK)" + } + ] } } diff --git a/dev-packages/e2e-tests/test-applications/node-express-mcp-v1/src/mcp.ts b/dev-packages/e2e-tests/test-applications/node-express-mcp-v1/src/mcp.ts index e39f80222d62..cb98e26ac355 100644 --- a/dev-packages/e2e-tests/test-applications/node-express-mcp-v1/src/mcp.ts +++ b/dev-packages/e2e-tests/test-applications/node-express-mcp-v1/src/mcp.ts @@ -52,6 +52,24 @@ server.registerTool('always-error', {}, async () => { throw new Error('intentional error for span status testing'); }); +// One tool per kind of failure the feedback hint covers. Each goes through a different part of the SDK, so these catch +// SDK changes that break the hint. +server.registerTool('aborted-fetch', {}, async () => { + await fetch('http://localhost:1', { signal: AbortSignal.abort() }); + + return { content: [] }; +}); + +server.registerTool('returns-error', {}, async () => ({ + isError: true, + content: [{ type: 'text', text: 'Upstream returned 500.' }], +})); + +server.registerTool('bad-output', { outputSchema: { count: z.number() } }, async () => ({ + content: [{ type: 'text', text: 'not a count' }], + structuredContent: { count: 'not a count' } as unknown as { count: number }, +})); + addFeedbackTool(server, { serverName: 'Echo-V1' }); const transports: Record = {}; diff --git a/dev-packages/e2e-tests/test-applications/node-express-mcp-v1/tests/mcp.test.ts b/dev-packages/e2e-tests/test-applications/node-express-mcp-v1/tests/mcp.test.ts index 3bd4421ed339..48473a5ddf4c 100644 --- a/dev-packages/e2e-tests/test-applications/node-express-mcp-v1/tests/mcp.test.ts +++ b/dev-packages/e2e-tests/test-applications/node-express-mcp-v1/tests/mcp.test.ts @@ -177,5 +177,27 @@ test('auto-instruments a legacy MCP SDK v1 server (no manual wrap)', async ({ ba ]); }); + await test.step('adds the hint to every kind of tool failure', async () => { + const hint = { + type: 'text', + text: 'If this keeps failing after you adjust your request, report it to the Echo-V1 team with `submit_feedback`.', + }; + const failures = [ + // A thrown error whose `message` has only a getter + ['aborted-fetch', expect.stringContaining('aborted')], + // An error result returned by the tool + ['returns-error', 'Upstream returned 500.'], + // Output the SDK rejects against the tool's `outputSchema` + ['bad-output', expect.stringContaining('Output validation error')], + ] as const; + + for (const [name, text] of failures) { + const result = await client.callTool({ name, arguments: {} }); + + expect(result.isError).toBe(true); + expect(result.content).toEqual([{ type: 'text', text }, hint]); + } + }); + await client.close(); }); diff --git a/dev-packages/e2e-tests/test-applications/node-express-mcp-v2/package.json b/dev-packages/e2e-tests/test-applications/node-express-mcp-v2/package.json index 94bebf526166..976e3863cd11 100644 --- a/dev-packages/e2e-tests/test-applications/node-express-mcp-v2/package.json +++ b/dev-packages/e2e-tests/test-applications/node-express-mcp-v2/package.json @@ -8,6 +8,7 @@ "test": "playwright test", "clean": "npx rimraf node_modules pnpm-lock.yaml", "test:build": "pnpm install && pnpm build", + "test:build-canary": "pnpm install && pnpm add https://pkg.pr.new/@modelcontextprotocol/server@main https://pkg.pr.new/@modelcontextprotocol/node@main https://pkg.pr.new/@modelcontextprotocol/client@main && pnpm build", "test:build-mcp-sdk-otel": "pnpm install && pnpm add https://pkg.pr.new/@modelcontextprotocol/server@5ee54ee https://pkg.pr.new/@modelcontextprotocol/node@5ee54ee https://pkg.pr.new/@modelcontextprotocol/client@5ee54ee && pnpm build", "test:assert": "pnpm test", "test:assert-mcp-sdk-otel": "MCP_SDK_OTEL=1 pnpm test" @@ -39,6 +40,12 @@ "assert-command": "pnpm test:assert-mcp-sdk-otel", "label": "node-express-mcp-v2 (MCP SDK OpenTelemetry)" } + ], + "canaryVariants": [ + { + "build-command": "pnpm test:build-canary", + "label": "node-express-mcp-v2 (MCP SDK main)" + } ] } } diff --git a/dev-packages/e2e-tests/test-applications/node-express-mcp-v2/src/mcp.ts b/dev-packages/e2e-tests/test-applications/node-express-mcp-v2/src/mcp.ts index 2fd79133ac01..229c48b2c5c4 100644 --- a/dev-packages/e2e-tests/test-applications/node-express-mcp-v2/src/mcp.ts +++ b/dev-packages/e2e-tests/test-applications/node-express-mcp-v2/src/mcp.ts @@ -56,6 +56,32 @@ server.registerTool('always-error', {}, async () => { throw new Error('intentional error for span status testing'); }); +// One tool per kind of failure the feedback hint covers. Each goes through a different part of the SDK, so these catch +// SDK changes that break the hint. +server.registerTool('aborted-fetch', {}, async () => { + await fetch('http://localhost:1', { signal: AbortSignal.abort() }); + + return { content: [] }; +}); + +server.registerTool('returns-error', {}, async () => ({ + isError: true, + content: [{ type: 'text', text: 'Upstream returned 500.' }], +})); + +// SDK v2 writes `structuredContent` that is not an object as a text block when the result has none, so the hint must +// come after it. +server.registerTool('structured-error', {}, async () => ({ + isError: true, + content: [], + structuredContent: ['quota exceeded', 'retry in 60s'], +})); + +server.registerTool('bad-output', { outputSchema: z.object({ count: z.number() }) }, async () => ({ + content: [{ type: 'text', text: 'not a count' }], + structuredContent: { count: 'not a count' } as unknown as { count: number }, +})); + // Registered by hand, unlike the v1 app, to get the handle that turns the tool off. const feedbackTool = createFeedbackTool({ serverName: 'Echo-V2', server }); const registeredFeedbackTool = server.registerTool(feedbackTool.name, feedbackTool.config, feedbackTool.handler); diff --git a/dev-packages/e2e-tests/test-applications/node-express-mcp-v2/tests/mcp.test.ts b/dev-packages/e2e-tests/test-applications/node-express-mcp-v2/tests/mcp.test.ts index e17a5b1adb51..08a65383116c 100644 --- a/dev-packages/e2e-tests/test-applications/node-express-mcp-v2/tests/mcp.test.ts +++ b/dev-packages/e2e-tests/test-applications/node-express-mcp-v2/tests/mcp.test.ts @@ -213,6 +213,30 @@ test('auto-instruments a stable MCP SDK v2 server (no manual wrap)', async ({ ba ]); }); + await test.step('adds the hint to every kind of tool failure', async () => { + const hint = { + type: 'text', + text: 'If this keeps failing after you adjust your request, report it to the Echo-V2 team with `submit_feedback`.', + }; + const failures = [ + // A thrown error whose `message` has only a getter + ['aborted-fetch', expect.stringContaining('aborted')], + // An error result returned by the tool + ['returns-error', 'Upstream returned 500.'], + // Output the SDK rejects against the tool's `outputSchema` + ['bad-output', expect.stringContaining('Output validation error')], + // The SDK's own text for an array `structuredContent`, kept ahead of the hint + ['structured-error', '["quota exceeded","retry in 60s"]'], + ] as const; + + for (const [name, text] of failures) { + const result = await client.callTool({ name, arguments: {} }); + + expect(result.isError).toBe(true); + expect(result.content).toEqual([{ type: 'text', text }, hint]); + } + }); + // Runs last: the feedback tool stays disabled for the rest of the app's life. await test.step('stops pointing at the feedback tool once it is disabled', async () => { await client.callTool({ name: 'disable-feedback', arguments: {} }); diff --git a/packages/mcp/src/createFeedbackTool.ts b/packages/mcp/src/createFeedbackTool.ts index a9d928854b9f..0f77b6e83b3a 100644 --- a/packages/mcp/src/createFeedbackTool.ts +++ b/packages/mcp/src/createFeedbackTool.ts @@ -50,17 +50,21 @@ interface McpServerWithClient { server?: { getClientVersion?: () => { name?: string } | undefined }; } -/** - * What the feedback tool's handler returns. - */ -export interface FeedbackToolResult { +interface FeedbackToolResultBase { content: Array<{ type: 'text'; text: string }>; - structuredContent?: { feedbackId: string }; - isError?: boolean; // MCP SDKs type tool results with an index signature, so a handler without one is not assignable. [key: string]: unknown; } +/** + * What the feedback tool's handler returns: an error, or the sent report's ID as `structuredContent`. + * Two shapes, because MCP SDK v2 only accepts a handler for a tool with an `outputSchema` if every success carries + * `structuredContent` and every error sets `isError: true`. + */ +export type FeedbackToolResult = + | (FeedbackToolResultBase & { isError: true }) + | (FeedbackToolResultBase & { isError?: false; structuredContent: { feedbackId: string } }); + /** * A feedback tool to register on an MCP server: `server.registerTool(tool.name, tool.config, tool.handler)`. */