|
| 1 | +import { DEBUG_BUILD } from './debug-build'; |
| 2 | +import { |
| 3 | + SEMANTIC_ATTRIBUTE_SENTRY_OP, |
| 4 | + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
| 5 | + SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, |
| 6 | +} from './semanticAttributes'; |
| 7 | +import { startSpan } from './tracing'; |
| 8 | +import { logger } from './utils-hoist'; |
| 9 | + |
| 10 | +interface MCPServerInstance { |
| 11 | + // The first arg is always a name, the last arg should always be a callback function (ie a handler). |
| 12 | + // TODO: We could also make use of the resource uri argument somehow. |
| 13 | + resource: (name: string, ...args: unknown[]) => void; |
| 14 | + // The first arg is always a name, the last arg should always be a callback function (ie a handler). |
| 15 | + tool: (name: string, ...args: unknown[]) => void; |
| 16 | + // The first arg is always a name, the last arg should always be a callback function (ie a handler). |
| 17 | + prompt: (name: string, ...args: unknown[]) => void; |
| 18 | +} |
| 19 | + |
| 20 | +const wrappedMcpServerInstances = new WeakSet(); |
| 21 | + |
| 22 | +/** |
| 23 | + * Wraps a MCP Server instance from the `@modelcontextprotocol/sdk` package with Sentry instrumentation. |
| 24 | + * |
| 25 | + * Compatible with versions `^1.9.0` of the `@modelcontextprotocol/sdk` package. |
| 26 | + */ |
| 27 | +// We are exposing this API for non-node runtimes that cannot rely on auto-instrumentation. |
| 28 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 29 | +export function wrapMcpServerWithSentry<S>(mcpServerInstance: any): S { |
| 30 | + if (wrappedMcpServerInstances.has(mcpServerInstance)) { |
| 31 | + return mcpServerInstance; |
| 32 | + } |
| 33 | + |
| 34 | + if (!isMcpServerInstance(mcpServerInstance)) { |
| 35 | + DEBUG_BUILD && logger.warn('Did not patch MCP server. Interface is incompatible.'); |
| 36 | + return mcpServerInstance; |
| 37 | + } |
| 38 | + |
| 39 | + mcpServerInstance.resource = new Proxy(mcpServerInstance.resource, { |
| 40 | + apply(target, thisArg, argArray) { |
| 41 | + const resourceName: unknown = argArray[0]; |
| 42 | + const resourceHandler: unknown = argArray[argArray.length - 1]; |
| 43 | + |
| 44 | + if (typeof resourceName !== 'string' || typeof resourceHandler !== 'function') { |
| 45 | + return target.apply(thisArg, argArray); |
| 46 | + } |
| 47 | + |
| 48 | + return startSpan( |
| 49 | + { |
| 50 | + name: `mcp-server/resource:${resourceName}`, |
| 51 | + forceTransaction: true, |
| 52 | + attributes: { |
| 53 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'auto.function.mcp-server', |
| 54 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.mcp-server', |
| 55 | + [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', |
| 56 | + 'mcp_server.resource': resourceName, |
| 57 | + }, |
| 58 | + }, |
| 59 | + () => target.apply(thisArg, argArray), |
| 60 | + ); |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + mcpServerInstance.tool = new Proxy(mcpServerInstance.tool, { |
| 65 | + apply(target, thisArg, argArray) { |
| 66 | + const toolName: unknown = argArray[0]; |
| 67 | + const toolHandler: unknown = argArray[argArray.length - 1]; |
| 68 | + |
| 69 | + if (typeof toolName !== 'string' || typeof toolHandler !== 'function') { |
| 70 | + return target.apply(thisArg, argArray); |
| 71 | + } |
| 72 | + |
| 73 | + return startSpan( |
| 74 | + { |
| 75 | + name: `mcp-server/tool:${toolName}`, |
| 76 | + forceTransaction: true, |
| 77 | + attributes: { |
| 78 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'auto.function.mcp-server', |
| 79 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.mcp-server', |
| 80 | + [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', |
| 81 | + 'mcp_server.tool': toolName, |
| 82 | + }, |
| 83 | + }, |
| 84 | + () => target.apply(thisArg, argArray), |
| 85 | + ); |
| 86 | + }, |
| 87 | + }); |
| 88 | + |
| 89 | + mcpServerInstance.prompt = new Proxy(mcpServerInstance.prompt, { |
| 90 | + apply(target, thisArg, argArray) { |
| 91 | + const promptName: unknown = argArray[0]; |
| 92 | + const promptHandler: unknown = argArray[argArray.length - 1]; |
| 93 | + |
| 94 | + if (typeof promptName !== 'string' || typeof promptHandler !== 'function') { |
| 95 | + return target.apply(thisArg, argArray); |
| 96 | + } |
| 97 | + |
| 98 | + return startSpan( |
| 99 | + { |
| 100 | + name: `mcp-server/resource:${promptName}`, |
| 101 | + forceTransaction: true, |
| 102 | + attributes: { |
| 103 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'auto.function.mcp-server', |
| 104 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.mcp-server', |
| 105 | + [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', |
| 106 | + 'mcp_server.prompt': promptName, |
| 107 | + }, |
| 108 | + }, |
| 109 | + () => target.apply(thisArg, argArray), |
| 110 | + ); |
| 111 | + }, |
| 112 | + }); |
| 113 | + |
| 114 | + wrappedMcpServerInstances.add(mcpServerInstance); |
| 115 | + |
| 116 | + return mcpServerInstance as S; |
| 117 | +} |
| 118 | + |
| 119 | +function isMcpServerInstance(mcpServerInstance: unknown): mcpServerInstance is MCPServerInstance { |
| 120 | + return ( |
| 121 | + typeof mcpServerInstance === 'object' && |
| 122 | + mcpServerInstance !== null && |
| 123 | + 'resource' in mcpServerInstance && |
| 124 | + typeof mcpServerInstance.resource === 'function' && |
| 125 | + 'tool' in mcpServerInstance && |
| 126 | + typeof mcpServerInstance.tool === 'function' && |
| 127 | + 'prompt' in mcpServerInstance && |
| 128 | + typeof mcpServerInstance.prompt === 'function' |
| 129 | + ); |
| 130 | +} |
0 commit comments