搭建mcp_server与使有client进行单元测试
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>4.0.2</version> <relativePath/> </parent> <groupId>com.chinaunicom.unicmp</groupId> <artifactId>mcp_server_4j_test</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mcp_server_4j_test</name> <description>mcp_server_4j_test</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>17</java.version> <spring-ai.version>2.0.0-M2</spring-ai.version> </properties> <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-mcp-client</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-model-ollama</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-bom</artifactId> <version>${spring-ai.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
|
application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| spring: application: name: mcp_server_4j_test
ai: mcp: server: enabled: true client: http: base-url: http://localhost:8080/sse ollama: base-url: http://localhost:11434 chat: model: deepseek-r1:8b
|
application.java
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.chinaunicom.unicmp.mcp.server.test;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class McpServer4jTestApplication {
public static void main(String[] args) { SpringApplication.run(McpServer4jTestApplication.class, args); }
}
|
tool
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.chinaunicom.unicmp.mcp.server.test.tool;
import org.springframework.ai.chat.client.ChatClient; import org.springframework.stereotype.Component;
@Component public class ChatTool {
private final ChatClient chatClient;
public ChatTool(ChatClient.Builder chatClientBuilder) { this.chatClient = chatClientBuilder.build(); }
public String chat(String message) { return chatClient.prompt() .user(message) .call() .content(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.chinaunicom.unicmp.mcp.server.test.tool;
import org.springaicommunity.mcp.annotation.McpTool;
public class MathTool {
@McpTool(description = "两个数字相加") public static int addNumbers(int a, int b) { System.out.println("正在执行addNumbers工具..."); return a + b; }
@McpTool(description = "两个数字相减") public static int subtractNumbers(int a, int b) { return a - b; }
}
|
config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.chinaunicom.unicmp.mcp.server.test.config;
import com.chinaunicom.unicmp.mcp.server.test.tool.MathTool; import org.springframework.ai.ollama.api.OllamaChatOptions; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class McpConfig {
@Bean public MathTool mathTool() { return new MathTool(); }
@Bean public OllamaChatOptions ollamaChatOptions() { return OllamaChatOptions.builder().build(); }
}
|
service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package com.chinaunicom.unicmp.mcp.server.test.service;
import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.model.ChatModel; import org.springframework.ai.mcp.SyncMcpToolCallbackProvider; import org.springframework.ai.ollama.api.OllamaChatOptions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service;
@Service public class McpClientExample {
private final ChatClient chatClient; private final SyncMcpToolCallbackProvider mcpToolCallbackProvider; private final String ollamaModel;
@Autowired private OllamaChatOptions ollamaChatOptions;
@Autowired public McpClientExample(ChatClient.Builder chatClientBuilder, SyncMcpToolCallbackProvider mcpToolCallbackProvider, ChatModel chatModel, @Value("${spring.ai.ollama.chat.model}") String ollamaModel) { this.chatClient = ChatClient.builder(chatModel) .defaultToolCallbacks(mcpToolCallbackProvider.getToolCallbacks()) .build(); this.mcpToolCallbackProvider = mcpToolCallbackProvider; this.ollamaModel = ollamaModel; }
public String chatWithMcpTools(String prompt) { return chatClient.prompt() .user(prompt) .options(ollamaChatOptions) .call() .content(); }
public Object[] getAvailableTools() { return mcpToolCallbackProvider.getToolCallbacks(); } }
|
test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package com.chinaunicom.unicmp.mcp.server.test;
import com.chinaunicom.unicmp.mcp.server.test.service.McpClientExample; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest public class McpClientTest {
@Autowired private McpClientExample mcpClientExample;
@Test public void testListAvailableTools() { var tools = mcpClientExample.getAvailableTools(); System.out.println("Available MCP Tools:"); for (var tool : tools) { System.out.println(" - " + tool); } }
@Test public void testChatWithMcpTools() { String response = mcpClientExample.chatWithMcpTools("请计算 10 加 5 的结果"); System.out.println("Response: " + response); }
@Test public void testChatWithSubtract() { String response = mcpClientExample.chatWithMcpTools("请计算 20 减去 8 的结果"); System.out.println("Response: " + response); }
@Test public void testChatWithChat() { String response = mcpClientExample.chatWithMcpTools("你是谁"); System.out.println("Response: " + response); }
}
|
cherry studio 测试mcp
安装 cherry studio 后,点击 设置-》MCP 服务器-》点击感叹号 -》安装 UV 与 Bun功具
点击添加mcp 服务器:
填写参数:
url 不能配置成 localhost,我配置成 localhost 一直不能连接。
启动着 mcp_server 后,点击开关按钮,可以开说明连接成功。
如果开启后,再进入 mcp_server 页面可以看到工具标签,就会发现两个工具。
在 聊天助手中输入 “10+5等于多少”,这时聊天框中就会发现使用着工具,mcp_server 中对应方法中也会有日志打印。
其他mcpserver
也可以找网上的 mcp server 进行测试。
https://mcp.so/