Skip to content

Commit e47e139

Browse files
authored
test(cloudflare): Cover MCP auto-instrumentation with the vite plugin (#27148)
Nothing proved that the Cloudflare vite plugin wraps MCP servers on its own, fixed by an integration test whose worker builds an `McpServer` with no Sentry calls. The test checks the MCP span, and that `mcpFeedbackIntegration` adds the feedback tool, its hint on errors and the report envelope.
1 parent 7f2ac7e commit e47e139

7 files changed

Lines changed: 123 additions & 1 deletion

File tree

‎dev-packages/cloudflare-integration-tests/package.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@langchain/langgraph": "^1.0.1",
2525
"@langchain/openai": "^1.6.2",
2626
"@langchain/typesafe": "^0.0.2",
27+
"@modelcontextprotocol/sdk": "1.32.2",
2728
"@opentelemetry/api": "^1.9.1",
2829
"@prisma/adapter-d1": "6.15.0",
2930
"@prisma/client": "6.15.0",
@@ -32,12 +33,14 @@
3233
"@sentry/conventions": "0.37.0",
3334
"@sentry/core": "11.23.0",
3435
"@sentry/hono": "11.23.0",
36+
"@sentry/mcp": "11.23.0",
3537
"ai": "^6.0.0",
3638
"hono": "^4.13.7",
3739
"openai": "5.18.1",
3840
"prisma": "6.15.0",
3941
"vite": "^8.4.1",
4042
"vitest": "^4.1.12",
41-
"wrangler": "4.156.0"
43+
"wrangler": "4.156.0",
44+
"zod": "^4.6.7"
4245
}
4346
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
2+
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
3+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
4+
import { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/sdk/validation/cfworker';
5+
import * as z from 'zod';
6+
7+
export default {
8+
async fetch(_request, _env, _ctx) {
9+
// Not wrapped with `wrapMcpServerWithSentry`: the vite plugin instruments `McpServer`.
10+
const server = new McpServer({ name: 'mcp-server-worker', version: '1.0.0' });
11+
server.registerTool('echo', { inputSchema: { message: z.string() } }, async ({ message }) => ({
12+
content: [{ type: 'text' as const, text: `Echo: ${message}` }],
13+
}));
14+
server.registerTool('returns-error', {}, async () => ({
15+
isError: true,
16+
content: [{ type: 'text' as const, text: 'Upstream returned 500.' }],
17+
}));
18+
19+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
20+
await server.connect(serverTransport);
21+
// The default validator compiles schemas with `new Function`, which workerd does not allow.
22+
const client = new Client(
23+
{ name: 'test-client', version: '1.0.0' },
24+
{ jsonSchemaValidator: new CfWorkerJsonSchemaValidator() },
25+
);
26+
await client.connect(clientTransport);
27+
28+
const tools = await client.listTools();
29+
const echo = await client.callTool({ name: 'echo', arguments: { message: 'hi' } });
30+
const failed = await client.callTool({ name: 'returns-error', arguments: {} });
31+
const feedback = await client.callTool({
32+
name: 'submit_feedback',
33+
arguments: { task: 'report from an agent', kind: 'idea', impact: 'friction', description: 'From a worker.' },
34+
});
35+
await client.close();
36+
37+
return Response.json({
38+
tools: tools.tools.map(tool => tool.name),
39+
echo: echo.content,
40+
failed: failed.content,
41+
feedback: feedback.content,
42+
});
43+
},
44+
} satisfies ExportedHandler;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as Sentry from '@sentry/cloudflare';
2+
import { mcpFeedbackIntegration } from '@sentry/mcp';
3+
4+
interface Env {
5+
SENTRY_DSN: string;
6+
}
7+
8+
export default Sentry.defineCloudflareOptions((env: Env) => ({
9+
dsn: env.SENTRY_DSN,
10+
tracesSampleRate: 1,
11+
integrations: [mcpFeedbackIntegration()],
12+
}));
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { expect, it } from 'vitest';
2+
import { createRunner } from '../../../../runner';
3+
4+
it('wraps an McpServer the vite plugin instrumented, so mcpFeedbackIntegration adds the feedback tool', async ({
5+
signal,
6+
}) => {
7+
const runner = createRunner(__dirname)
8+
.ignore('event')
9+
.unordered()
10+
.expect(envelope => {
11+
const [, feedback] = envelope[1][0] as [unknown, Record<string, any>];
12+
expect(feedback).toMatchObject({
13+
type: 'feedback',
14+
contexts: { feedback: { source: 'mcp_agent', name: 'MCP agent (test-client)' } },
15+
tags: { 'feedback.kind': 'idea', 'feedback.impact': 'friction' },
16+
});
17+
})
18+
.start(signal);
19+
20+
const spansPromise = runner.collectStreamedSpans(spans => spans.some(span => span.name === 'tools/call echo'));
21+
22+
const result = await runner.makeRequest<Record<string, unknown>>('get', '/');
23+
24+
expect(result).toEqual({
25+
tools: ['submit_feedback', 'echo', 'returns-error'],
26+
echo: [{ type: 'text', text: 'Echo: hi' }],
27+
failed: [
28+
{ type: 'text', text: 'Upstream returned 500.' },
29+
{
30+
type: 'text',
31+
text: 'If this keeps failing after you adjust your request, report it to the mcp-server-worker team with `submit_feedback`.',
32+
},
33+
],
34+
feedback: [{ type: 'text', text: expect.stringMatching(/^Feedback sent \(ID [0-9a-f]{32}\)\.$/) }],
35+
});
36+
37+
// Only `wrapMcpServerWithSentry` creates MCP spans, so this one shows the plugin wrapped the server.
38+
expect((await spansPromise).find(span => span.name === 'tools/call echo')).toBeDefined();
39+
40+
await runner.completed();
41+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { cloudflare } from '@cloudflare/vite-plugin';
2+
import { sentryCloudflareVitePlugin } from '@sentry/cloudflare/vite';
3+
import { defineConfig } from 'vite';
4+
5+
export default defineConfig({
6+
plugins: [cloudflare(), sentryCloudflareVitePlugin()],
7+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "mcp-server-worker",
3+
"compatibility_date": "2026-04-26",
4+
"main": "index.ts",
5+
"compatibility_flags": ["nodejs_compat"],
6+
}

‎pnpm-lock.yaml‎

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)