Skip to content

Commit 974f4cf

Browse files
fix(core): Fall back to genAI for MCP content when mcp is unset
Keeps the MCP server instrumentation non-breaking: `dataCollection.mcp` takes precedence per field, and unset fields fall back to `dataCollection.genAI` until v12. Co-Authored-By: Jan Peer Stöcklmair <jan.peer@sentry.io>
1 parent a9fa6f9 commit 974f4cf

7 files changed

Lines changed: 82 additions & 42 deletions

File tree

‎dev-packages/e2e-tests/test-applications/node-express-static/src/app.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ Sentry.init({
1717
tunnel: `http://localhost:3031/`, // proxy server
1818
tracesSampleRate: 1,
1919
dataCollection: {
20-
mcp: {
20+
// Covers the `genAI` fallback for MCP content. The node-express app covers `mcp`.
21+
// TODO(v12): Switch to `mcp` once the `genAI` fallback is removed.
22+
genAI: {
2123
inputs: false,
2224
outputs: false,
2325
},

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ function resolveMcpOptions(options: McpServerWrapperOptions): ResolvedMcpOptions
2727
};
2828
}
2929

30-
const mcp = getClient()?.getDataCollectionOptions().mcp;
30+
const client = getClient();
31+
// TODO(v12): Drop the `genAI` fallback and read `client.getDataCollectionOptions().mcp`. Until then, the raw option
32+
// is read because the resolved `mcp` always has defaults applied and would hide whether the user set it.
33+
const userMcp = client?.getOptions().dataCollection?.mcp;
34+
const genAI = client?.getDataCollectionOptions().genAI;
3135

3236
return {
33-
recordInputs: options.recordInputs ?? mcp?.inputs ?? true,
34-
recordOutputs: options.recordOutputs ?? mcp?.outputs ?? true,
37+
recordInputs: options.recordInputs ?? userMcp?.inputs ?? genAI?.inputs ?? true,
38+
recordOutputs: options.recordOutputs ?? userMcp?.outputs ?? genAI?.outputs ?? true,
3539
};
3640
}
3741

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ export type SessionData = {
231231
* Options for configuring the MCP server wrapper.
232232
*/
233233
export type McpServerWrapperOptions = {
234-
/** Whether to capture tool/prompt input arguments in spans. Defaults to `dataCollection.mcp.inputs`. */
234+
/** Whether to capture tool/prompt input arguments in spans. Defaults to `dataCollection.mcp.inputs`, then `dataCollection.genAI.inputs`. */
235235
recordInputs?: boolean;
236-
/** Whether to capture tool/prompt output results in spans. Defaults to `dataCollection.mcp.outputs`. */
236+
/** Whether to capture tool/prompt output results in spans. Defaults to `dataCollection.mcp.outputs`, then `dataCollection.genAI.outputs`. */
237237
recordOutputs?: boolean;
238238
};
239239

‎packages/core/src/types/datacollection.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export interface DataCollection {
9595
* Metadata (method name, tool or prompt name, session and protocol version, content counts, error status) is always
9696
* collected. MCP content is gated by this option, not `genAI`, even when it is stored on `gen_ai.*` attributes.
9797
* The `recordInputs`/`recordOutputs` options of `wrapMcpServerWithSentry`, when set, take precedence over these values.
98+
* When a field isn't set, the MCP server instrumentation falls back to the matching `genAI` field.
9899
* @default { inputs: true, outputs: true }
99100
*/
100101
mcp?: {

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { getCurrentScope, withScope } from '../../../../src/currentScopes';
33
import { wrapMcpServerWithSentry } from '../../../../src/integrations/mcp-server';
44
import { Scope } from '../../../../src/scope';
55
import * as tracingModule from '../../../../src/tracing/trace';
6-
import { createMockClient, createMockMcpServer, createMockTransport } from './testUtils';
6+
import type { DataCollection } from '../../../../src/types/datacollection';
7+
import {
8+
createMockClient,
9+
createMockClientWithDataCollection,
10+
createMockMcpServer,
11+
createMockTransport,
12+
} from './testUtils';
713

814
describe('MCP Server Capture Policy', () => {
915
type MockTransport = ReturnType<typeof createMockTransport>;
@@ -416,4 +422,50 @@ describe('MCP Server Capture Policy', () => {
416422
);
417423
expectToolResult(span);
418424
});
425+
426+
describe('dataCollection precedence', () => {
427+
it.each<[string, DataCollection, { inputs: boolean; outputs: boolean }]>([
428+
['records by default', {}, { inputs: true, outputs: true }],
429+
['uses mcp', { mcp: { inputs: false, outputs: false } }, { inputs: false, outputs: false }],
430+
// TODO(v12): Remove the genAI fallback cases.
431+
[
432+
'falls back to genAI when mcp is unset',
433+
{ genAI: { inputs: false, outputs: false } },
434+
{ inputs: false, outputs: false },
435+
],
436+
[
437+
'prefers mcp over genAI',
438+
{ mcp: { inputs: true, outputs: true }, genAI: { inputs: false, outputs: false } },
439+
{ inputs: true, outputs: true },
440+
],
441+
[
442+
'prefers a disabled mcp over an enabled genAI',
443+
{ mcp: { inputs: false, outputs: false }, genAI: { inputs: true, outputs: true } },
444+
{ inputs: false, outputs: false },
445+
],
446+
[
447+
'falls back to genAI per field',
448+
{ mcp: { inputs: true }, genAI: { inputs: false, outputs: false } },
449+
{ inputs: true, outputs: false },
450+
],
451+
])('%s', async (_, dataCollection, expected) => {
452+
const sessionId = `data-collection-${JSON.stringify(dataCollection)}`;
453+
const transport = await connectWrappedServer(sessionId);
454+
const scope = new Scope();
455+
scope.setClient(createMockClientWithDataCollection(dataCollection));
456+
const span = queueInactiveSpan();
457+
458+
receiveToolCall(transport, scope, { id: 'precedence-request', location: 'Vienna, Austria' });
459+
await sendToolResult(transport, scope, { id: 'precedence-request', text: 'Forecast for Vienna' });
460+
461+
expect(startInactiveSpanSpy).toHaveBeenCalledWith(
462+
buildToolSpanConfig({
463+
id: 'precedence-request',
464+
sessionId,
465+
...(expected.inputs && { location: 'Vienna, Austria' }),
466+
}),
467+
);
468+
expectToolResult(span, expected.outputs ? 'Forecast for Vienna' : undefined);
469+
});
470+
});
419471
});

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import type { Client } from '../../../../src/client';
2+
import type { DataCollection } from '../../../../src/types/datacollection';
3+
import { resolveDataCollectionOptions } from '../../../../src/utils/data-collection/resolveDataCollectionOptions';
24
import { vi } from 'vitest';
35

46
/**
57
* Creates a mock Sentry client with getDataCollectionOptions for use in MCP server tests.
68
* @param userInfo - Whether user identity data (IP, port) is collected. Default: true
7-
* @param mcp - Whether MCP inputs/outputs are recorded. Defaults to match userInfo.
9+
* @param mcp - The `dataCollection.mcp` setting. Defaults to match userInfo.
810
* @param traceLifecycle - Whether spans are streamed or sent as transactions. Default: 'static'
911
*/
1012
export function createMockClient(
@@ -14,7 +16,7 @@ export function createMockClient(
1416
): Client {
1517
const mcpOptions = mcp ?? { inputs: userInfo, outputs: userInfo };
1618
return {
17-
getOptions: () => ({ traceLifecycle }),
19+
getOptions: () => ({ traceLifecycle, dataCollection: { mcp: mcpOptions } }),
1820
getDataCollectionOptions: () => ({
1921
userInfo,
2022
cookies: true,
@@ -33,6 +35,18 @@ export function createMockClient(
3335
} as unknown as Client;
3436
}
3537

38+
/**
39+
* Creates a mock Sentry client whose data collection options are resolved from `dataCollection`, as the real client does.
40+
*/
41+
export function createMockClientWithDataCollection(dataCollection: DataCollection): Client {
42+
return {
43+
getOptions: () => ({ traceLifecycle: 'static', dataCollection }),
44+
getDataCollectionOptions: () => resolveDataCollectionOptions({ dataCollection }),
45+
getDsn: () => ({ publicKey: 'test-key', host: 'test-host' }),
46+
emit: vi.fn(),
47+
} as unknown as Client;
48+
}
49+
3650
/**
3751
* Create a mock MCP server instance for testing (legacy API: tool/resource/prompt)
3852
*/

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

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,39 +1123,6 @@ describe('MCP Server Transport Instrumentation', () => {
11231123
);
11241124
});
11251125

1126-
it('should not gate MCP inputs on dataCollection.genAI', async () => {
1127-
const client = createMockClient(true);
1128-
const dataCollection = client.getDataCollectionOptions();
1129-
getClientSpy.mockReturnValue({
1130-
...client,
1131-
getDataCollectionOptions: () => ({ ...dataCollection, genAI: { inputs: false, outputs: false } }),
1132-
} as typeof client);
1133-
1134-
const mockMcpServer = createMockMcpServer();
1135-
const wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
1136-
const transport = createMockTransport();
1137-
1138-
await wrappedMcpServer.connect(transport);
1139-
1140-
transport.onmessage?.(
1141-
{
1142-
jsonrpc: '2.0',
1143-
method: 'tools/call',
1144-
id: 'tool-1',
1145-
params: { name: 'weather', arguments: { location: 'London' } },
1146-
},
1147-
{},
1148-
);
1149-
1150-
expect(startInactiveSpanSpy).toHaveBeenCalledWith(
1151-
expect.objectContaining({
1152-
attributes: expect.objectContaining({
1153-
'mcp.request.argument.location': '"London"',
1154-
}),
1155-
}),
1156-
);
1157-
});
1158-
11591126
it('should capture inputs/outputs when dataCollection.mcp.inputs/outputs are true', async () => {
11601127
getClientSpy.mockReturnValue(createMockClient(true));
11611128

0 commit comments

Comments
 (0)