Skip to content

Conversation

lukeina2z
Copy link
Contributor

@lukeina2z lukeina2z commented Oct 15, 2025

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)

Related PRs:
OTel Python
Add OpenTelemetry instrumentation for Model Context Protocol (MCP)
open-telemetry/opentelemetry-python-contrib#3822

MCP semantic conventions #2083
open-telemetry/semantic-conventions#2083

@trentm
Copy link
Contributor

trentm commented Oct 15, 2025

@lukeina2z You mentioned having a Python instrumentation for the same. Could you link to that please? It could help provide context.

Also a link to the in-progress semconv PR would be helpful.

@lukeina2z
Copy link
Contributor Author

@lukeina2z You mentioned having a Python instrumentation for the same. Could you link to that please? It could help provide context.

Also a link to the in-progress semconv PR would be helpful.

Both are added in PR description as "Related PRs".

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants