You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
OTEL: Add error.type and rpc.response.status_code for JSON-RPC errors #3765
Identified during the OTEL MCP semantic conventions review (parent issue: #3399). These two spec gaps are related and should be implemented together since they both require the same response-body inspection approach.
error.type missing for JSON-RPC errors and tool_error
Severity: Medium File: pkg/telemetry/middleware.go
error.type is only set for HTTP 5xx errors. JSON-RPC errors returned in HTTP 200 responses (e.g., -32601) and failed CallToolResult ("tool_error") are invisible to telemetry. This is acknowledged in code comments as future work.
Fix: Would require response body inspection — buffer/parse the JSON-RPC response to detect error codes and tool errors.
Missing rpc.response.status_code for JSON-RPC errors
Severity: Low File: pkg/telemetry/middleware.go
The OTEL RPC semconv includes rpc.response.status_code for error responses. Would require response body parsing for JSON-RPC error codes.
Fix: Same response-body inspection approach as above.
Implementation considerations
Both require intercepting/buffering the response body before it's written to the client
Need to parse JSON-RPC response to extract error codes (e.g., -32601, -32602) and CallToolResult.isError
A tee-style response writer that captures the body while still streaming to the client would be one approach
Should set error.type to the JSON-RPC error code string (e.g., "-32601") or "tool_error" for failed tool calls
High value for observability — currently JSON-RPC errors are invisible in telemetry since they return HTTP 200
The proxy already has the infrastructure for this. The ModifyResponse hook on httputil.ReverseProxy fires before the response body streams to the client. The SSEResponseProcessor already demonstrates a tee pattern using io.Pipe to read and process the response body while forwarding it unmodified. This same pattern can be used for JSON-RPC response inspection.
The key insight is that ModifyResponse runs before the middleware's deferred span finalization, so attributes set on the span during response processing will be captured.
Implementation Steps
1. Add a response body tee to capture JSON-RPC responses
Where: pkg/transport/proxy/transparent/
Create a new TelemetryResponseProcessor that implements the existing ResponseProcessor interface. It should:
Use the same io.Pipe tee pattern as SSEResponseProcessor (see sse_response_processor.go:89-104)
Buffer only application/json responses (not SSE streams or other content types)
Parse the buffered JSON to detect JSON-RPC errors and CallToolResult.isError
Store the parsed result somewhere accessible to the telemetry middleware (context won't work since this is on the response path — use resp.Request.Context() with a new context key, or store on the response header as internal metadata)
ShouldProcess criteria:
Response Content-Type is application/json
Response status is 200 (we already handle non-200 in the middleware)
2. Define a parsed response struct and context key
Where: pkg/mcp/parser.go
Add alongside the existing ParsedMCPRequest:
typeParsedMCPResponsestruct {
// JSONRPCErrorCode is set when the response is a JSON-RPC error (e.g., -32601)JSONRPCErrorCodeint// JSONRPCErrorMessage is the error message from the JSON-RPC errorJSONRPCErrorMessagestring// IsToolError is true when the response is a successful JSON-RPC response// but the tool itself reported an error (CallToolResult.isError = true)IsToolErrorbool
}
Use resp.Request context to pass this from the response processor to the middleware. The request's context is accessible in both ModifyResponse (via resp.Request) and in the middleware's deferred span finalization (via the original request context). Store using a new context key in pkg/mcp/parser.go.
Important: Since resp.Request.Context() is read-only at that point, use a pointer-based approach — store a *ParsedMCPResponse pointer in the request context during the request phase (in the MCP parsing middleware), then populate it during the response phase in the response processor. The telemetry middleware reads the same pointer after the response processor has filled it in.
3. Integrate the response processor into the proxy
The current modifyResponse delegates to a single ResponseProcessor. You'll need to support running multiple processors (one for SSE rewriting, one for telemetry). Either:
Change modifyResponse to run a chain of processors, or
Compose processors (the telemetry processor wraps any other processor)
The telemetry processor should run for all transport types (SSE and streamable-http), not just one.
4. Set OTel span attributes from the parsed response
Where: pkg/telemetry/middleware.go
In the deferred span finalization (around line 590-608 in setSpanStatus), check for the parsed response:
JSON-RPC error: Set error.type to the error code string (e.g., "-32601"), set rpc.response.status_code to the numeric code, mark span status as Error
Tool error (isError: true): Set error.type to "tool_error", mark span status as Error
Also update recordMetrics (around line 660) to include the error type in the status attribute for toolCallCounter and mcp.server.operation.duration.
modifyResponse — needs to support multiple processors
pkg/mcp/parser.go
Add ParsedMCPResponse struct and context key
pkg/telemetry/middleware.go:590-608
setSpanStatus — consume parsed response to set error.type
pkg/telemetry/middleware.go:660-720
recordMetrics — include error type in metrics attributes
pkg/telemetry/middleware.go:742-749
Existing TODO comment to remove once implemented
Testing
Unit test the JSON-RPC response parser with: valid success, JSON-RPC error codes, isError: true tool results, malformed JSON, non-JSON responses, batch responses
Unit test the telemetry response processor with mock responses
Verify span attributes are set correctly for each error type using the existing OTEL test patterns in pkg/telemetry/middleware_test.go
Scope Boundaries
In scope: error.type for JSON-RPC errors and tool errors, rpc.response.status_code for JSON-RPC error codes Out of scope: Capturing full error message content in spans (privacy/size concerns — can be a follow-up), batch request handling (can be a follow-up)
Activity
Add comprehensive OTEL telemetry documentation and migration guide
Add OTEL telemetry documentation and migration guide (#3784)
ChrisJBurns commentedon Mar 4, 2026
Implementation Guide
Architecture Overview
The proxy already has the infrastructure for this. The
ModifyResponsehook onhttputil.ReverseProxyfires before the response body streams to the client. TheSSEResponseProcessoralready demonstrates a tee pattern usingio.Pipeto read and process the response body while forwarding it unmodified. This same pattern can be used for JSON-RPC response inspection.Data flow: Client → Middleware (wraps
ResponseWriter) → ReverseProxy → MCP Server →ModifyResponse(response processors) → Middleware (finalizes span) → ClientThe key insight is that
ModifyResponseruns before the middleware's deferred span finalization, so attributes set on the span during response processing will be captured.Implementation Steps
1. Add a response body tee to capture JSON-RPC responses
Where:
pkg/transport/proxy/transparent/Create a new
TelemetryResponseProcessorthat implements the existingResponseProcessorinterface. It should:io.Pipetee pattern asSSEResponseProcessor(seesse_response_processor.go:89-104)application/jsonresponses (not SSE streams or other content types)CallToolResult.isErrorresp.Request.Context()with a new context key, or store on the response header as internal metadata)ShouldProcesscriteria:Content-Typeisapplication/jsonWhat to extract from the response body:
JSON-RPC error response structure:
{"jsonrpc": "2.0", "id": 1, "error": {"code": -32601, "message": "Method not found"}}Tool call error structure (HTTP 200, valid JSON-RPC response, but tool failed):
{"jsonrpc": "2.0", "id": 1, "result": {"content": [...], "isError": true}}2. Define a parsed response struct and context key
Where:
pkg/mcp/parser.goAdd alongside the existing
ParsedMCPRequest:Use
resp.Requestcontext to pass this from the response processor to the middleware. The request's context is accessible in bothModifyResponse(viaresp.Request) and in the middleware's deferred span finalization (via the original request context). Store using a new context key inpkg/mcp/parser.go.Important: Since
resp.Request.Context()is read-only at that point, use a pointer-based approach — store a*ParsedMCPResponsepointer in the request context during the request phase (in the MCP parsing middleware), then populate it during the response phase in the response processor. The telemetry middleware reads the same pointer after the response processor has filled it in.3. Integrate the response processor into the proxy
Where:
pkg/transport/proxy/transparent/transparent_proxy.goThe current
modifyResponsedelegates to a singleResponseProcessor. You'll need to support running multiple processors (one for SSE rewriting, one for telemetry). Either:modifyResponseto run a chain of processors, orThe telemetry processor should run for all transport types (SSE and streamable-http), not just one.
4. Set OTel span attributes from the parsed response
Where:
pkg/telemetry/middleware.goIn the deferred span finalization (around line 590-608 in
setSpanStatus), check for the parsed response:error.typeto the error code string (e.g.,"-32601"), setrpc.response.status_codeto the numeric code, mark span status asErrorisError: true): Seterror.typeto"tool_error", mark span status asErrorAlso update
recordMetrics(around line 660) to include the error type in thestatusattribute fortoolCallCounterandmcp.server.operation.duration.Key Files
pkg/transport/proxy/transparent/response_processor.goResponseProcessorinterface — add new telemetry processor herepkg/transport/proxy/transparent/sse_response_processor.goio.Pipetee patternpkg/transport/proxy/transparent/transparent_proxy.go:432modifyResponse— needs to support multiple processorspkg/mcp/parser.goParsedMCPResponsestruct and context keypkg/telemetry/middleware.go:590-608setSpanStatus— consume parsed response to seterror.typepkg/telemetry/middleware.go:660-720recordMetrics— include error type in metrics attributespkg/telemetry/middleware.go:742-749Testing
isError: truetool results, malformed JSON, non-JSON responses, batch responsespkg/telemetry/middleware_test.goScope Boundaries
In scope:
error.typefor JSON-RPC errors and tool errors,rpc.response.status_codefor JSON-RPC error codesOut of scope: Capturing full error message content in spans (privacy/size concerns — can be a follow-up), batch request handling (can be a follow-up)
Related Issues