Repository navigation
fix(core): capture accurate MCP network attributes #24506
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
Draft
betegon
wants to merge
2
commits into
develop
Choose a base branch
from
bt/fix-mcp-network-attributes
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.
+330
−49
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
55 changes: 55 additions & 0 deletions
55
...e-integration-tests/suites/tracing/mcp-server/manual-instrumentation/scenario-http-v1.mjs
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 |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; | ||
| import { WebStandardStreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js'; | ||
| import { wrapMcpServerWithSentry } from '@sentry/node'; | ||
|
|
||
| class CustomTransport extends WebStandardStreamableHTTPServerTransport {} | ||
|
|
||
| const server = wrapMcpServerWithSentry(new McpServer({ name: 'Echo', version: '1.0.0' })); | ||
| server.registerTool('echo', {}, async () => ({ content: [{ type: 'text', text: 'Tool echo' }] })); | ||
| const transport = new CustomTransport({ sessionIdGenerator: () => 'test-session', enableJsonResponse: true }); | ||
|
|
||
| async function request(id, method, userAgent, params) { | ||
| const headers = { | ||
| 'Content-Type': 'application/json', | ||
| Accept: 'application/json, text/event-stream', | ||
| 'Mcp-Protocol-Version': '2025-11-25', | ||
| }; | ||
| if (userAgent !== undefined) { | ||
| headers['User-Agent'] = userAgent; | ||
| } | ||
| if (transport.sessionId !== undefined) { | ||
| headers['Mcp-Session-Id'] = transport.sessionId; | ||
| } | ||
|
|
||
| const response = await transport.handleRequest( | ||
| new Request('https://example.test/mcp', { | ||
| method: 'POST', | ||
| headers, | ||
| body: JSON.stringify({ jsonrpc: '2.0', id, method, params }), | ||
| }), | ||
| ); | ||
| return response.status === 202 ? undefined : response.json(); | ||
| } | ||
|
|
||
| async function run() { | ||
| await server.connect(transport); | ||
| const initialize = await request(1, 'initialize', 'initialize-client/1.0', { | ||
| protocolVersion: '2025-11-25', | ||
| capabilities: {}, | ||
| clientInfo: { name: 'test-client', version: '1.0.0' }, | ||
| }); | ||
| assert.ok(initialize.result); | ||
| await request(undefined, 'notifications/initialized'); | ||
|
|
||
| const [tools, resources] = await Promise.all([ | ||
| request(2, 'tools/list', 'tools-client/2.0'), | ||
| request(3, 'resources/list', 'resources-client/3.0'), | ||
| ]); | ||
| assert.equal(tools.result.tools[0].name, 'echo'); | ||
| assert.equal(resources.error.code, -32601); | ||
| assert.ok((await request(4, 'tools/list')).result); | ||
| await server.close(); | ||
| } | ||
|
|
||
| run(); |
54 changes: 54 additions & 0 deletions
54
...e-integration-tests/suites/tracing/mcp-server/manual-instrumentation/scenario-http-v2.mjs
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 |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { createMcpHandler, McpServer } from '@modelcontextprotocol/server'; | ||
| import { wrapMcpServerWithSentry } from '@sentry/node'; | ||
|
|
||
| const handler = createMcpHandler(() => { | ||
| const server = wrapMcpServerWithSentry(new McpServer({ name: 'Echo', version: '1.0.0' })); | ||
| server.registerTool('echo', {}, async () => ({ content: [{ type: 'text', text: 'Tool echo' }] })); | ||
| return server; | ||
| }); | ||
|
|
||
| async function request(id, method, userAgent) { | ||
| const headers = { | ||
| 'Content-Type': 'application/json', | ||
| Accept: 'application/json, text/event-stream', | ||
| 'Mcp-Protocol-Version': '2026-07-28', | ||
| 'Mcp-Method': method, | ||
| }; | ||
| if (userAgent !== undefined) { | ||
| headers['User-Agent'] = userAgent; | ||
| } | ||
|
|
||
| const response = await handler.fetch( | ||
| new Request('https://example.test/mcp', { | ||
| method: 'POST', | ||
| headers, | ||
| body: JSON.stringify({ | ||
| jsonrpc: '2.0', | ||
| id, | ||
| method, | ||
| params: { | ||
| _meta: { | ||
| 'io.modelcontextprotocol/protocolVersion': '2026-07-28', | ||
| 'io.modelcontextprotocol/clientCapabilities': {}, | ||
| }, | ||
| }, | ||
| }), | ||
| }), | ||
| ); | ||
| return response.json(); | ||
| } | ||
|
|
||
| async function run() { | ||
| const [discovery, tools, resources] = await Promise.all([ | ||
| request(1, 'server/discover', 'discovery-client/1.0'), | ||
| request(2, 'tools/list', 'tools-client/2.0'), | ||
| request(3, 'resources/list', 'resources-client/3.0'), | ||
| ]); | ||
| assert.ok(discovery.result); | ||
| assert.equal(tools.result.tools[0].name, 'echo'); | ||
| assert.equal(resources.error.code, -32601); | ||
| assert.ok((await request(4, 'tools/list')).result); | ||
| } | ||
|
|
||
| run(); |
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
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
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
64 changes: 64 additions & 0 deletions
64
packages/core/test/lib/integrations/mcp-server/networkAttributes.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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { buildTransportAttributes } from '../../../../src/integrations/mcp-server/sessionExtraction'; | ||
| import { createMockStdioTransport, createMockTransport } from './testUtils'; | ||
|
|
||
| describe('MCP network attributes', () => { | ||
| it('identifies per-request HTTP without request metadata', () => { | ||
| const transport = createMockTransport(); | ||
| Object.defineProperty(transport, 'constructor', { value: { name: 'PerRequestHTTPServerTransport' } }); | ||
|
|
||
| expect(buildTransportAttributes(transport)).toEqual({ | ||
| 'mcp.transport': 'PerRequestHTTPServerTransport', | ||
| 'mcp.session.id': 'test-session-123', | ||
| 'network.protocol.name': 'http', | ||
| }); | ||
| }); | ||
|
|
||
| it('does not report JSON-RPC as a network protocol for stdio', () => { | ||
| const attributes = buildTransportAttributes(createMockStdioTransport()); | ||
|
|
||
| expect(attributes).toMatchObject({ | ||
| 'mcp.transport': 'StdioServerTransport', | ||
| 'network.transport': 'pipe', | ||
| }); | ||
| expect(attributes).not.toHaveProperty('network.protocol.name'); | ||
| expect(attributes).not.toHaveProperty('network.protocol.version'); | ||
| expect(attributes).not.toHaveProperty('user_agent.original'); | ||
| }); | ||
|
|
||
| it('does not infer a network transport from a custom class name', () => { | ||
| const transport = createMockTransport(); | ||
| Object.defineProperty(transport, 'constructor', { value: { name: 'CustomHttpStdioTransport' } }); | ||
|
|
||
| expect(buildTransportAttributes(transport)).toEqual({ | ||
| 'mcp.transport': 'CustomHttpStdioTransport', | ||
| 'mcp.session.id': 'test-session-123', | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['HTTP/1.1', '1.1', 'tcp'], | ||
| ['HTTP/2', '2', 'tcp'], | ||
| ['HTTP/3', '3', 'quic'], | ||
| ])('records Cloudflare %s metadata from the current request', (httpProtocol, version, networkTransport) => { | ||
| const request = Object.assign(new Request('https://example.com/mcp'), { cf: { httpProtocol } }); | ||
| const attributes = buildTransportAttributes(createMockTransport(), { request }); | ||
|
|
||
| expect(attributes).toMatchObject({ | ||
| 'network.protocol.name': 'http', | ||
| 'network.protocol.version': version, | ||
| 'network.transport': networkTransport, | ||
| }); | ||
| }); | ||
|
|
||
| it('omits unknown HTTP version and transport instead of inferring them from the URL or headers', () => { | ||
| const request = new Request('https://example.com/mcp', { | ||
| headers: { 'MCP-Protocol-Version': '2026-07-28', 'X-Forwarded-Proto': 'https' }, | ||
| }); | ||
| const attributes = buildTransportAttributes(createMockTransport(), { request }); | ||
|
|
||
| expect(attributes['network.protocol.name']).toBe('http'); | ||
| expect(attributes).not.toHaveProperty('network.protocol.version'); | ||
| expect(attributes).not.toHaveProperty('network.transport'); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.