Package com.tnsai.mcp.testing
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidafterEach(org.junit.jupiter.api.extension.ExtensionContext context) voidassertToolExists(String toolName) Asserts that a tool with the given name exists on the server.voidassertToolReturns(String toolName, Map<String, Object> args, Predicate<com.fasterxml.jackson.databind.JsonNode> check) Asserts that a tool returns a result matching the given predicate.voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context) Returns the MCP client connected to the mock server.Returns the mock MCP server.Returns the mock transport.
-
Constructor Details
-
McpServerTestKit
public McpServerTestKit()
-
-
Method Details
-
beforeEach
- Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback- Throws:
Exception
-
afterEach
- Specified by:
afterEachin interfaceorg.junit.jupiter.api.extension.AfterEachCallback- Throws:
Exception
-
getServer
Returns the mock MCP server.- Returns:
- the mock server
-
getTransport
Returns the mock transport.- Returns:
- the mock transport
-
getClient
Returns the MCP client connected to the mock server.- Returns:
- the MCP client
-
assertToolExists
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 existIOException- if communication failsMcpClient.McpException
-
assertToolReturns
public void assertToolReturns(String toolName, Map<String, Object> args, Predicate<com.fasterxml.jackson.databind.JsonNode> check) throws IOException, McpClient.McpExceptionAsserts that a tool returns a result matching the given predicate.- Parameters:
toolName- the tool nameargs- the tool argumentscheck- predicate to validate the result- Throws:
AssertionError- if the result does not matchIOException- if communication failsMcpClient.McpException
-