Class MockMcpServer

java.lang.Object
com.tnsai.mcp.testing.MockMcpServer

public class MockMcpServer extends Object
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:
  • Constructor Details

    • MockMcpServer

      public MockMcpServer()
      Creates a MockMcpServer with default name and version.
    • MockMcpServer

      public MockMcpServer(String serverName, String serverVersion)
      Creates a MockMcpServer with specified name and version.
      Parameters:
      serverName - the server name
      serverVersion - 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 name
      handler - function that takes arguments and returns a result
      Returns:
      this server for chaining
    • whenToolReturns

      public MockMcpServer whenToolReturns(String name, com.fasterxml.jackson.databind.JsonNode result)
      Registers a tool that always returns the given result.
      Parameters:
      name - the tool name
      result - the result to return
      Returns:
      this server for chaining
    • whenToolThrows

      public MockMcpServer whenToolThrows(String name, int errorCode, String errorMessage)
      Registers a tool that always throws an error.
      Parameters:
      name - the tool name
      errorCode - the JSON-RPC error code
      errorMessage - the error message
      Returns:
      this server for chaining
    • whenResource

      public MockMcpServer whenResource(String uri, String content)
      Registers a resource with text content.
      Parameters:
      uri - the resource URI
      content - the text content
      Returns:
      this server for chaining
    • whenPrompt

      public MockMcpServer whenPrompt(String name, com.fasterxml.jackson.databind.JsonNode result)
      Registers a prompt handler that returns the given result.
      Parameters:
      name - the prompt name
      result - the prompt result
      Returns:
      this server for chaining
    • processMessage

      public String processMessage(String jsonRpcMessage)
      Processes a JSON-RPC message and returns the response.
      Parameters:
      jsonRpcMessage - the incoming JSON-RPC message
      Returns:
      the JSON-RPC response string
    • getServerName

      public String getServerName()
      Returns the server name.
      Returns:
      the server name
    • getServerVersion

      public String getServerVersion()
      Returns the server version.
      Returns:
      the server version