feat(instrumentation-mcp): add OpenTelemetry instrumentation for MCP SDK#3175
Closed
lukeina2z wants to merge 1 commit intoopen-telemetry:mainfrom
Closed
feat(instrumentation-mcp): add OpenTelemetry instrumentation for MCP SDK#3175lukeina2z wants to merge 1 commit intoopen-telemetry:mainfrom
lukeina2z wants to merge 1 commit intoopen-telemetry:mainfrom
Conversation
Contributor
|
@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. |
Author
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add automatic instrumentation for the Model Context Protocol (MCP) SDK, enabling distributed tracing across MCP client-server boundaries.
Features:
Implementation:
Semantic Conventions:
Examples:
Testing:
Known Limitations:
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