Package com.tnsai.mcp.observability
Class McpTracingInterceptor
java.lang.Object
com.tnsai.mcp.observability.McpTracingInterceptor
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionMcpTracingInterceptor(McpTransport delegate, McpTracer tracer) Creates a tracing interceptor wrapping the given transport. -
Method Summary
Modifier and TypeMethodDescriptionvoidconnect()Connects to the MCP server.voidDisconnects from the MCP server.Returns the underlying transport being decorated.booleanChecks if the transport is currently connected.voidSends a message to the MCP server.voidsetOnError(Consumer<Throwable> errorConsumer) Sets a consumer for transport errors.voidsetOnMessage(Consumer<String> messageConsumer) Sets a consumer for incoming messages from the MCP server.
-
Constructor Details
-
McpTracingInterceptor
Creates a tracing interceptor wrapping the given transport.- Parameters:
delegate- the underlying transport to delegate totracer- the tracer to create spans with
-
-
Method Details
-
connect
Description copied from interface:McpTransportConnects to the MCP server.- Specified by:
connectin interfaceMcpTransport- Throws:
IOException- if connection fails
-
send
Description copied from interface:McpTransportSends a message to the MCP server.- Specified by:
sendin interfaceMcpTransport- Parameters:
message- the message to send (JSON-RPC request)- Throws:
IOException- if sending fails
-
setOnMessage
Description copied from interface:McpTransportSets a consumer for incoming messages from the MCP server.- Specified by:
setOnMessagein interfaceMcpTransport- Parameters:
messageConsumer- the consumer for incoming messages (JSON-RPC response/notification)
-
setOnError
Description copied from interface:McpTransportSets a consumer for transport errors.- Specified by:
setOnErrorin interfaceMcpTransport- Parameters:
errorConsumer- the consumer for transport errors
-
disconnect
Description copied from interface:McpTransportDisconnects from the MCP server.- Specified by:
disconnectin interfaceMcpTransport- Throws:
IOException- if disconnection fails
-
isConnected
public boolean isConnected()Description copied from interface:McpTransportChecks if the transport is currently connected.- Specified by:
isConnectedin interfaceMcpTransport- Returns:
- true if connected, false otherwise
-
getDelegate
Returns the underlying transport being decorated.- Returns:
- the delegate transport
-