Class TracingMcpClient

java.lang.Object
com.tnsai.mcp.observability.TracingMcpClient

public class TracingMcpClient extends Object
Decorator that adds tracing and metrics collection to an McpClient.

Wraps all public McpClient operations with:

Each operation creates a span with relevant attributes such as tool name, resource URI, or prompt name. If a metrics collector is provided, tool invocations are also recorded with success/failure status and duration.

Example usage:


 McpClient client = McpClient.create(transport);
 client.connect();
 client.initialize();

 McpTracer tracer = McpTracer.logging();
 McpMetricsCollector metrics = new McpMetricsCollector();
 TracingMcpClient tracingClient = new TracingMcpClient(client, tracer, metrics);

 // All operations are now traced and measured
 List<McpToolDefinition> tools = tracingClient.listTools();
 JsonNode result = tracingClient.executeTool("readFile", Map.of("path", "/tmp/test.txt"));

 // Access collected metrics
 Map<String, Object> summary = tracingClient.getMetrics().getSummary();
 
See Also:
  • Constructor Details

    • TracingMcpClient

      public TracingMcpClient(McpClient delegate, McpTracer tracer)
      Creates a tracing client without metrics collection.
      Parameters:
      delegate - the McpClient to wrap
      tracer - the tracer to create spans with
    • TracingMcpClient

      public TracingMcpClient(McpClient delegate, McpTracer tracer, McpMetricsCollector metrics)
      Creates a tracing client with metrics collection.
      Parameters:
      delegate - the McpClient to wrap
      tracer - the tracer to create spans with
      metrics - the metrics collector (may be null)
  • Method Details