Package com.tnsai.mcp.testing
Class McpInspectorClient
java.lang.Object
com.tnsai.mcp.testing.McpInspectorClient
- All Implemented Interfaces:
McpTransport
Decorator transport that records all JSON-RPC messages for inspection.
Wraps an existing McpTransport and records every message sent and received, along with timing information. Useful for debugging and analyzing MCP interactions.
Usage
McpTransport baseTransport = new MockMcpTransport(server); McpInspectorClient inspector = new McpInspectorClient(baseTransport); McpClient client = new McpClient(inspector); client.connect(); client.initialize(); // After operations... List<McpInspectorClient.MessageRecord> messages = inspector.getMessages(); Duration avgLatency = inspector.getAverageLatency();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordA recorded message with metadata. -
Constructor Summary
ConstructorsConstructorDescriptionMcpInspectorClient(McpTransport delegate) Creates an inspector that wraps the given transport. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all recorded messages.voidconnect()Connects to the MCP server.voidDisconnects from the MCP server.Returns the average latency of all recorded request-response pairs.Returns all recorded messages.getMessagesByMethod(String method) Returns messages filtered by method name.Returns a count of messages grouped by method.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
-
McpInspectorClient
Creates an inspector that wraps the given transport.- Parameters:
delegate- the transport to wrap
-
-
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
-
getMessages
Returns all recorded messages.- Returns:
- list of message records
-
getMessagesByMethod
Returns messages filtered by method name.- Parameters:
method- the method to filter by- Returns:
- filtered list of message records
-
getAverageLatency
Returns the average latency of all recorded request-response pairs.- Returns:
- the average latency, or Duration.ZERO if no latencies recorded
-
getMethodCounts
Returns a count of messages grouped by method.- Returns:
- map of method name to message count
-
clear
public void clear()Clears all recorded messages.
-