Commit 1823b32
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 23c5f4e commit 1823b32
File tree
49 files changed
+9365
-6
lines changed- .github
- 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.
49 files changed
+9365
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| 116 | + | |
| 117 | + | |
116 | 118 | | |
117 | 119 | | |
118 | 120 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| 88 | + | |
| 89 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
| |||
0 commit comments