Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)"
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ class MyMCPAgentBase extends McpAgent<Env, unknown, Record<string, unknown>> {
};
},
);

// 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 },
}));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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)"
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, StreamableHTTPServerTransport> = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)"
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {} });
Expand Down
16 changes: 10 additions & 6 deletions packages/mcp/src/createFeedbackTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)`.
*/
Expand Down
Loading