-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Associate resource/tool/prompt invocations with request span instead of response span #16126
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
feat(core): Associate resource/tool/prompt invocations with request span instead of response span #16126
Changes from 3 commits
Commits
Show all changes
4 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
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
64 changes: 64 additions & 0 deletions
64
dev-packages/e2e-tests/test-applications/node-express/src/mcp.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,64 @@ | ||
import express from 'express'; | ||
import { McpServer, ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js'; | ||
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js'; | ||
import { z } from 'zod'; | ||
import { wrapMcpServerWithSentry } from '@sentry/core'; | ||
|
||
const mcpRouter = express.Router(); | ||
|
||
const server = wrapMcpServerWithSentry( | ||
new McpServer({ | ||
name: 'Echo', | ||
version: '1.0.0', | ||
}), | ||
); | ||
|
||
server.resource('echo', new ResourceTemplate('echo://{message}', { list: undefined }), async (uri, { message }) => ({ | ||
contents: [ | ||
{ | ||
uri: uri.href, | ||
text: `Resource echo: ${message}`, | ||
}, | ||
], | ||
})); | ||
|
||
server.tool('echo', { message: z.string() }, async ({ message }, rest) => { | ||
return { | ||
content: [{ type: 'text', text: `Tool echo: ${message}` }], | ||
}; | ||
}); | ||
|
||
server.prompt('echo', { message: z.string() }, ({ message }, extra) => ({ | ||
messages: [ | ||
{ | ||
role: 'user', | ||
content: { | ||
type: 'text', | ||
text: `Please process this message: ${message}`, | ||
}, | ||
}, | ||
], | ||
})); | ||
|
||
const transports: Record<string, SSEServerTransport> = {}; | ||
|
||
mcpRouter.get('/sse', async (_, res) => { | ||
const transport = new SSEServerTransport('/messages', res); | ||
transports[transport.sessionId] = transport; | ||
res.on('close', () => { | ||
delete transports[transport.sessionId]; | ||
}); | ||
await server.connect(transport); | ||
}); | ||
|
||
mcpRouter.post('/messages', async (req, res) => { | ||
const sessionId = req.query.sessionId; | ||
const transport = transports[sessionId as string]; | ||
if (transport) { | ||
await transport.handlePostMessage(req, res); | ||
} else { | ||
res.status(400).send('No transport found for sessionId'); | ||
} | ||
}); | ||
|
||
export { mcpRouter }; |
109 changes: 109 additions & 0 deletions
109
dev-packages/e2e-tests/test-applications/node-express/tests/mcp.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,109 @@ | ||
import { expect, test } from '@playwright/test'; | ||
import { waitForTransaction } from '@sentry-internal/test-utils'; | ||
import { Client } from '@modelcontextprotocol/sdk/client/index.js'; | ||
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'; | ||
|
||
test.only('Should record transactions for mcp handlers', async ({ baseURL }) => { | ||
const transport = new SSEClientTransport(new URL(`${baseURL}/sse`)); | ||
|
||
const client = new Client({ | ||
name: 'test-client', | ||
version: '1.0.0', | ||
}); | ||
|
||
await client.connect(transport); | ||
|
||
await test.step('tool handler', async () => { | ||
const postTransactionPromise = waitForTransaction('node-express', transactionEvent => { | ||
return transactionEvent.transaction === 'POST /messages'; | ||
}); | ||
const toolTransactionPromise = waitForTransaction('node-express', transactionEvent => { | ||
return transactionEvent.transaction === 'mcp-server/tool:echo'; | ||
}); | ||
|
||
const toolResult = await client.callTool({ | ||
name: 'echo', | ||
arguments: { | ||
message: 'foobar', | ||
}, | ||
}); | ||
|
||
expect(toolResult).toMatchObject({ | ||
content: [ | ||
{ | ||
text: 'Tool echo: foobar', | ||
type: 'text', | ||
}, | ||
], | ||
}); | ||
|
||
const postTransaction = await postTransactionPromise; | ||
expect(postTransaction).toBeDefined(); | ||
|
||
const toolTransaction = await toolTransactionPromise; | ||
expect(toolTransaction).toBeDefined(); | ||
|
||
// 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 | ||
}); | ||
|
||
await test.step('resource handler', async () => { | ||
const postTransactionPromise = waitForTransaction('node-express', transactionEvent => { | ||
return transactionEvent.transaction === 'POST /messages'; | ||
}); | ||
const resourceTransactionPromise = waitForTransaction('node-express', transactionEvent => { | ||
return transactionEvent.transaction === 'mcp-server/resource:echo'; | ||
}); | ||
|
||
const resourceResult = await client.readResource({ | ||
uri: 'echo://foobar', | ||
}); | ||
|
||
expect(resourceResult).toMatchObject({ | ||
contents: [{ text: 'Resource echo: foobar', uri: 'echo://foobar' }], | ||
}); | ||
|
||
const postTransaction = await postTransactionPromise; | ||
expect(postTransaction).toBeDefined(); | ||
|
||
const resourceTransaction = await resourceTransactionPromise; | ||
expect(resourceTransaction).toBeDefined(); | ||
|
||
// 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 | ||
}); | ||
|
||
await test.step('prompt handler', async () => { | ||
const postTransactionPromise = waitForTransaction('node-express', transactionEvent => { | ||
return transactionEvent.transaction === 'POST /messages'; | ||
}); | ||
const promptTransactionPromise = waitForTransaction('node-express', transactionEvent => { | ||
return transactionEvent.transaction === 'mcp-server/prompt:echo'; | ||
}); | ||
|
||
const promptResult = await client.getPrompt({ | ||
name: 'echo', | ||
arguments: { | ||
message: 'foobar', | ||
}, | ||
}); | ||
|
||
expect(promptResult).toMatchObject({ | ||
messages: [ | ||
{ | ||
content: { | ||
text: 'Please process this message: foobar', | ||
type: 'text', | ||
}, | ||
role: 'user', | ||
}, | ||
], | ||
}); | ||
|
||
const postTransaction = await postTransactionPromise; | ||
expect(postTransaction).toBeDefined(); | ||
|
||
const promptTransaction = await promptTransactionPromise; | ||
expect(promptTransaction).toBeDefined(); | ||
|
||
// 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 | ||
}); | ||
}); |
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.