Skip to content

Commit 47030df

Browse files
committed
feat(tests): Add unit tests for MCP server, resources, prompts, and tools
1 parent 9a714e2 commit 47030df

File tree

6 files changed

+141
-0
lines changed

6 files changed

+141
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.github.codeboyzhou.mcp.declarative;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import com.github.codeboyzhou.mcp.declarative.test.TestSimpleMcpStdioServer;
7+
import io.modelcontextprotocol.client.McpClient;
8+
import io.modelcontextprotocol.client.McpSyncClient;
9+
import io.modelcontextprotocol.client.transport.ServerParameters;
10+
import io.modelcontextprotocol.client.transport.StdioClientTransport;
11+
import io.modelcontextprotocol.spec.McpSchema;
12+
import java.util.List;
13+
import org.junit.jupiter.api.Test;
14+
15+
class McpServersTest {
16+
17+
String classpath = System.getProperty("java.class.path");
18+
19+
ServerParameters stdioServerParameters =
20+
ServerParameters.builder("java")
21+
.args("-cp", classpath, TestSimpleMcpStdioServer.class.getName())
22+
.build();
23+
24+
StdioClientTransport stdioClientTransport = new StdioClientTransport(stdioServerParameters);
25+
26+
@Test
27+
void testStartStdioServer_shouldSucceed() {
28+
try (McpSyncClient client = McpClient.sync(stdioClientTransport).build()) {
29+
McpSchema.InitializeResult initialized = client.initialize();
30+
assertEquals("mcp-server", initialized.serverInfo().name());
31+
assertEquals("1.0.0", initialized.serverInfo().version());
32+
assertEquals("test", initialized.instructions());
33+
}
34+
}
35+
36+
@Test
37+
void testStartStdioServer_shouldRegisteredResources() {
38+
try (McpSyncClient client = McpClient.sync(stdioClientTransport).build()) {
39+
client.initialize();
40+
List<McpSchema.Resource> resources = client.listResources().resources();
41+
assertEquals(1, resources.size());
42+
McpSchema.Resource resource = resources.get(0);
43+
assertEquals("test://resource1", resource.uri());
44+
assertEquals("resource1_name", resource.name());
45+
assertEquals("resource1_title", resource.title());
46+
assertEquals("resource1_description", resource.description());
47+
assertEquals("text/plain", resource.mimeType());
48+
}
49+
}
50+
51+
@Test
52+
void testStartStdioServer_shouldRegisteredPrompts() {
53+
try (McpSyncClient client = McpClient.sync(stdioClientTransport).build()) {
54+
client.initialize();
55+
List<McpSchema.Prompt> prompts = client.listPrompts().prompts();
56+
assertEquals(1, prompts.size());
57+
McpSchema.Prompt prompt = prompts.get(0);
58+
assertEquals("prompt1_name", prompt.name());
59+
assertEquals("prompt1_title", prompt.title());
60+
assertEquals("prompt1_description", prompt.description());
61+
assertTrue(prompt.arguments().isEmpty());
62+
}
63+
}
64+
65+
@Test
66+
void testStartStdioServer_shouldRegisteredTools() {
67+
try (McpSyncClient client = McpClient.sync(stdioClientTransport).build()) {
68+
client.initialize();
69+
List<McpSchema.Tool> tools = client.listTools().tools();
70+
assertEquals(1, tools.size());
71+
McpSchema.Tool tool = tools.get(0);
72+
assertEquals("tool1_name", tool.name());
73+
assertEquals("tool1_title", tool.title());
74+
assertEquals("tool1_description", tool.description());
75+
assertTrue(tool.inputSchema().properties().isEmpty());
76+
}
77+
}
78+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.codeboyzhou.mcp.declarative.test;
2+
3+
import com.github.codeboyzhou.mcp.declarative.annotation.McpPrompt;
4+
import com.github.codeboyzhou.mcp.declarative.annotation.McpPrompts;
5+
6+
@McpPrompts
7+
public class TestMcpPrompts {
8+
9+
@McpPrompt(name = "prompt1_name", title = "prompt1_title", description = "prompt1_description")
10+
public String prompt1() {
11+
return "prompt1_content";
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.github.codeboyzhou.mcp.declarative.test;
2+
3+
import com.github.codeboyzhou.mcp.declarative.annotation.McpResource;
4+
import com.github.codeboyzhou.mcp.declarative.annotation.McpResources;
5+
6+
@McpResources
7+
public class TestMcpResources {
8+
9+
@McpResource(
10+
uri = "test://resource1",
11+
name = "resource1_name",
12+
title = "resource1_title",
13+
description = "resource1_description")
14+
public String resource1() {
15+
return "resource1_content";
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.codeboyzhou.mcp.declarative.test;
2+
3+
import com.github.codeboyzhou.mcp.declarative.annotation.McpTool;
4+
import com.github.codeboyzhou.mcp.declarative.annotation.McpTools;
5+
6+
@McpTools
7+
public class TestMcpTools {
8+
9+
@McpTool(name = "tool1_name", title = "tool1_title", description = "tool1_description")
10+
public String tool1() {
11+
return "tool1_content";
12+
}
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.codeboyzhou.mcp.declarative.test;
2+
3+
import com.github.codeboyzhou.mcp.declarative.McpServers;
4+
import com.github.codeboyzhou.mcp.declarative.server.simple.SimpleMcpServerBaseInfo;
5+
import java.time.Duration;
6+
7+
public class TestSimpleMcpStdioServer {
8+
9+
public static void main(String[] args) {
10+
SimpleMcpServerBaseInfo info =
11+
SimpleMcpServerBaseInfo.builder()
12+
.name("mcp-server")
13+
.version("1.0.0")
14+
.instructions("test")
15+
.requestTimeout(Duration.ofSeconds(10))
16+
.build();
17+
McpServers.run(TestSimpleMcpStdioServer.class, args).startStdioServer(info);
18+
}
19+
}

src/test/resources/logback.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<property name="PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"/>
55

66
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
7+
<target>System.err</target>
78
<encoder>
89
<pattern>${PATTERN}</pattern>
910
<charset>UTF-8</charset>

0 commit comments

Comments
 (0)