Commit f30fd9c
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
File tree
46 files changed
+9361
-6
lines changed- packages
- auto-instrumentations-node
- src
- instrumentation-mcp
- examples
- http-client-server
- client
- src
- server
- src
- simple-client-server
- client
- src
- server
- src
- src
- test
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| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| 88 | + | |
| 89 | + | |
0 commit comments