Package com.tnsai.mcp.observability
Class TracingMcpClient
java.lang.Object
com.tnsai.mcp.observability.TracingMcpClient
Decorator that adds tracing and metrics collection to an
McpClient.
Wraps all public McpClient operations with:
- Tracing spans via
McpTracer - Metrics collection via
McpMetricsCollector(optional)
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 Summary
ConstructorsConstructorDescriptionTracingMcpClient(McpClient delegate, McpTracer tracer) Creates a tracing client without metrics collection.TracingMcpClient(McpClient delegate, McpTracer tracer, McpMetricsCollector metrics) Creates a tracing client with metrics collection. -
Method Summary
Modifier and TypeMethodDescriptionvoidconnect()Connects to the MCP server with tracing.voidDisconnects from the MCP server with tracing.com.fasterxml.jackson.databind.JsonNodeexecuteTool(String toolName, Map<String, Object> arguments) Executes a tool on the server with tracing and metrics.Returns the underlying McpClient being decorated.Returns the metrics collector, or null if metrics collection is disabled.com.fasterxml.jackson.databind.JsonNodeGets a prompt with the given arguments, with tracing.voidInitializes the MCP session with tracing.booleanChecks if the underlying client is connected.booleanChecks if the underlying client has completed initialization.Lists available prompts on the server with tracing.Lists available resources on the server with tracing.Lists available tools on the server with tracing.readResource(String uri) Reads a resource from the server with tracing.
-
Constructor Details
-
TracingMcpClient
Creates a tracing client without metrics collection.- Parameters:
delegate- the McpClient to wraptracer- the tracer to create spans with
-
TracingMcpClient
Creates a tracing client with metrics collection.- Parameters:
delegate- the McpClient to wraptracer- the tracer to create spans withmetrics- the metrics collector (may be null)
-
-
Method Details
-
connect
Connects to the MCP server with tracing.- Throws:
IOException- if connection fails
-
initialize
Initializes the MCP session with tracing.- Throws:
IOException- if initialization failsMcpClient.McpException- if server returns an error
-
disconnect
Disconnects from the MCP server with tracing.- Throws:
IOException- if disconnection fails
-
isConnected
public boolean isConnected()Checks if the underlying client is connected.- Returns:
- true if connected
-
isInitialized
public boolean isInitialized()Checks if the underlying client has completed initialization.- Returns:
- true if initialized
-
listTools
Lists available tools on the server with tracing.- Returns:
- list of tool definitions
- Throws:
IOException- if communication failsMcpClient.McpException- if server returns an error
-
executeTool
public com.fasterxml.jackson.databind.JsonNode executeTool(String toolName, Map<String, Object> arguments) throws IOException, McpClient.McpExceptionExecutes a tool on the server with tracing and metrics.- Parameters:
toolName- the name of the toolarguments- the arguments for the tool- Returns:
- the result of the execution
- Throws:
IOException- if communication failsMcpClient.McpException- if server returns an error
-
listResources
Lists available resources on the server with tracing.- Returns:
- list of resources
- Throws:
IOException- if communication failsMcpClient.McpException- if server returns an error
-
readResource
Reads a resource from the server with tracing.- Parameters:
uri- the resource URI- Returns:
- list of resource contents
- Throws:
IOException- if communication failsMcpClient.McpException- if server returns an error
-
listPrompts
Lists available prompts on the server with tracing.- Returns:
- list of prompts
- Throws:
IOException- if communication failsMcpClient.McpException- if server returns an error
-
getPrompt
public com.fasterxml.jackson.databind.JsonNode getPrompt(String name, Map<String, String> arguments) throws IOException, McpClient.McpExceptionGets a prompt with the given arguments, with tracing.- Parameters:
name- the prompt namearguments- the prompt arguments- Returns:
- the prompt messages
- Throws:
IOException- if communication failsMcpClient.McpException- if server returns an error
-
getMetrics
Returns the metrics collector, or null if metrics collection is disabled.- Returns:
- the metrics collector
-
getDelegate
Returns the underlying McpClient being decorated.- Returns:
- the delegate client
-