Package com.tnsai.mcp.testing
Class MockMcpServer
java.lang.Object
com.tnsai.mcp.testing.MockMcpServer
In-memory MCP server for unit testing.
Allows registering tool, resource, and prompt handlers that respond to JSON-RPC messages without any network I/O.
Usage
MockMcpServer server = new MockMcpServer("test-server", "1.0.0");
server.whenToolReturns("echo", objectMapper.createObjectNode().put("text", "hello"));
server.whenResource("file://data.txt", "file contents");
String response = server.processMessage(jsonRpcRequest);
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classException thrown by mock tool handlers to simulate errors. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a MockMcpServer with default name and version.MockMcpServer(String serverName, String serverVersion) Creates a MockMcpServer with specified name and version. -
Method Summary
Modifier and TypeMethodDescriptionReturns the server name.Returns the server version.processMessage(String jsonRpcMessage) Processes a JSON-RPC message and returns the response.whenPrompt(String name, com.fasterxml.jackson.databind.JsonNode result) Registers a prompt handler that returns the given result.whenResource(String uri, String content) Registers a resource with text content.Registers a tool handler function.whenToolReturns(String name, com.fasterxml.jackson.databind.JsonNode result) Registers a tool that always returns the given result.whenToolThrows(String name, int errorCode, String errorMessage) Registers a tool that always throws an error.
-
Constructor Details
-
MockMcpServer
public MockMcpServer()Creates a MockMcpServer with default name and version. -
MockMcpServer
Creates a MockMcpServer with specified name and version.- Parameters:
serverName- the server nameserverVersion- the server version
-
-
Method Details
-
whenTool
public MockMcpServer whenTool(String name, Function<Map<String, Object>, com.fasterxml.jackson.databind.JsonNode> handler) Registers a tool handler function.- Parameters:
name- the tool namehandler- function that takes arguments and returns a result- Returns:
- this server for chaining
-
whenToolReturns
Registers a tool that always returns the given result.- Parameters:
name- the tool nameresult- the result to return- Returns:
- this server for chaining
-
whenToolThrows
Registers a tool that always throws an error.- Parameters:
name- the tool nameerrorCode- the JSON-RPC error codeerrorMessage- the error message- Returns:
- this server for chaining
-
whenResource
Registers a resource with text content.- Parameters:
uri- the resource URIcontent- the text content- Returns:
- this server for chaining
-
whenPrompt
Registers a prompt handler that returns the given result.- Parameters:
name- the prompt nameresult- the prompt result- Returns:
- this server for chaining
-
processMessage
Processes a JSON-RPC message and returns the response.- Parameters:
jsonRpcMessage- the incoming JSON-RPC message- Returns:
- the JSON-RPC response string
-
getServerName
Returns the server name.- Returns:
- the server name
-
getServerVersion
Returns the server version.- Returns:
- the server version
-