|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForRequest } from '@sentry-internal/test-utils'; |
| 3 | + |
| 4 | +test('sends spans for MCP tool calls via MCPAgent (DurableObject)', async ({ baseURL }) => { |
| 5 | + const mcpToolWaiter = waitForRequest('cloudflare-mcp-agent', event => { |
| 6 | + const transaction = event.envelope[1][0][1]; |
| 7 | + return ( |
| 8 | + typeof transaction !== 'string' && |
| 9 | + 'transaction' in transaction && |
| 10 | + transaction.transaction === 'tools/call my-tool' |
| 11 | + ); |
| 12 | + }); |
| 13 | + |
| 14 | + // Step 1: Initialize the MCP session |
| 15 | + const initResponse = await fetch(`${baseURL}/mcp`, { |
| 16 | + method: 'POST', |
| 17 | + headers: { |
| 18 | + 'Content-Type': 'application/json', |
| 19 | + Accept: 'application/json, text/event-stream', |
| 20 | + }, |
| 21 | + body: JSON.stringify({ |
| 22 | + jsonrpc: '2.0', |
| 23 | + id: 0, |
| 24 | + method: 'initialize', |
| 25 | + params: { |
| 26 | + protocolVersion: '2024-11-05', |
| 27 | + capabilities: {}, |
| 28 | + clientInfo: { |
| 29 | + name: 'test-client', |
| 30 | + version: '1.0.0', |
| 31 | + }, |
| 32 | + }, |
| 33 | + }), |
| 34 | + }); |
| 35 | + |
| 36 | + expect(initResponse.status).toBe(200); |
| 37 | + const sessionId = initResponse.headers.get('Mcp-Session-Id'); |
| 38 | + expect(sessionId).toBeTruthy(); |
| 39 | + |
| 40 | + // Step 2: Send initialized notification |
| 41 | + await fetch(`${baseURL}/mcp`, { |
| 42 | + method: 'POST', |
| 43 | + headers: { |
| 44 | + 'Content-Type': 'application/json', |
| 45 | + Accept: 'application/json, text/event-stream', |
| 46 | + 'Mcp-Session-Id': sessionId!, |
| 47 | + }, |
| 48 | + body: JSON.stringify({ |
| 49 | + jsonrpc: '2.0', |
| 50 | + method: 'notifications/initialized', |
| 51 | + }), |
| 52 | + }); |
| 53 | + |
| 54 | + // Step 3: Call the tool with the session ID |
| 55 | + const response = await fetch(`${baseURL}/mcp`, { |
| 56 | + method: 'POST', |
| 57 | + headers: { |
| 58 | + 'Content-Type': 'application/json', |
| 59 | + Accept: 'application/json, text/event-stream', |
| 60 | + 'Mcp-Session-Id': sessionId!, |
| 61 | + }, |
| 62 | + body: JSON.stringify({ |
| 63 | + jsonrpc: '2.0', |
| 64 | + id: 1, |
| 65 | + method: 'tools/call', |
| 66 | + params: { |
| 67 | + name: 'my-tool', |
| 68 | + arguments: { |
| 69 | + message: 'hello from MCPAgent test', |
| 70 | + }, |
| 71 | + }, |
| 72 | + }), |
| 73 | + }); |
| 74 | + |
| 75 | + expect(response.status).toBe(200); |
| 76 | + |
| 77 | + const mcpData = await mcpToolWaiter; |
| 78 | + const mcpEvent = mcpData.envelope[1][0][1]; |
| 79 | + |
| 80 | + expect(mcpEvent.contexts?.trace?.trace_id).toBe(mcpData.envelope[0].trace.trace_id); |
| 81 | + expect(mcpEvent.contexts?.trace).toEqual({ |
| 82 | + trace_id: expect.any(String), |
| 83 | + parent_span_id: expect.any(String), |
| 84 | + span_id: expect.any(String), |
| 85 | + op: 'mcp.server', |
| 86 | + origin: 'auto.function.mcp_server', |
| 87 | + data: expect.objectContaining({ |
| 88 | + 'sentry.origin': 'auto.function.mcp_server', |
| 89 | + 'sentry.op': 'mcp.server', |
| 90 | + 'mcp.method.name': 'tools/call', |
| 91 | + 'mcp.tool.name': 'my-tool', |
| 92 | + 'mcp.tool.extra': 'from-mcpagent', |
| 93 | + 'mcp.tool.input': '{"message":"hello from MCPAgent test"}', |
| 94 | + }), |
| 95 | + }); |
| 96 | +}); |
0 commit comments