Class McpTracingInterceptor

java.lang.Object
com.tnsai.mcp.observability.McpTracingInterceptor
All Implemented Interfaces:
McpTransport

public class McpTracingInterceptor extends Object implements McpTransport
Decorator that wraps an McpTransport to add tracing spans around send and receive operations.

For each send(String) call, this interceptor:

  • Creates a new span named "mcp.transport.send"
  • Records the JSON-RPC method name (extracted from the message)
  • Records the message size in bytes
  • Sets the span status based on success or failure

For incoming messages, the interceptor wraps the message consumer to record response sizes.

Example usage:


 McpTransport transport = new StdioTransport("npx", "server");
 McpTracer tracer = McpTracer.logging();
 McpTransport tracedTransport = new McpTracingInterceptor(transport, tracer);

 McpClient client = new McpClient(tracedTransport);
 
See Also:
  • Constructor Details

    • McpTracingInterceptor

      public McpTracingInterceptor(McpTransport delegate, McpTracer tracer)
      Creates a tracing interceptor wrapping the given transport.
      Parameters:
      delegate - the underlying transport to delegate to
      tracer - the tracer to create spans with
  • Method Details

    • connect

      public void connect() throws IOException
      Description copied from interface: McpTransport
      Connects to the MCP server.
      Specified by:
      connect in interface McpTransport
      Throws:
      IOException - if connection fails
    • send

      public void send(String message) throws IOException
      Description copied from interface: McpTransport
      Sends a message to the MCP server.
      Specified by:
      send in interface McpTransport
      Parameters:
      message - the message to send (JSON-RPC request)
      Throws:
      IOException - if sending fails
    • setOnMessage

      public void setOnMessage(Consumer<String> messageConsumer)
      Description copied from interface: McpTransport
      Sets a consumer for incoming messages from the MCP server.
      Specified by:
      setOnMessage in interface McpTransport
      Parameters:
      messageConsumer - the consumer for incoming messages (JSON-RPC response/notification)
    • setOnError

      public void setOnError(Consumer<Throwable> errorConsumer)
      Description copied from interface: McpTransport
      Sets a consumer for transport errors.
      Specified by:
      setOnError in interface McpTransport
      Parameters:
      errorConsumer - the consumer for transport errors
    • disconnect

      public void disconnect() throws IOException
      Description copied from interface: McpTransport
      Disconnects from the MCP server.
      Specified by:
      disconnect in interface McpTransport
      Throws:
      IOException - if disconnection fails
    • isConnected

      public boolean isConnected()
      Description copied from interface: McpTransport
      Checks if the transport is currently connected.
      Specified by:
      isConnected in interface McpTransport
      Returns:
      true if connected, false otherwise
    • getDelegate

      public McpTransport getDelegate()
      Returns the underlying transport being decorated.
      Returns:
      the delegate transport