|
1 | | -import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
2 | | -import { getCurrentScope, getDynamicSamplingContextFromSpan, setCurrentClient, startSpan } from '../../../../src'; |
3 | | -import { wrapTransportOnMessage } from '../../../../src/integrations/mcp-server/transport'; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { |
| 3 | + getClient, |
| 4 | + getCurrentScope, |
| 5 | + getDynamicSamplingContextFromSpan, |
| 6 | + setCurrentClient, |
| 7 | + startSpan, |
| 8 | +} from '../../../../src'; |
| 9 | +import { wrapTransportOnMessage, wrapTransportSend } from '../../../../src/integrations/mcp-server/transport'; |
4 | 10 | import type { Span } from '../../../../src/types/span'; |
5 | 11 | import { getActiveSpan, spanToJSON } from '../../../../src/utils/spanUtils'; |
6 | 12 | import { getDefaultTestClientOptions, TestClient } from '../../../mocks/client'; |
@@ -112,3 +118,125 @@ describe('MCP server trace propagation via params._meta', () => { |
112 | 118 | expect(json.links).toBeUndefined(); |
113 | 119 | }); |
114 | 120 | }); |
| 121 | + |
| 122 | +describe('MCP server trace injection into outgoing params._meta', () => { |
| 123 | + beforeEach(() => { |
| 124 | + const client = new TestClient( |
| 125 | + getDefaultTestClientOptions({ dsn: 'https://public@dsn.ingest.sentry.io/1337', tracesSampleRate: 1 }), |
| 126 | + ); |
| 127 | + setCurrentClient(client); |
| 128 | + client.init(); |
| 129 | + }); |
| 130 | + |
| 131 | + afterEach(() => { |
| 132 | + getCurrentScope().setClient(undefined); |
| 133 | + }); |
| 134 | + |
| 135 | + function wrappedSend() { |
| 136 | + const transport = createMockStdioTransport(); |
| 137 | + const originalSend = vi.fn().mockResolvedValue(undefined); |
| 138 | + transport.send = originalSend; |
| 139 | + wrapTransportSend(transport, {}); |
| 140 | + return { send: transport.send, originalSend }; |
| 141 | + } |
| 142 | + |
| 143 | + it('injects the active span into outgoing requests', async () => { |
| 144 | + const { send, originalSend } = wrappedSend(); |
| 145 | + const request = { jsonrpc: '2.0', method: 'sampling/createMessage', id: 7, params: { maxTokens: 10 } }; |
| 146 | + |
| 147 | + let toolSpan: Span | undefined; |
| 148 | + await startSpan({ name: 'tools/call get-weather' }, async span => { |
| 149 | + toolSpan = span; |
| 150 | + await send(request, { relatedRequestId: 1 }); |
| 151 | + }); |
| 152 | + |
| 153 | + const { traceId, spanId } = toolSpan!.spanContext(); |
| 154 | + expect(originalSend).toHaveBeenCalledWith( |
| 155 | + { |
| 156 | + ...request, |
| 157 | + params: { |
| 158 | + maxTokens: 10, |
| 159 | + _meta: { |
| 160 | + 'sentry-trace': `${traceId}-${spanId}-1`, |
| 161 | + traceparent: `00-${traceId}-${spanId}-01`, |
| 162 | + baggage: expect.stringContaining(`sentry-trace_id=${traceId}`), |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + { relatedRequestId: 1 }, |
| 167 | + ); |
| 168 | + expect(request.params).toEqual({ maxTokens: 10 }); |
| 169 | + }); |
| 170 | + |
| 171 | + it('parents outgoing notifications to their own notification span', async () => { |
| 172 | + const { send, originalSend } = wrappedSend(); |
| 173 | + let notificationSpan: Span | undefined; |
| 174 | + getClient()!.on('spanStart', span => { |
| 175 | + if (spanToJSON(span).name === 'notifications/progress') { |
| 176 | + notificationSpan = span; |
| 177 | + } |
| 178 | + }); |
| 179 | + |
| 180 | + await startSpan({ name: 'tools/call get-weather' }, () => |
| 181 | + send({ jsonrpc: '2.0', method: 'notifications/progress', params: { progressToken: 1, progress: 50 } }), |
| 182 | + ); |
| 183 | + |
| 184 | + const { traceId, spanId } = notificationSpan!.spanContext(); |
| 185 | + expect(originalSend).toHaveBeenCalledWith({ |
| 186 | + jsonrpc: '2.0', |
| 187 | + method: 'notifications/progress', |
| 188 | + params: { |
| 189 | + progressToken: 1, |
| 190 | + progress: 50, |
| 191 | + _meta: expect.objectContaining({ |
| 192 | + 'sentry-trace': `${traceId}-${spanId}-1`, |
| 193 | + traceparent: `00-${traceId}-${spanId}-01`, |
| 194 | + }), |
| 195 | + }, |
| 196 | + }); |
| 197 | + }); |
| 198 | + |
| 199 | + it('keeps existing _meta entries and adds params when there are none', async () => { |
| 200 | + const { send, originalSend } = wrappedSend(); |
| 201 | + |
| 202 | + await startSpan({ name: 'tools/call get-weather' }, async () => { |
| 203 | + await send({ jsonrpc: '2.0', method: 'ping', id: 1 }); |
| 204 | + await send({ jsonrpc: '2.0', method: 'roots/list', id: 2, params: { _meta: { 'com.example/key': 'value' } } }); |
| 205 | + }); |
| 206 | + |
| 207 | + expect(originalSend).toHaveBeenCalledWith({ |
| 208 | + jsonrpc: '2.0', |
| 209 | + method: 'ping', |
| 210 | + id: 1, |
| 211 | + params: { _meta: expect.objectContaining({ 'sentry-trace': expect.any(String) }) }, |
| 212 | + }); |
| 213 | + expect(originalSend).toHaveBeenCalledWith({ |
| 214 | + jsonrpc: '2.0', |
| 215 | + method: 'roots/list', |
| 216 | + id: 2, |
| 217 | + params: { _meta: expect.objectContaining({ 'com.example/key': 'value', 'sentry-trace': expect.any(String) }) }, |
| 218 | + }); |
| 219 | + }); |
| 220 | + |
| 221 | + it.each([ |
| 222 | + ['a traceparent', { traceparent: `00-${TRACE_ID}-${PARENT_SPAN_ID}-01` }], |
| 223 | + ['a sentry-trace', { 'sentry-trace': `${TRACE_ID}-${PARENT_SPAN_ID}-1` }], |
| 224 | + ['a non-object _meta', 'not-an-object'], |
| 225 | + ])('leaves requests that already carry %s unchanged', async (_, meta) => { |
| 226 | + const { send, originalSend } = wrappedSend(); |
| 227 | + const request = { jsonrpc: '2.0', method: 'elicitation/create', id: 3, params: { _meta: meta } }; |
| 228 | + |
| 229 | + await startSpan({ name: 'tools/call get-weather' }, () => send(request)); |
| 230 | + |
| 231 | + expect(originalSend).toHaveBeenCalledWith(request); |
| 232 | + }); |
| 233 | + |
| 234 | + it('does not touch responses', async () => { |
| 235 | + const { send, originalSend } = wrappedSend(); |
| 236 | + const response = { jsonrpc: '2.0', id: 1, result: { content: [] } }; |
| 237 | + |
| 238 | + await startSpan({ name: 'tools/call get-weather' }, () => send(response)); |
| 239 | + |
| 240 | + expect(originalSend).toHaveBeenCalledWith(response); |
| 241 | + }); |
| 242 | +}); |
0 commit comments