|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 4 | +import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'; |
| 5 | + |
| 6 | +test('Should record transactions for mcp handlers', async ({ baseURL }) => { |
| 7 | + const transport = new SSEClientTransport(new URL(`${baseURL}/sse`)); |
| 8 | + |
| 9 | + const client = new Client({ |
| 10 | + name: 'test-client', |
| 11 | + version: '1.0.0', |
| 12 | + }); |
| 13 | + |
| 14 | + await client.connect(transport); |
| 15 | + |
| 16 | + await test.step('tool handler', async () => { |
| 17 | + const postTransactionPromise = waitForTransaction('node-express', transactionEvent => { |
| 18 | + return transactionEvent.transaction === 'POST /messages'; |
| 19 | + }); |
| 20 | + const toolTransactionPromise = waitForTransaction('node-express', transactionEvent => { |
| 21 | + return transactionEvent.transaction === 'mcp-server/tool:echo'; |
| 22 | + }); |
| 23 | + |
| 24 | + const toolResult = await client.callTool({ |
| 25 | + name: 'echo', |
| 26 | + arguments: { |
| 27 | + message: 'foobar', |
| 28 | + }, |
| 29 | + }); |
| 30 | + |
| 31 | + expect(toolResult).toMatchObject({ |
| 32 | + content: [ |
| 33 | + { |
| 34 | + text: 'Tool echo: foobar', |
| 35 | + type: 'text', |
| 36 | + }, |
| 37 | + ], |
| 38 | + }); |
| 39 | + |
| 40 | + const postTransaction = await postTransactionPromise; |
| 41 | + expect(postTransaction).toBeDefined(); |
| 42 | + |
| 43 | + const toolTransaction = await toolTransactionPromise; |
| 44 | + expect(toolTransaction).toBeDefined(); |
| 45 | + |
| 46 | + // 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 |
| 47 | + }); |
| 48 | + |
| 49 | + await test.step('resource handler', async () => { |
| 50 | + const postTransactionPromise = waitForTransaction('node-express', transactionEvent => { |
| 51 | + return transactionEvent.transaction === 'POST /messages'; |
| 52 | + }); |
| 53 | + const resourceTransactionPromise = waitForTransaction('node-express', transactionEvent => { |
| 54 | + return transactionEvent.transaction === 'mcp-server/resource:echo'; |
| 55 | + }); |
| 56 | + |
| 57 | + const resourceResult = await client.readResource({ |
| 58 | + uri: 'echo://foobar', |
| 59 | + }); |
| 60 | + |
| 61 | + expect(resourceResult).toMatchObject({ |
| 62 | + contents: [{ text: 'Resource echo: foobar', uri: 'echo://foobar' }], |
| 63 | + }); |
| 64 | + |
| 65 | + const postTransaction = await postTransactionPromise; |
| 66 | + expect(postTransaction).toBeDefined(); |
| 67 | + |
| 68 | + const resourceTransaction = await resourceTransactionPromise; |
| 69 | + expect(resourceTransaction).toBeDefined(); |
| 70 | + |
| 71 | + // 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 |
| 72 | + }); |
| 73 | + |
| 74 | + await test.step('prompt handler', async () => { |
| 75 | + const postTransactionPromise = waitForTransaction('node-express', transactionEvent => { |
| 76 | + return transactionEvent.transaction === 'POST /messages'; |
| 77 | + }); |
| 78 | + const promptTransactionPromise = waitForTransaction('node-express', transactionEvent => { |
| 79 | + return transactionEvent.transaction === 'mcp-server/prompt:echo'; |
| 80 | + }); |
| 81 | + |
| 82 | + const promptResult = await client.getPrompt({ |
| 83 | + name: 'echo', |
| 84 | + arguments: { |
| 85 | + message: 'foobar', |
| 86 | + }, |
| 87 | + }); |
| 88 | + |
| 89 | + expect(promptResult).toMatchObject({ |
| 90 | + messages: [ |
| 91 | + { |
| 92 | + content: { |
| 93 | + text: 'Please process this message: foobar', |
| 94 | + type: 'text', |
| 95 | + }, |
| 96 | + role: 'user', |
| 97 | + }, |
| 98 | + ], |
| 99 | + }); |
| 100 | + |
| 101 | + const postTransaction = await postTransactionPromise; |
| 102 | + expect(postTransaction).toBeDefined(); |
| 103 | + |
| 104 | + const promptTransaction = await promptTransactionPromise; |
| 105 | + expect(promptTransaction).toBeDefined(); |
| 106 | + |
| 107 | + // 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 |
| 108 | + }); |
| 109 | +}); |
0 commit comments