Skip to content

Commit f30fd9c

Browse files
committed
feat(instrumentation-mcp): add OpenTelemetry instrumentation for MCP SDK
Add automatic instrumentation for the Model Context Protocol (MCP) SDK, enabling distributed tracing across MCP client-server boundaries. Features: - Automatic span creation for client requests and server handlers - Distributed tracing with context propagation via request metadata - MCP-specific semantic conventions (methods, tools, prompts, resources) - Support for CommonJS with zero-code auto-instrumentation - Debug logging for troubleshooting (file-based for stdio compatibility) - Runtime module patching using require-in-the-middle/import-in-the-middle Implementation: - Instruments Client.prototype.request for client-side tracing - Instruments Server.prototype.setRequestHandler for server-side tracing - Injects trace context into request.params._meta field - Extracts trace context from request.params._meta on server - Handles both stdio and HTTP/SSE transports - Workaround for McpServer/Server class loading order issue Semantic Conventions: - mcp.method.name: MCP method name (e.g., tools/call) - mcp.request.id: Request identifier - mcp.tool.name: Tool name for tools/call - mcp.request.argument.*: Individual tool arguments - mcp.prompt.name: Prompt name for prompts/get - mcp.resource.uri: Resource URI for resources/read Examples: - simple-client-server: Basic stdio transport with tracing - http-client-server: HTTP/SSE transport with tracing Testing: - Unit tests for span attribute updates - Tests for client request patching - Tests for server handler patching - Tests for debug logging configuration Known Limitations: - ESM (ECMAScript Modules) not supported - Assumes MCP SDK internal structure (dist/cjs or dist/esm)
1 parent ef0ca68 commit f30fd9c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+9361
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,5 @@ version.ts
8585
/.vs
8686
.cursor/rules/nx-rules.mdc
8787
.github/instructions/nx.instructions.md
88+
89+
packages/instrumentation-typeorm/connection2.db

0 commit comments

Comments
 (0)