Visitar URL original
ref(core): Only inject MCP _meta traceparent with propagateTraceparent · getsentry/sentry-javascript@fa2456a · GitHub
Skip to content

Commit fa2456a

Browse files
ref(core): Only inject MCP _meta traceparent with propagateTraceparent
1 parent 952cf06 commit fa2456a

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

‎packages/core/src/integrations/mcp-server/tracePropagation.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ export function continueMcpTrace<T>(params: unknown, callback: (links: SpanLink[
5252
/**
5353
* Returns a copy of an outgoing request or notification with the active trace context added to `params._meta`, so the
5454
* receiving side can continue the trace. Messages that already carry trace context are returned unchanged.
55-
*
56-
* `traceparent` is always added, independent of the `propagateTraceparent` option: that option guards HTTP requests
57-
* against CORS failures, which do not apply here, and W3C Trace Context is what non-Sentry MCP peers read.
5855
*/
5956
export function injectMcpTraceContext<M extends JsonRpcRequest | JsonRpcNotification>(message: M): M {
6057
const meta = message.params?._meta;
@@ -65,7 +62,13 @@ export function injectMcpTraceContext<M extends JsonRpcRequest | JsonRpcNotifica
6562
return message;
6663
}
6764

68-
const { 'sentry-trace': sentryTrace, baggage, traceparent } = getTraceData({ propagateTraceparent: true });
65+
const {
66+
'sentry-trace': sentryTrace,
67+
baggage,
68+
traceparent,
69+
} = getTraceData({
70+
propagateTraceparent: getClient()?.getOptions().propagateTraceparent,
71+
});
6972
if (!sentryTrace) {
7073
return message;
7174
}

‎packages/core/test/lib/integrations/mcp-server/tracePropagation.test.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,7 @@ describe('MCP server trace propagation via params._meta', () => {
147147

148148
describe('MCP server trace injection into outgoing params._meta', () => {
149149
beforeEach(() => {
150-
const client = new TestClient(
151-
getDefaultTestClientOptions({ dsn: 'https://public@dsn.ingest.sentry.io/1337', tracesSampleRate: 1 }),
152-
);
153-
setCurrentClient(client);
154-
client.init();
150+
initClient({ dsn: 'https://public@dsn.ingest.sentry.io/1337', propagateTraceparent: true });
155151
});
156152

157153
afterEach(() => {
@@ -222,6 +218,20 @@ describe('MCP server trace injection into outgoing params._meta', () => {
222218
});
223219
});
224220

221+
it('only injects traceparent when propagateTraceparent is enabled', async () => {
222+
initClient({ dsn: 'https://public@dsn.ingest.sentry.io/1337' });
223+
const { send, originalSend } = wrappedSend();
224+
225+
await startSpan({ name: 'tools/call get-weather' }, () => send({ jsonrpc: '2.0', method: 'ping', id: 1 }));
226+
227+
expect(originalSend).toHaveBeenCalledWith({
228+
jsonrpc: '2.0',
229+
method: 'ping',
230+
id: 1,
231+
params: { _meta: { 'sentry-trace': expect.any(String), baggage: expect.any(String) } },
232+
});
233+
});
234+
225235
it('keeps existing _meta entries and adds params when there are none', async () => {
226236
const { send, originalSend } = wrappedSend();
227237

0 commit comments

Comments
 (0)