Class McpServerTestKit

java.lang.Object
com.tnsai.mcp.testing.McpServerTestKit
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension

public class McpServerTestKit extends Object implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
JUnit 5 extension for easy MCP testing.

Sets up a MockMcpServer, MockMcpTransport, and McpClient for each test. Also provides assertion helpers for common MCP testing patterns.

Usage

 @RegisterExtension
 McpServerTestKit testKit = new McpServerTestKit();

 @Test
 void testToolExecution() throws Exception {
     testKit.getServer().whenToolReturns("echo", resultNode);
     testKit.assertToolExists("echo");
     testKit.assertToolReturns("echo", Map.of("msg", "hi"), result -> result.has("text"));
 }
 
See Also:
  • Constructor Details

    • McpServerTestKit

      public McpServerTestKit()
  • Method Details

    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
      Throws:
      Exception
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      Throws:
      Exception
    • getServer

      public MockMcpServer getServer()
      Returns the mock MCP server.
      Returns:
      the mock server
    • getTransport

      public MockMcpTransport getTransport()
      Returns the mock transport.
      Returns:
      the mock transport
    • getClient

      public McpClient getClient()
      Returns the MCP client connected to the mock server.
      Returns:
      the MCP client
    • assertToolExists

      public void assertToolExists(String toolName) throws IOException, McpClient.McpException
      Asserts that a tool with the given name exists on the server.
      Parameters:
      toolName - the tool name to check
      Throws:
      AssertionError - if the tool does not exist
      IOException - if communication fails
      McpClient.McpException
    • assertToolReturns

      public void assertToolReturns(String toolName, Map<String,Object> args, Predicate<com.fasterxml.jackson.databind.JsonNode> check) throws IOException, McpClient.McpException
      Asserts that a tool returns a result matching the given predicate.
      Parameters:
      toolName - the tool name
      args - the tool arguments
      check - predicate to validate the result
      Throws:
      AssertionError - if the result does not match
      IOException - if communication fails
      McpClient.McpException