Skip to content

Commit 65c51e6

Browse files
fix(core): Merge existing MCP _meta baggage instead of replacing it
1 parent d5efefb commit 65c51e6

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { getClient } from '../../currentScopes';
77
import { continueTrace } from '../../tracing/trace';
88
import type { SpanLink } from '../../types/link';
9+
import { mergeBaggageHeaders } from '../../utils/baggage';
910
import { isObjectLike } from '../../utils/is';
1011
import { getActiveSpan } from '../../utils/spanUtils';
1112
import { getTraceData } from '../../utils/traceData';
@@ -73,6 +74,8 @@ export function injectMcpTraceContext<M extends JsonRpcRequest | JsonRpcNotifica
7374
return message;
7475
}
7576

77+
const existingBaggage = isObjectLike(meta) && typeof meta.baggage === 'string' ? meta.baggage : undefined;
78+
7679
return {
7780
...message,
7881
params: {
@@ -81,7 +84,7 @@ export function injectMcpTraceContext<M extends JsonRpcRequest | JsonRpcNotifica
8184
...meta,
8285
'sentry-trace': sentryTrace,
8386
...(traceparent && { traceparent }),
84-
...(baggage && { baggage }),
87+
...(baggage && { baggage: mergeBaggageHeaders(existingBaggage, baggage) }),
8588
},
8689
},
8790
};

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,31 @@ describe('MCP server trace injection into outgoing params._meta', () => {
254254
});
255255
});
256256

257+
it('merges existing third-party baggage entries with the Sentry entries', async () => {
258+
const { send, originalSend } = wrappedSend();
259+
260+
await startSpan({ name: 'tools/call get-weather' }, () =>
261+
send({
262+
jsonrpc: '2.0',
263+
method: 'ping',
264+
id: 1,
265+
params: { _meta: { baggage: 'vendor=abc,sentry-release=stale' } },
266+
}),
267+
);
268+
269+
expect(originalSend).toHaveBeenCalledWith({
270+
jsonrpc: '2.0',
271+
method: 'ping',
272+
id: 1,
273+
params: {
274+
_meta: expect.objectContaining({
275+
// The stale Sentry entry is replaced, third-party entries are kept.
276+
baggage: expect.stringMatching(/^vendor=abc,(?!.*stale).*sentry-trace_id=/),
277+
}),
278+
},
279+
});
280+
});
281+
257282
it.each([
258283
['a traceparent', { traceparent: `00-${TRACE_ID}-${PARENT_SPAN_ID}-01` }],
259284
['a sentry-trace', { 'sentry-trace': `${TRACE_ID}-${PARENT_SPAN_ID}-1` }],

0 commit comments

Comments
 (0)