Skip to content

Commit 1823b32

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 23c5f4e commit 1823b32

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

.github/component_owners.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ components:
113113
# Unmaintained
114114
packages/instrumentation-koa: []
115115
# Unmaintained
116+
packages/instrumentation-mcp:
117+
- lukeina2z
116118
packages/instrumentation-memcached: []
117119
# Unmaintained
118120
packages/instrumentation-mongodb:

.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

.release-please-manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"packages/instrumentation-ioredis": "0.54.0",
4646
"packages/instrumentation-knex": "0.51.0",
4747
"packages/instrumentation-koa": "0.55.0",
48+
"packages/instrumentation-mcp": "0.0.1",
4849
"packages/instrumentation-memcached": "0.50.0",
4950
"packages/instrumentation-mongodb": "0.59.0",
5051
"packages/instrumentation-mysql": "0.52.0",

0 commit comments

Comments
 (0)