Package com.tnsai.mcp.testing
Class MockMcpTransport
java.lang.Object
com.tnsai.mcp.testing.MockMcpTransport
- All Implemented Interfaces:
McpTransport
In-memory transport that connects McpClient directly to MockMcpServer.
Messages are routed synchronously without any network I/O, making it ideal for unit testing MCP client behavior.
Usage
MockMcpServer server = new MockMcpServer();
server.whenToolReturns("echo", resultNode);
MockMcpTransport transport = new MockMcpTransport(server);
McpClient client = new McpClient(transport);
client.connect();
client.initialize();
JsonNode result = client.executeTool("echo", Map.of());
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionMockMcpTransport(MockMcpServer server) Creates a MockMcpTransport backed by the given server. -
Method Summary
Modifier and TypeMethodDescriptionvoidconnect()Connects to the MCP server.voidDisconnects from the MCP server.Returns the underlying MockMcpServer.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
-
MockMcpTransport
Creates a MockMcpTransport backed by the given server.- Parameters:
server- the mock MCP server to route messages to
-
-
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
-
getServer
Returns the underlying MockMcpServer.- Returns:
- the mock server
-