-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(core): Support stable MCP SDK v2 #22883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sergical
wants to merge
5
commits into
develop
Choose a base branch
from
feat/mcp-sdk-v2-support
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f46bc61
feat(core): support stable MCP SDK v2
sergical 7da20a0
test(cloudflare): retain MCP HTTP span assertions
sergical eab5aa9
Merge branch 'develop' into feat/mcp-sdk-v2-support
sergical 5c3fbc6
fix(core): scope MCP session metadata extraction
sergical f7c3a99
test(core): cover MCP metadata dispatch
sergical File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 130 additions & 77 deletions
207
dev-packages/e2e-tests/test-applications/cloudflare-mcp/tests/index.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,111 +1,164 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForRequest } from '@sentry-internal/test-utils'; | ||
|
|
||
| test('sends spans for MCP tool calls', async ({ baseURL }) => { | ||
| const spanRequestWaiter = waitForRequest('cloudflare-mcp', event => { | ||
| const transaction = event.envelope[1][0][1]; | ||
| return typeof transaction !== 'string' && 'transaction' in transaction && transaction.transaction === 'POST /mcp'; | ||
| }); | ||
| const APP_NAME = 'cloudflare-mcp'; | ||
|
|
||
| function getTransaction(eventData: Awaited<ReturnType<typeof waitForRequest>>) { | ||
| const event = eventData.envelope[1][0][1]; | ||
| return typeof event !== 'string' && 'transaction' in event ? event : undefined; | ||
| } | ||
|
|
||
| function requireTransaction(eventData: Awaited<ReturnType<typeof waitForRequest>>) { | ||
| const event = getTransaction(eventData); | ||
| if (!event) { | ||
| throw new Error('Expected a transaction event'); | ||
| } | ||
| return event; | ||
| } | ||
|
|
||
| test.describe.configure({ mode: 'serial' }); | ||
|
|
||
| const spanMcpWaiter = waitForRequest('cloudflare-mcp', event => { | ||
| const transaction = event.envelope[1][0][1]; | ||
| test('sends spans for MCP 2026-07-28 tool calls', async ({ baseURL }) => { | ||
| const url = `${baseURL}/mcp?protocol=modern`; | ||
| const requestWaiter = waitForRequest(APP_NAME, eventData => { | ||
| const event = getTransaction(eventData); | ||
| return event?.transaction === 'POST /mcp' && event.contexts?.trace?.data?.['url.full'] === url; | ||
| }); | ||
| const mcpWaiter = waitForRequest(APP_NAME, eventData => { | ||
| const event = getTransaction(eventData); | ||
| return ( | ||
| typeof transaction !== 'string' && | ||
| 'transaction' in transaction && | ||
| transaction.transaction === 'tools/call my-tool' | ||
| event?.transaction === 'tools/call my-tool' && | ||
| event.contexts?.trace?.data?.['mcp.protocol.version'] === '2026-07-28' | ||
| ); | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/mcp`, { | ||
| const response = await fetch(url, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Accept: 'application/json, text/event-stream', | ||
| 'MCP-Protocol-Version': '2026-07-28', | ||
| 'Mcp-Method': 'tools/call', | ||
| 'Mcp-Name': 'my-tool', | ||
| }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| id: 'modern-tool-call', | ||
| method: 'tools/call', | ||
| params: { | ||
| _meta: { | ||
| 'io.modelcontextprotocol/protocolVersion': '2026-07-28', | ||
| 'io.modelcontextprotocol/clientInfo': { | ||
| name: 'cloudflare-modern-client', | ||
| version: '2.0.0', | ||
| }, | ||
| 'io.modelcontextprotocol/clientCapabilities': {}, | ||
| }, | ||
| name: 'my-tool', | ||
| arguments: { | ||
| message: 'ʕっ•ᴥ•ʔっ', | ||
| message: 'modern protocol request', | ||
| }, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| await expect(response.json()).resolves.toMatchObject({ | ||
| jsonrpc: '2.0', | ||
| id: 'modern-tool-call', | ||
| result: { | ||
| resultType: 'complete', | ||
| content: [{ type: 'text', text: 'Tool my-tool: modern protocol request' }], | ||
| }, | ||
| }); | ||
|
|
||
| const requestData = await spanRequestWaiter; | ||
| const mcpData = await spanMcpWaiter; | ||
|
|
||
| const requestEvent = requestData.envelope[1][0][1]; | ||
| const mcpEvent = mcpData.envelope[1][0][1]; | ||
|
|
||
| // Check that the events have contexts | ||
| // this is for TypeScript type safety | ||
| if ( | ||
| typeof mcpEvent === 'string' || | ||
| !('contexts' in mcpEvent) || | ||
| typeof requestEvent === 'string' || | ||
| !('contexts' in requestEvent) | ||
| ) { | ||
| throw new Error("Events don't have contexts"); | ||
| } | ||
| const requestData = await requestWaiter; | ||
| const mcpData = await mcpWaiter; | ||
| const requestEvent = requireTransaction(requestData); | ||
| const mcpEvent = requireTransaction(mcpData); | ||
| const requestTrace = requestEvent.contexts?.trace; | ||
| const mcpTrace = mcpEvent.contexts?.trace; | ||
|
|
||
| expect(mcpEvent.contexts?.trace?.trace_id).toBe((mcpData.envelope[0].trace as any).trace_id); | ||
| expect(requestTrace?.op).toBe('http.server'); | ||
| expect(requestTrace?.origin).toBe('auto.http.cloudflare'); | ||
| expect(requestTrace?.status).toBe('ok'); | ||
| expect(requestTrace?.data?.['sentry.origin']).toBe('auto.http.cloudflare'); | ||
| expect(requestTrace?.data?.['sentry.op']).toBe('http.server'); | ||
| expect(requestTrace?.data?.['sentry.source']).toBe('url'); | ||
| expect(requestTrace?.data?.['http.request.method']).toBe('POST'); | ||
| expect(requestTrace?.data?.['url.path']).toBe('/mcp'); | ||
| expect(requestTrace?.data?.['url.full']).toBe(url); | ||
| expect(requestTrace?.data?.['url.port']).toBe('38787'); | ||
| expect(requestTrace?.data?.['url.scheme']).toBe('http:'); | ||
| expect(requestTrace?.data?.['server.address']).toBe('localhost'); | ||
| expect(requestTrace?.data?.['http.request.body.size']).toBe(345); | ||
| expect(requestTrace?.data?.['user_agent.original']).toBe('node'); | ||
| expect(requestTrace?.data?.['http.request.header.content_type']).toBe('application/json'); | ||
| expect(requestTrace?.data?.['network.protocol.name']).toBe('HTTP/1.1'); | ||
| expect(requestTrace?.data?.['http.response.status_code']).toBe(200); | ||
| expect(requestTrace?.data?.['mcp.server.extra']).toBe(' /|\ ^._.^ /|\ '); | ||
| expect(mcpTrace?.trace_id).toBe(requestTrace?.trace_id); | ||
| expect(mcpTrace?.trace_id).toBe((mcpData.envelope[0].trace as { trace_id: string }).trace_id); | ||
| expect(mcpTrace?.parent_span_id).toBe(requestTrace?.span_id); | ||
| expect(requestData.envelope[0].event_id).not.toBe(mcpData.envelope[0].event_id); | ||
| expect(mcpTrace?.op).toBe('mcp.server'); | ||
| expect(mcpTrace?.origin).toBe('auto.function.mcp_server'); | ||
| expect(mcpTrace?.status).toBe('ok'); | ||
| expect(mcpTrace?.data?.['mcp.transport']).toBe('PerRequestHTTPServerTransport'); | ||
| expect(mcpTrace?.data?.['network.transport']).toBe('tcp'); | ||
| expect(mcpTrace?.data?.['mcp.protocol.version']).toBe('2026-07-28'); | ||
| expect(mcpTrace?.data?.['mcp.client.name']).toBe('cloudflare-modern-client'); | ||
| expect(mcpTrace?.data?.['mcp.client.version']).toBe('2.0.0'); | ||
| expect(mcpTrace?.data?.['mcp.server.name']).toBe('cloudflare-mcp'); | ||
| expect(mcpTrace?.data?.['mcp.server.version']).toBe('2.0.0'); | ||
| expect(mcpTrace?.data?.['mcp.method.name']).toBe('tools/call'); | ||
| expect(mcpTrace?.data?.['mcp.request.id']).toBe('modern-tool-call'); | ||
| expect(mcpTrace?.data?.['mcp.tool.name']).toBe('my-tool'); | ||
| expect(mcpTrace?.data?.['mcp.request.argument.message']).toBe('"modern protocol request"'); | ||
| expect(mcpTrace?.data?.['mcp.tool.result.content_count']).toBe(1); | ||
| expect(mcpTrace?.data?.['mcp.tool.result.content']).toBe('Tool my-tool: modern protocol request'); | ||
| }); | ||
|
|
||
| expect(requestEvent.contexts?.trace).toEqual({ | ||
| span_id: expect.any(String), | ||
| trace_id: expect.any(String), | ||
| data: expect.objectContaining({ | ||
| 'sentry.origin': 'auto.http.cloudflare', | ||
| 'sentry.op': 'http.server', | ||
| 'sentry.source': 'url', | ||
| 'sentry.sample_rate': 1, | ||
| 'http.request.method': 'POST', | ||
| 'url.path': '/mcp', | ||
| 'url.full': 'http://localhost:38787/mcp', | ||
| 'url.port': '38787', | ||
| 'url.scheme': 'http:', | ||
| 'server.address': 'localhost', | ||
| 'http.request.body.size': 120, | ||
| 'user_agent.original': 'node', | ||
| 'http.request.header.content_type': 'application/json', | ||
| 'network.protocol.name': 'HTTP/1.1', | ||
| 'mcp.server.extra': ' /|\ ^._.^ /|\ ', | ||
| 'http.response.status_code': 200, | ||
| }), | ||
| op: 'http.server', | ||
| status: 'ok', | ||
| origin: 'auto.http.cloudflare', | ||
| test('keeps sending spans for legacy-compatible MCP tool calls', async ({ baseURL }) => { | ||
| const url = `${baseURL}/mcp?protocol=legacy`; | ||
| const mcpWaiter = waitForRequest(APP_NAME, eventData => { | ||
| const event = getTransaction(eventData); | ||
| return ( | ||
| event?.transaction === 'tools/call my-tool' && | ||
| event.contexts?.trace?.data?.['mcp.request.argument.message'] === '"legacy protocol request"' | ||
| ); | ||
| }); | ||
|
|
||
| expect(mcpEvent.contexts?.trace).toEqual({ | ||
| trace_id: expect.any(String), | ||
| parent_span_id: requestEvent.contexts?.trace?.span_id, | ||
| span_id: expect.any(String), | ||
| op: 'mcp.server', | ||
| origin: 'auto.function.mcp_server', | ||
| status: 'ok', | ||
| data: { | ||
| 'sentry.origin': 'auto.function.mcp_server', | ||
| 'sentry.op': 'mcp.server', | ||
| 'sentry.source': 'route', | ||
| 'mcp.transport': 'WorkerTransport', | ||
| 'network.transport': 'unknown', | ||
| 'network.protocol.version': '2.0', | ||
| 'mcp.method.name': 'tools/call', | ||
| 'mcp.request.id': '1', | ||
| 'mcp.tool.name': 'my-tool', | ||
| 'mcp.request.argument.message': '"ʕっ•ᴥ•ʔっ"', | ||
| 'mcp.tool.extra': 'ƸӜƷ', | ||
| 'mcp.tool.input': '{"message":"ʕっ•ᴥ•ʔっ"}', | ||
| 'mcp.tool.result.content_count': 1, | ||
| 'mcp.tool.result.content_type': 'text', | ||
| 'mcp.tool.result.content': 'Tool my-tool: ʕっ•ᴥ•ʔっ', | ||
| const response = await fetch(url, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Accept: 'application/json, text/event-stream', | ||
| }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: '2.0', | ||
| id: 'legacy-tool-call', | ||
| method: 'tools/call', | ||
| params: { | ||
| name: 'my-tool', | ||
| arguments: { | ||
| message: 'legacy protocol request', | ||
| }, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| expect(response.status).toBe(200); | ||
|
|
||
| const mcpEvent = requireTransaction(await mcpWaiter); | ||
| const trace = mcpEvent.contexts?.trace; | ||
|
|
||
| expect(trace?.op).toBe('mcp.server'); | ||
| expect(trace?.status).toBe('ok'); | ||
| expect(trace?.data?.['mcp.transport']).toBe('WebStandardStreamableHTTPServerTransport'); | ||
| expect(trace?.data?.['mcp.method.name']).toBe('tools/call'); | ||
| expect(trace?.data?.['mcp.request.id']).toBe('legacy-tool-call'); | ||
| expect(trace?.data?.['mcp.tool.name']).toBe('my-tool'); | ||
| expect(trace?.data?.['mcp.protocol.version']).toBeUndefined(); | ||
| expect(trace?.data?.['mcp.tool.result.content']).toBe('Tool my-tool: legacy protocol request'); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not my bear 😭