|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | +import { Client } from '@modelcontextprotocol/client'; |
| 4 | +import { StreamableHTTPClientTransport } from '@modelcontextprotocol/client'; |
| 5 | + |
| 6 | +test('Should record transactions for MCP handlers using @modelcontextprotocol/sdk v2 (register* API)', async ({ |
| 7 | + baseURL, |
| 8 | +}) => { |
| 9 | + const transport = new StreamableHTTPClientTransport(new URL(`${baseURL}/mcp`)); |
| 10 | + |
| 11 | + const client = new Client({ |
| 12 | + name: 'test-client-v2', |
| 13 | + version: '1.0.0', |
| 14 | + }); |
| 15 | + |
| 16 | + const initializeTransactionPromise = waitForTransaction('node-express-mcp-v2', transactionEvent => { |
| 17 | + return transactionEvent.transaction === 'initialize'; |
| 18 | + }); |
| 19 | + |
| 20 | + await client.connect(transport); |
| 21 | + |
| 22 | + await test.step('initialize handshake', async () => { |
| 23 | + const initializeTransaction = await initializeTransactionPromise; |
| 24 | + expect(initializeTransaction).toBeDefined(); |
| 25 | + expect(initializeTransaction.contexts?.trace?.op).toEqual('mcp.server'); |
| 26 | + expect(initializeTransaction.contexts?.trace?.data?.['mcp.method.name']).toEqual('initialize'); |
| 27 | + expect(initializeTransaction.contexts?.trace?.data?.['mcp.client.name']).toEqual('test-client-v2'); |
| 28 | + expect(initializeTransaction.contexts?.trace?.data?.['mcp.server.name']).toEqual('Echo-V2'); |
| 29 | + expect(initializeTransaction.contexts?.trace?.data?.['mcp.transport']).toMatch(/StreamableHTTPServerTransport/); |
| 30 | + }); |
| 31 | + |
| 32 | + await test.step('registerTool handler', async () => { |
| 33 | + const toolTransactionPromise = waitForTransaction('node-express-mcp-v2', transactionEvent => { |
| 34 | + return transactionEvent.transaction === 'tools/call echo'; |
| 35 | + }); |
| 36 | + |
| 37 | + const toolResult = await client.callTool({ |
| 38 | + name: 'echo', |
| 39 | + arguments: { |
| 40 | + message: 'foobar', |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + expect(toolResult).toMatchObject({ |
| 45 | + content: [ |
| 46 | + { |
| 47 | + text: 'Tool echo: foobar', |
| 48 | + type: 'text', |
| 49 | + }, |
| 50 | + ], |
| 51 | + }); |
| 52 | + |
| 53 | + const toolTransaction = await toolTransactionPromise; |
| 54 | + expect(toolTransaction).toBeDefined(); |
| 55 | + expect(toolTransaction.contexts?.trace?.op).toEqual('mcp.server'); |
| 56 | + expect(toolTransaction.contexts?.trace?.data?.['mcp.method.name']).toEqual('tools/call'); |
| 57 | + expect(toolTransaction.contexts?.trace?.data?.['mcp.tool.name']).toEqual('echo'); |
| 58 | + // Proves span was completed with results (span correlation worked end-to-end) |
| 59 | + expect(toolTransaction.contexts?.trace?.data?.['mcp.tool.result.content_count']).toEqual(1); |
| 60 | + }); |
| 61 | + |
| 62 | + await test.step('registerResource handler', async () => { |
| 63 | + const resourceTransactionPromise = waitForTransaction('node-express-mcp-v2', transactionEvent => { |
| 64 | + return transactionEvent.transaction === 'resources/read echo://foobar'; |
| 65 | + }); |
| 66 | + |
| 67 | + const resourceResult = await client.readResource({ |
| 68 | + uri: 'echo://foobar', |
| 69 | + }); |
| 70 | + |
| 71 | + expect(resourceResult).toMatchObject({ |
| 72 | + contents: [{ text: 'Resource echo: foobar', uri: 'echo://foobar' }], |
| 73 | + }); |
| 74 | + |
| 75 | + const resourceTransaction = await resourceTransactionPromise; |
| 76 | + expect(resourceTransaction).toBeDefined(); |
| 77 | + expect(resourceTransaction.contexts?.trace?.op).toEqual('mcp.server'); |
| 78 | + expect(resourceTransaction.contexts?.trace?.data?.['mcp.method.name']).toEqual('resources/read'); |
| 79 | + }); |
| 80 | + |
| 81 | + await test.step('registerPrompt handler', async () => { |
| 82 | + const promptTransactionPromise = waitForTransaction('node-express-mcp-v2', transactionEvent => { |
| 83 | + return transactionEvent.transaction === 'prompts/get echo'; |
| 84 | + }); |
| 85 | + |
| 86 | + const promptResult = await client.getPrompt({ |
| 87 | + name: 'echo', |
| 88 | + arguments: { |
| 89 | + message: 'foobar', |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + expect(promptResult).toMatchObject({ |
| 94 | + messages: [ |
| 95 | + { |
| 96 | + content: { |
| 97 | + text: 'Please process this message: foobar', |
| 98 | + type: 'text', |
| 99 | + }, |
| 100 | + role: 'user', |
| 101 | + }, |
| 102 | + ], |
| 103 | + }); |
| 104 | + |
| 105 | + const promptTransaction = await promptTransactionPromise; |
| 106 | + expect(promptTransaction).toBeDefined(); |
| 107 | + expect(promptTransaction.contexts?.trace?.op).toEqual('mcp.server'); |
| 108 | + expect(promptTransaction.contexts?.trace?.data?.['mcp.method.name']).toEqual('prompts/get'); |
| 109 | + }); |
| 110 | + |
| 111 | + await test.step('error tool sets span status to internal_error', async () => { |
| 112 | + const toolTransactionPromise = waitForTransaction('node-express-mcp-v2', transactionEvent => { |
| 113 | + return transactionEvent.transaction === 'tools/call always-error'; |
| 114 | + }); |
| 115 | + |
| 116 | + try { |
| 117 | + await client.callTool({ name: 'always-error', arguments: {} }); |
| 118 | + } catch { |
| 119 | + // Expected: MCP SDK throws when the tool returns a JSON-RPC error |
| 120 | + } |
| 121 | + |
| 122 | + const toolTransaction = await toolTransactionPromise; |
| 123 | + expect(toolTransaction).toBeDefined(); |
| 124 | + expect(toolTransaction.contexts?.trace?.op).toEqual('mcp.server'); |
| 125 | + expect(toolTransaction.contexts?.trace?.status).toEqual('internal_error'); |
| 126 | + }); |
| 127 | + |
| 128 | + await client.close(); |
| 129 | +}); |
0 commit comments