Repository navigation
feat(core): Inject trace context into outgoing MCP server messages #26311
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
sentry-junior
wants to merge
6
commits into
feat/mcp-server-meta-trace-propagation
from
feat/mcp-server-inject-meta-trace
+325
−6
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f28c077
feat(core): Inject trace context into outgoing MCP server messages
sentry-junior[bot] 6f74886
ref(core): Only inject MCP _meta traceparent with propagateTraceparent
sentry-junior[bot] ef6d851
test(node): Use conventions constants in MCP trace injection test
sentry-junior[bot] 0e7925f
test(node): Collect MCP trace injection spans across envelopes
sentry-junior[bot] a276df9
fix(core): Merge existing MCP _meta baggage instead of replacing it
sentry-junior[bot] d5dbd39
test(node): Cover MCP trace injection into server-to-client requests
sentry-junior[bot] 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
9 changes: 9 additions & 0 deletions
9
dev-packages/node-integration-tests/suites/tracing/mcp-server/trace-injection/instrument.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,9 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); |
44 changes: 44 additions & 0 deletions
44
dev-packages/node-integration-tests/suites/tracing/mcp-server/trace-injection/scenario.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,44 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { Client } from '@modelcontextprotocol/client'; | ||
| import { InMemoryTransport, McpServer } from '@modelcontextprotocol/server'; | ||
|
|
||
| const server = new McpServer({ name: 'Echo', version: '1.0.0' }, { capabilities: { logging: {} } }); | ||
|
|
||
| server.registerTool('echo', {}, async ctx => { | ||
| await ctx.mcpReq.notify({ method: 'notifications/message', params: { level: 'info', data: 'echoing' } }); | ||
| await ctx.mcpReq.elicitInput({ | ||
| message: 'Confirm echo', | ||
| requestedSchema: { type: 'object', properties: { confirm: { type: 'boolean' } } }, | ||
| }); | ||
| return { content: [{ type: 'text', text: 'Tool echo' }] }; | ||
| }); | ||
|
|
||
| async function run() { | ||
| const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); | ||
| const client = new Client({ name: 'test-client', version: '1.0.0' }, { capabilities: { elicitation: { form: {} } } }); | ||
|
|
||
| // The client is not instrumented, so it continues the trace by hand to show what the server put on the wire. | ||
| const continueFromMeta = (params, name) => { | ||
| const meta = params._meta ?? {}; | ||
| Sentry.continueTrace({ sentryTrace: meta['sentry-trace'], baggage: meta.baggage }, () => { | ||
| Sentry.startSpan({ name, op: 'test.client', forceTransaction: true }, () => undefined); | ||
| }); | ||
| }; | ||
|
|
||
| client.setNotificationHandler('notifications/message', notification => { | ||
| continueFromMeta(notification.params, 'handle log message'); | ||
| }); | ||
| client.setRequestHandler('elicitation/create', request => { | ||
| continueFromMeta(request.params, 'handle elicitation'); | ||
| return { action: 'accept', content: { confirm: true } }; | ||
| }); | ||
|
|
||
| await Promise.all([server.connect(serverTransport), client.connect(clientTransport)]); | ||
|
|
||
| await client.callTool({ name: 'echo', arguments: {} }); | ||
|
|
||
| await client.close(); | ||
| await server.close(); | ||
| } | ||
|
|
||
| run(); | ||
46 changes: 46 additions & 0 deletions
46
dev-packages/node-integration-tests/suites/tracing/mcp-server/trace-injection/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,46 @@ | ||
| import { MCP_METHOD_NAME, SENTRY_OP } from '@sentry/conventions/attributes'; | ||
| import { MCP_NOTIFICATION_SERVER_TO_CLIENT, MCP_SERVER } from '@sentry/conventions/op'; | ||
| import type { SerializedStreamedSpan } from '@sentry/core'; | ||
| import { afterAll, describe, expect } from 'vitest'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../../utils/runner'; | ||
|
|
||
| describe('MCP server trace injection into outgoing params._meta', () => { | ||
| afterAll(() => { | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createTestRunner, test) => { | ||
| test('lets the client continue the trace from outgoing notifications and requests', async () => { | ||
| const isToolCall = (span: SerializedStreamedSpan): boolean => | ||
| span.attributes[SENTRY_OP]?.value === MCP_SERVER && span.attributes[MCP_METHOD_NAME]?.value === 'tools/call'; | ||
| const isNotification = (span: SerializedStreamedSpan): boolean => | ||
| span.attributes[SENTRY_OP]?.value === MCP_NOTIFICATION_SERVER_TO_CLIENT; | ||
| const isLogHandler = (span: SerializedStreamedSpan): boolean => span.name === 'handle log message'; | ||
| const isElicitationHandler = (span: SerializedStreamedSpan): boolean => span.name === 'handle elicitation'; | ||
|
|
||
| const runner = createTestRunner(); | ||
| // The spans are separate segments and can arrive in different envelopes, so collect them across envelopes by trace. | ||
| const spansPromise = runner.collectStreamedSpans( | ||
| spans => | ||
| spans.some(isToolCall) && | ||
| spans.some(isNotification) && | ||
| spans.some(isLogHandler) && | ||
| spans.some(isElicitationHandler), | ||
| ); | ||
|
|
||
| await runner.start().completed(); | ||
|
|
||
| const spans = await spansPromise; | ||
| const toolCall = spans.find(isToolCall)!; | ||
| const notification = spans.find(isNotification)!; | ||
| const logHandler = spans.find(isLogHandler)!; | ||
| const elicitationHandler = spans.find(isElicitationHandler)!; | ||
|
|
||
| expect(notification.parent_span_id).toBe(toolCall.span_id); | ||
| expect(logHandler.trace_id).toBe(toolCall.trace_id); | ||
| expect(logHandler.parent_span_id).toBe(notification.span_id); | ||
| expect(elicitationHandler.trace_id).toBe(toolCall.trace_id); | ||
| expect(elicitationHandler.parent_span_id).toBe(toolCall.span_id); | ||
| }); | ||
| }); | ||
| }); |
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
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.