Skip to content

feat(v9/core): add MCP server instrumentation #17196

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

Merged
merged 1 commit into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { mcpRouter } from './mcp';
const app = express();
const port = 3030;

app.use(express.json());

app.use(mcpRouter);

app.get('/crash-in-with-monitor/:id', async (req, res) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mcpRouter.post('/messages', async (req, res) => {
const sessionId = req.query.sessionId;
const transport = transports[sessionId as string];
if (transport) {
await transport.handlePostMessage(req, res);
await transport.handlePostMessage(req, res, req.body);
} else {
res.status(400).send('No transport found for sessionId');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('Sends correct error event', async ({ baseURL }) => {
expect(errorEvent.exception?.values).toHaveLength(1);
expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception with id 123');

expect(errorEvent.request).toEqual({
expect(errorEvent.request).toMatchObject({
method: 'GET',
cookies: {},
headers: expect.any(Object),
Expand Down Expand Up @@ -43,11 +43,11 @@ test('Should record caught exceptions with local variable', async ({ baseURL })

test('To not crash app from withMonitor', async ({ baseURL }) => {
const doRequest = async (id: number) => {
const response = await fetch(`${baseURL}/crash-in-with-monitor/${id}`)
const response = await fetch(`${baseURL}/crash-in-with-monitor/${id}`);
return response.json();
}
const [response1, response2] = await Promise.all([doRequest(1), doRequest(2)])
expect(response1.message).toBe('This is an exception withMonitor: 1')
expect(response2.message).toBe('This is an exception withMonitor: 2')
expect(response1.pid).toBe(response2.pid) //Just to double-check, TBS
};
const [response1, response2] = await Promise.all([doRequest(1), doRequest(2)]);
expect(response1.message).toBe('This is an exception withMonitor: 1');
expect(response2.message).toBe('This is an exception withMonitor: 2');
expect(response1.pid).toBe(response2.pid); //Just to double-check, TBS
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('Should record transactions for mcp handlers', async ({ baseURL }) => {
return transactionEvent.transaction === 'POST /messages';
});
const toolTransactionPromise = waitForTransaction('node-express', transactionEvent => {
return transactionEvent.transaction === 'mcp-server/tool:echo';
return transactionEvent.transaction === 'tools/call echo';
});

const toolResult = await client.callTool({
Expand All @@ -39,10 +39,12 @@ test('Should record transactions for mcp handlers', async ({ baseURL }) => {

const postTransaction = await postTransactionPromise;
expect(postTransaction).toBeDefined();
expect(postTransaction.contexts?.trace?.op).toEqual('http.server');

const toolTransaction = await toolTransactionPromise;
expect(toolTransaction).toBeDefined();

expect(toolTransaction.contexts?.trace?.op).toEqual('mcp.server');
expect(toolTransaction.contexts?.trace?.data?.['mcp.method.name']).toEqual('tools/call');
// TODO: When https://github.com/modelcontextprotocol/typescript-sdk/pull/358 is released check for trace id equality between the post transaction and the handler transaction
});

Expand All @@ -51,7 +53,7 @@ test('Should record transactions for mcp handlers', async ({ baseURL }) => {
return transactionEvent.transaction === 'POST /messages';
});
const resourceTransactionPromise = waitForTransaction('node-express', transactionEvent => {
return transactionEvent.transaction === 'mcp-server/resource:echo';
return transactionEvent.transaction === 'resources/read echo://foobar';
});

const resourceResult = await client.readResource({
Expand All @@ -64,10 +66,12 @@ test('Should record transactions for mcp handlers', async ({ baseURL }) => {

const postTransaction = await postTransactionPromise;
expect(postTransaction).toBeDefined();
expect(postTransaction.contexts?.trace?.op).toEqual('http.server');

const resourceTransaction = await resourceTransactionPromise;
expect(resourceTransaction).toBeDefined();

expect(resourceTransaction.contexts?.trace?.op).toEqual('mcp.server');
expect(resourceTransaction.contexts?.trace?.data?.['mcp.method.name']).toEqual('resources/read');
// TODO: When https://github.com/modelcontextprotocol/typescript-sdk/pull/358 is released check for trace id equality between the post transaction and the handler transaction
});

Expand All @@ -76,7 +80,7 @@ test('Should record transactions for mcp handlers', async ({ baseURL }) => {
return transactionEvent.transaction === 'POST /messages';
});
const promptTransactionPromise = waitForTransaction('node-express', transactionEvent => {
return transactionEvent.transaction === 'mcp-server/prompt:echo';
return transactionEvent.transaction === 'prompts/get echo';
});

const promptResult = await client.getPrompt({
Expand All @@ -100,10 +104,12 @@ test('Should record transactions for mcp handlers', async ({ baseURL }) => {

const postTransaction = await postTransactionPromise;
expect(postTransaction).toBeDefined();
expect(postTransaction.contexts?.trace?.op).toEqual('http.server');

const promptTransaction = await promptTransactionPromise;
expect(promptTransaction).toBeDefined();

expect(promptTransaction.contexts?.trace?.op).toEqual('mcp.server');
expect(promptTransaction.contexts?.trace?.data?.['mcp.method.name']).toEqual('prompts/get');
// TODO: When https://github.com/modelcontextprotocol/typescript-sdk/pull/358 is released check for trace id equality between the post transaction and the handler transaction
});
});
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export { featureFlagsIntegration, type FeatureFlagsIntegration } from './integra
export { profiler } from './profiling';
export { instrumentFetchRequest } from './fetch';
export { trpcMiddleware } from './trpc';
export { wrapMcpServerWithSentry } from './mcp-server';
export { wrapMcpServerWithSentry } from './integrations/mcp-server';
export { captureFeedback } from './feedback';
export type { ReportDialogOptions } from './report-dialog';
export { _INTERNAL_captureLog, _INTERNAL_flushLogsBuffer, _INTERNAL_captureSerializedLog } from './logs/exports';
Expand Down
Loading
Loading