UNPKG

@sentry/core

Version:
98 lines (97 loc) • 3.61 kB
import { getClient } from "../../currentScopes.js"; import "../../tracing/index.js"; import { withActiveSpan } from "../../tracing/trace.js"; import { isObjectLike } from "../../utils/is.js"; // src/integrations/mcp-server/upstreamInstrumentation.ts /** * Newer `@modelcontextprotocol/server` versions start their own OpenTelemetry span for every inbound * message. When that span reaches Sentry, we add our attributes to it instead of starting a second one. * * The SDK starts that span in `_instrumentation.receive`, which it calls synchronously from * `transport.onmessage`. So our `onmessage` wrapper parks the message's span data in `pendingHandoff` * (see `withUpstreamSpanHandoff`), and our wrapped `receive` picks it up and decides per message: * - The SDK's span reaches Sentry: enrich it. * - It doesn't (a non-Sentry tracer provider, or none at all): start our own span instead. */ let pendingHandoff; /** * Wraps the MCP SDK's package-internal `_instrumentation.receive` and `_instrumentation.send`. * Returns `false` when the server has no such instrumentation (MCP SDK v1, v2 <= 2.4, or `openTelemetry: false`). */ function hookUpstreamInstrumentation(serverInstance) { const inner = serverInstance.server; const protocol = isObjectLike(inner) ? inner : serverInstance; const original = protocol._instrumentation; if (typeof original?.receive !== "function") return false; protocol._instrumentation = { ...original, receive: (operation) => { const handoff = pendingHandoff; if (handoff?.method !== operation.method || handoff.id !== operation.id) return original.receive?.(operation); pendingHandoff = void 0; return receiveWithHandoff(handoff, () => original.receive?.(operation)); }, send: original.send && ((operation) => activateUpstreamSpan(() => original.send?.(operation))) }; return true; } /** Runs the MCP SDK's `onmessage` with `handoff` parked for its `receive` call. */ function withUpstreamSpanHandoff(handoff, onMessage) { pendingHandoff = handoff; try { return onMessage(); } finally { pendingHandoff = void 0; } } function startUpstreamScope(start) { let upstreamSpan; const unsubscribe = getClient()?.on("spanStart", (span) => { upstreamSpan ?? (upstreamSpan = span); }); try { return { scope: start(), upstreamSpan }; } finally { unsubscribe?.(); } } function runInUpstreamSpan(span, scope, fn) { return withActiveSpan(span, () => scope.run(fn)); } function activateUpstreamSpan(send) { const { scope, upstreamSpan } = startUpstreamScope(send); if (!scope || !upstreamSpan) return scope; return { run: (fn) => runInUpstreamSpan(upstreamSpan, scope, fn), end: (outcome) => scope.end(outcome) }; } function receiveWithHandoff(handoff, receive) { const { scope, upstreamSpan } = startUpstreamScope(receive); if (!scope || !upstreamSpan) { const fallbackSpan = handoff.startFallbackSpan(); return { run: (fn) => withActiveSpan(fallbackSpan, () => scope ? scope.run(fn) : fn()), end: (outcome) => { scope?.end(outcome); if (handoff.id === void 0) fallbackSpan.end(); } }; } const span = upstreamSpan; const upstreamScope = scope; span.setAttributes(handoff.attributes); if (handoff.links) span.addLinks(handoff.links); return { run: (fn) => runInUpstreamSpan(span, upstreamScope, fn), end: (outcome) => { if (outcome.type === "result" || outcome.type === "error") handoff.onResponse?.(span, outcome.result, outcome.error); upstreamScope.end(outcome); } }; } export { hookUpstreamInstrumentation, withUpstreamSpanHandoff }; //# sourceMappingURL=upstreamInstrumentation.js.map