Repository navigation
feat(core): Enrich MCP SDK spans instead of duplicating them #26309
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
Draft
sentry-junior
wants to merge
8
commits into
develop
Choose a base branch
from
feat/mcp-sdk-otel-handoff
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+694
−129
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
68eefb3
feat(core): Enrich MCP SDK spans instead of duplicating them
sentry-junior[bot] aa020cc
ref(core): Trim MCP SDK hand-off to stay within the bundle size budget
sentry-junior[bot] 1c4f6a4
test(e2e): Simplify MCP SDK OpenTelemetry variant install
sentry-junior[bot] 9b31a9d
ref(core): Simplify MCP response attribute handling
sentry-junior[bot] 3103b3e
ref(core): Simplify MCP SDK span hand-off
sentry-junior[bot] c95d2af
ref(core): Fold initialize client attributes into the MCP request spa…
sentry-junior[bot] ffad06f
test(core): Use semantic attribute constants in MCP SDK hand-off tests
sentry-junior[bot] c63a8ef
test(node): Explain why the MCP e2e app allows exotic subdeps
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
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
4 changes: 4 additions & 0 deletions
4
dev-packages/e2e-tests/test-applications/node-express-mcp-v2/pnpm-workspace.yaml
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 |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| minimumReleaseAge: 0 | ||
| # The MCP SDK OpenTelemetry variant installs unreleased pkg.pr.new previews, which depend on | ||
| # @modelcontextprotocol/core through a pkg.pr.new tarball URL. Remove this once an npm release of | ||
| # @modelcontextprotocol/server ships the OpenTelemetry instrumentation and the variant pins that version. | ||
| blockExoticSubdeps: false | ||
59 changes: 59 additions & 0 deletions
59
dev-packages/e2e-tests/test-applications/node-express-mcp-v2/tests/mcp-sdk-otel.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,59 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { | ||
| MCP_CLIENT_NAME, | ||
| MCP_METHOD_NAME, | ||
| MCP_PROMPT_NAME, | ||
| MCP_REQUEST_ARGUMENT_KEY_BASE, | ||
| MCP_SERVER_NAME, | ||
| MCP_TOOL_RESULT_CONTENT_COUNT, | ||
| SENTRY_KIND, | ||
| } from '@sentry/conventions/attributes'; | ||
| import { HTTP_SERVER, MCP_SERVER } from '@sentry/conventions/op'; | ||
| import { collectStreamedSpans, getSpanOp, type SerializedStreamedSpan } from '@sentry-internal/test-utils'; | ||
| import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client'; | ||
|
|
||
| test.skip(process.env.MCP_SDK_OTEL !== '1', 'Only runs in the MCP SDK OpenTelemetry variant'); | ||
|
|
||
| /** Resolves with the single MCP span of the request whose span is named `spanName`, once its trace is complete. */ | ||
| async function getMcpSpan(spanName: string): Promise<SerializedStreamedSpan> { | ||
| const spans = await collectStreamedSpans( | ||
| 'node-express-mcp-v2', | ||
| spans => spans.some(span => span.is_segment) && spans.some(span => span.name === spanName), | ||
| ); | ||
| const mcpSpans = spans.filter(span => span.attributes?.[MCP_METHOD_NAME]); | ||
| expect(mcpSpans).toHaveLength(1); | ||
| expect(getSpanOp(spans.find(span => span.is_segment)!)).toBe(HTTP_SERVER); | ||
|
|
||
| const [mcpSpan] = mcpSpans; | ||
| expect(mcpSpan!.name).toBe(spanName); | ||
| expect(mcpSpan!.is_segment).toBe(false); | ||
| expect(mcpSpan!.attributes?.[SENTRY_KIND]?.value).toBe('server'); | ||
| expect(getSpanOp(mcpSpan!)).toBe(MCP_SERVER); | ||
| return mcpSpan!; | ||
| } | ||
|
|
||
| test('enriches the MCP SDK spans instead of duplicating them', async ({ baseURL }) => { | ||
| const client = new Client({ name: 'test-client-v2', version: '1.0.0' }); | ||
|
|
||
| const initializeSpanPromise = getMcpSpan('initialize'); | ||
| await client.connect(new StreamableHTTPClientTransport(new URL(`${baseURL}/mcp`))); | ||
| const initializeSpan = await initializeSpanPromise; | ||
| expect(initializeSpan.attributes?.[MCP_CLIENT_NAME]?.value).toBe('test-client-v2'); | ||
| expect(initializeSpan.attributes?.[MCP_SERVER_NAME]?.value).toBe('Echo-V2'); | ||
|
|
||
| const toolSpanPromise = getMcpSpan('tools/call echo'); | ||
| await client.callTool({ name: 'echo', arguments: { message: 'foobar' } }); | ||
| const toolSpan = await toolSpanPromise; | ||
| expect(toolSpan.attributes?.[`${MCP_REQUEST_ARGUMENT_KEY_BASE}.message`]?.value).toBe('"foobar"'); | ||
| expect(toolSpan.attributes?.[MCP_TOOL_RESULT_CONTENT_COUNT]?.value).toBe(1); | ||
|
|
||
| const promptSpanPromise = getMcpSpan('prompts/get echo'); | ||
| await client.getPrompt({ name: 'echo', arguments: { message: 'foobar' } }); | ||
| expect((await promptSpanPromise).attributes?.[MCP_PROMPT_NAME]?.value).toBe('echo'); | ||
|
|
||
| const errorSpanPromise = getMcpSpan('tools/call always-error'); | ||
| await client.callTool({ name: 'always-error', arguments: {} }).catch(() => undefined); | ||
| expect((await errorSpanPromise).status).toBe('error'); | ||
|
|
||
| await client.close(); | ||
| }); |
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.
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.