Skip to content

OTEL: Add error.type and rpc.response.status_code for JSON-RPC errors #3765

Open

Description

@ChrisJBurns

Context

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

Activity

  1. added
    enhancement
    on Feb 11, 2026
  2. added
    go
    on Feb 11, 2026
  3. ChrisJBurns commented on Mar 4, 2026

    @ChrisJBurns
    CollaboratorAuthor

    Implementation Guide

    Architecture Overview

    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.

    Data flow: Client → Middleware (wraps ResponseWriter) → ReverseProxy → MCP Server → ModifyResponse (response processors) → Middleware (finalizes span) → Client

    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)

    What 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.go

    Add alongside the existing ParsedMCPRequest:

    type ParsedMCPResponse struct {
        // JSONRPCErrorCode is set when the response is a JSON-RPC error (e.g., -32601)
        JSONRPCErrorCode int
        // JSONRPCErrorMessage is the error message from the JSON-RPC error
        JSONRPCErrorMessage string
        // IsToolError is true when the response is a successful JSON-RPC response
        // but the tool itself reported an error (CallToolResult.isError = true)
        IsToolError bool
    }

    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

    Where: pkg/transport/proxy/transparent/transparent_proxy.go

    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.

    Key Files

    File Role
    pkg/transport/proxy/transparent/response_processor.go ResponseProcessor interface — add new telemetry processor here
    pkg/transport/proxy/transparent/sse_response_processor.go Reference implementation for the io.Pipe tee pattern
    pkg/transport/proxy/transparent/transparent_proxy.go:432 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)

    Related Issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgotelemetry

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions