引入依赖
<?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.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ruoyi</groupId>
<artifactId>mcp-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mcp-server</name>
<description>mcp-server</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<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-mcp-server-webflux</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-webmvc-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置下端口号
server.port=9999
写一个配置类 和tool
package com.ruoyi.mcpserver;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
@Service
public class User {
@Tool(description = "获取用户分数")
public Integer getScore(@ToolParam(description = "用户名") String userName) {
if ("zhangsan".equals(userName)) return 11;
if ("lisi".equals(userName)) return 22;
return 0;
}
}
package com.ruoyi.mcpserver;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public ToolCallbackProvider userProvider(User user) {
return MethodToolCallbackProvider.builder().toolObjects(user).build();
}
}
启动项目

服务器端启动成功
maven package 产生jar包
在cmd命令行中java -jar xxx.jar启动

client增加配置
spring.ai.mcp.client.sse.connections.s1.url=http://localhost:9999
重新启动client端
问个server端的问题

查看日志:
2026-01-01T23:25:11.293+08:00 INFO 18824 --- [demo1] [nio-8080-exec-4] c.e.demo.intercepter.GlobalInterceptor : 请求地址:http://localhost:8080/wx
2026-01-01T23:25:11.311+08:00 DEBUG 18824 --- [demo1] [nio-8080-exec-4] o.s.a.c.c.advisor.SimpleLoggerAdvisor : request: ChatClientRequest[prompt=Prompt{messages=[UserMessage{content='what is your name', properties={messageType=USER}, messageType=USER}, AssistantMessage [messageType=ASSISTANT, toolCalls=[], textContent=I am Qwen, a large-scale language model developed by Alibaba Cloud. How can I assist you today?, metadata={role=ASSISTANT, messageType=ASSISTANT, finishReason=STOP, refusal=, index=0, annotations=[], id=chatcmpl-a6366bf2-3050-9bc5-bda9-1bd788e508cf}], UserMessage{content='zhangsan的分数', properties={messageType=USER}, messageType=USER}], modelOptions=OpenAiChatOptions: {"streamUsage":false,"model":"qwen3-max","temperature":0.7}}, context={}]
2026-01-01T23:25:13.035+08:00 DEBUG 18824 --- [demo1] [nio-8080-exec-4] o.s.a.m.tool.DefaultToolCallingManager : Executing tool call: spring_ai_mcp_client_s1_getScore
2026-01-01T23:25:14.418+08:00 DEBUG 18824 --- [demo1] [nio-8080-exec-4] o.s.a.c.c.advisor.SimpleLoggerAdvisor : response: {
服务确实由mcp server提供
成功!