Repository navigation
feat(core): capture MCP OAuth client names per request #24508
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
Open
betegon
wants to merge
3
commits into
develop
Choose a base branch
from
feat/mcp-oauth-client-name
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
8 changes: 8 additions & 0 deletions
8
dev-packages/node-integration-tests/suites/tracing/mcp-server-oauth-client/instrument.mjs
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,8 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| tracesSampleRate: 1, | ||
| transport: loggingTransport, | ||
| }); |
77 changes: 77 additions & 0 deletions
77
dev-packages/node-integration-tests/suites/tracing/mcp-server-oauth-client/scenario-v1.mjs
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,77 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { AsyncLocalStorage } from 'node:async_hooks'; | ||
| import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; | ||
| import { WebStandardStreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js'; | ||
| import { startSpan, wrapMcpServerWithSentry } from '@sentry/node'; | ||
|
|
||
| const requestContext = new AsyncLocalStorage(); | ||
| let resolverCalls = 0; | ||
| const server = wrapMcpServerWithSentry(new McpServer({ name: 'test-server', version: '1.0.0' }), { | ||
| getOAuthClientName(authInfo) { | ||
| resolverCalls++; | ||
| return authInfo?.extra?.clientName ?? requestContext.getStore(); | ||
| }, | ||
| }); | ||
| server.registerTool('echo', {}, async () => ({ content: [{ type: 'text', text: 'ok' }] })); | ||
| const transport = new WebStandardStreamableHTTPServerTransport({ | ||
| sessionIdGenerator: () => 'test-session', | ||
| enableJsonResponse: true, | ||
| }); | ||
|
|
||
| async function request(id, method, clientName, contextName) { | ||
| const params = | ||
| method === 'initialize' | ||
| ? { protocolVersion: '2025-11-25', capabilities: {}, clientInfo: { name: 'protocol-v1', version: '1.0.0' } } | ||
| : {}; | ||
| const response = await requestContext.run(contextName, async () => { | ||
| await new Promise(resolve => setImmediate(resolve)); | ||
| return transport.handleRequest( | ||
| new Request('https://example.test/mcp', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Accept: 'application/json, text/event-stream', | ||
| 'MCP-Protocol-Version': '2025-11-25', | ||
| ...(method !== 'initialize' && { 'Mcp-Session-Id': 'test-session' }), | ||
| }, | ||
| body: JSON.stringify({ jsonrpc: '2.0', id, method, params }), | ||
| }), | ||
| { | ||
| authInfo: clientName | ||
| ? { clientId: id, token: 'test-token', scopes: ['test-scope'], extra: { clientName } } | ||
| : undefined, | ||
| }, | ||
| ); | ||
| }); | ||
| if (id === undefined) { | ||
| assert.equal(response.status, 202); | ||
| return; | ||
| } | ||
| assert.equal(response.status, 200); | ||
| const body = await response.json(); | ||
| assert.equal(body.id, id); | ||
| if (method === 'resources/list') { | ||
| assert.equal(body.error.code, -32601); | ||
| } else { | ||
| assert.equal(body.error, undefined); | ||
| if (method === 'tools/list') { | ||
| assert.equal(body.result.tools[0].name, 'echo'); | ||
| } else { | ||
| assert.equal(body.result.protocolVersion, '2025-11-25'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| startSpan({ name: 'oauth requests' }, async span => { | ||
| await server.connect(transport); | ||
| await request('discovery', 'initialize', 'Registered Discovery'); | ||
| await request(undefined, 'notifications/initialized', 'Notification Client'); | ||
| await Promise.all([ | ||
| request('context', 'tools/list', undefined, 'Registered Context'), | ||
| request('unsupported', 'resources/list', 'Registered Unsupported'), | ||
| request('missing', 'tools/list'), | ||
| ]); | ||
| assert.equal(resolverCalls, 4); | ||
| await server.close(); | ||
| span.setAttribute('test.responses.checked', true); | ||
| }); |
75 changes: 75 additions & 0 deletions
75
dev-packages/node-integration-tests/suites/tracing/mcp-server-oauth-client/scenario-v2.mjs
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,75 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { AsyncLocalStorage } from 'node:async_hooks'; | ||
| import { createMcpHandler, McpServer } from '@modelcontextprotocol/server'; | ||
| import { startSpan, wrapMcpServerWithSentry } from '@sentry/node'; | ||
|
|
||
| const requestContext = new AsyncLocalStorage(); | ||
| let resolverCalls = 0; | ||
| const handler = createMcpHandler(async () => { | ||
| await new Promise(resolve => setImmediate(resolve)); | ||
| const server = wrapMcpServerWithSentry(new McpServer({ name: 'test-server', version: '1.0.0' }), { | ||
| getOAuthClientName(authInfo) { | ||
| resolverCalls++; | ||
| return authInfo?.extra?.clientName ?? requestContext.getStore(); | ||
| }, | ||
| }); | ||
| server.registerTool('echo', {}, async () => ({ content: [{ type: 'text', text: 'ok' }] })); | ||
| return server; | ||
| }); | ||
|
|
||
| async function request(id, method, clientName, contextName) { | ||
| const params = { | ||
| _meta: { | ||
| 'io.modelcontextprotocol/protocolVersion': '2026-07-28', | ||
| 'io.modelcontextprotocol/clientCapabilities': {}, | ||
| ...(id !== 'missing' && { | ||
| 'io.modelcontextprotocol/clientInfo': { name: `protocol-${id}`, version: '1.0.0' }, | ||
| }), | ||
| }, | ||
| }; | ||
| const response = await requestContext.run(contextName, () => | ||
| handler.fetch( | ||
| new Request('https://example.test/mcp', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Accept: 'application/json, text/event-stream', | ||
| 'MCP-Protocol-Version': '2026-07-28', | ||
| 'MCP-Method': method, | ||
| }, | ||
| body: JSON.stringify({ jsonrpc: '2.0', id, method, params }), | ||
| }), | ||
| { | ||
| authInfo: clientName | ||
| ? { clientId: id, token: 'test-token', scopes: ['test-scope'], extra: { clientName } } | ||
| : undefined, | ||
| }, | ||
| ), | ||
| ); | ||
| const body = await response.json(); | ||
| assert.equal(body.id, id); | ||
| if (method === 'resources/list') { | ||
| assert.equal(response.status, 404); | ||
| assert.equal(body.error.code, -32601); | ||
| } else { | ||
| assert.equal(response.status, 200); | ||
| assert.equal(body.error, undefined); | ||
| if (method === 'tools/list') { | ||
| assert.equal(body.result.tools[0].name, 'echo'); | ||
| } else { | ||
| assert.ok(body.result.supportedVersions.includes('2026-07-28')); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| startSpan({ name: 'oauth requests' }, async span => { | ||
| await Promise.all([ | ||
| request('discovery', 'server/discover', 'Registered Discovery'), | ||
| request('context', 'tools/list', undefined, 'Registered Context'), | ||
| request('unsupported', 'resources/list', 'Registered Unsupported'), | ||
| request('missing', 'tools/list'), | ||
| ]); | ||
| assert.equal(resolverCalls, 4); | ||
| await handler.close(); | ||
| span.setAttribute('test.responses.checked', true); | ||
| }); |
79 changes: 79 additions & 0 deletions
79
dev-packages/node-integration-tests/suites/tracing/mcp-server-oauth-client/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,79 @@ | ||
| import { | ||
| MCP_AUTH_CLIENT_NAME, | ||
| MCP_CLIENT_NAME, | ||
| MCP_METHOD_NAME, | ||
| MCP_REQUEST_ID, | ||
| SENTRY_OP, | ||
| } from '@sentry/conventions/attributes'; | ||
| import { MCP_SERVER } from '@sentry/conventions/op'; | ||
| import { afterAll, describe, expect } from 'vitest'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; | ||
|
|
||
| describe('MCP OAuth client attribution', () => { | ||
| afterAll(() => { | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| for (const { version, expectedRequests, additionalDependencies } of [ | ||
| { | ||
| version: 'v1', | ||
| expectedRequests: [ | ||
| ['discovery', 'initialize', 'Registered Discovery', 'protocol-v1'], | ||
| ['context', 'tools/list', 'Registered Context', 'protocol-v1'], | ||
| ['unsupported', 'resources/list', 'Registered Unsupported', 'protocol-v1'], | ||
| ['missing', 'tools/list', undefined, 'protocol-v1'], | ||
| ], | ||
| additionalDependencies: { '@modelcontextprotocol/sdk': '1.30.0' }, | ||
| }, | ||
| { | ||
| version: 'v2', | ||
| expectedRequests: [ | ||
| ['discovery', 'server/discover', 'Registered Discovery', 'protocol-discovery'], | ||
| ['context', 'tools/list', 'Registered Context', 'protocol-context'], | ||
| ['unsupported', 'resources/list', 'Registered Unsupported', 'protocol-unsupported'], | ||
| ['missing', 'tools/list', undefined, undefined], | ||
| ], | ||
| }, | ||
| ]) { | ||
| createEsmAndCjsTests( | ||
| __dirname, | ||
| `scenario-${version}.mjs`, | ||
| 'instrument.mjs', | ||
| (createTestRunner, test) => { | ||
| test(`keeps ${version} OAuth identity request-local and separate from MCP identity`, async () => { | ||
| const runner = createTestRunner(); | ||
| const spansPromise = runner.collectStreamedSpans( | ||
| spans => | ||
| spans.some(span => span.is_segment && span.name === 'oauth requests') && | ||
| expectedRequests.every(([id]) => | ||
| spans.some( | ||
| span => | ||
| span.attributes[SENTRY_OP]?.value === MCP_SERVER && span.attributes[MCP_REQUEST_ID]?.value === id, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| await runner.start().completed(); | ||
|
|
||
| const spans = await spansPromise; | ||
| const root = spans.find(span => span.is_segment && span.name === 'oauth requests'); | ||
| expect(root?.attributes['test.responses.checked']?.value).toBe(true); | ||
| const requests = spans.filter( | ||
| span => span.attributes[SENTRY_OP]?.value === MCP_SERVER && span.attributes[MCP_REQUEST_ID], | ||
| ); | ||
| expect(requests).toHaveLength(expectedRequests.length); | ||
| expect( | ||
| requests.map(({ attributes }) => [ | ||
| attributes[MCP_REQUEST_ID]?.value, | ||
| attributes[MCP_METHOD_NAME]?.value, | ||
| attributes[MCP_AUTH_CLIENT_NAME]?.value, | ||
| attributes[MCP_CLIENT_NAME]?.value, | ||
| ]), | ||
| ).toEqual(expect.arrayContaining(expectedRequests)); | ||
| expect(JSON.stringify(spans)).not.toContain('Notification Client'); | ||
| }); | ||
| }, | ||
| { additionalDependencies }, | ||
| ); | ||
| } | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this diff is leaving to little information.