SpringAI 实现 SSE MCP Server项目
阿里云百炼(qwen大模型)https://bailian.console.aliyun.com/
springAI项目 https://start.spring.io/
mcp项目分为server服务端(提供mcp服务),client客户端(去请求mcp服务)
一、创建MCP Server项目
1、pom配置
<?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>3.4.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.mcp.example</groupId> <artifactId>mcp-webflux-server-example</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mcp-webflux-server-example</name> <description>mcp-webflux-server-example</description> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-bom</artifactId> <version>1.0.0-M7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <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-test</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>central</id> <url>https://maven.aliyun.com/repository/central</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <name>Central Portal Snapshots</name> <id>central-portal-snapshots</id> <url>https://central.sonatype.com/repository/maven-snapshots/</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </repository> </repositories> </project>
2、yml配置
spring:
ai:
mcp:
server:
name: webflux-mcp-server
version: 1.0.0
type: ASYNC # Recommended for reactive applications
sse-message-endpoint: /mcp/messages
3、service服务文件
package com.mcp.example; import org.springframework.ai.tool.annotation.Tool; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; /** * 定义时间服务 */ @Service public class DateTimeService { @Tool(description = "Get the current date and time in the user's timezone") String getCurrentDateTime() { return LocalDateTime.now().atZone(LocaleContextHolder.getTimeZone().toZoneId()).toString(); } @Tool(description = "Set a user alarm for the given time, provided in ISO-8601 format") String setAlarm(String time) { LocalDateTime alarmTime = LocalDateTime.parse(time, DateTimeFormatter.ISO_DATE_TIME); return "Alarm set for " + alarmTime; } }
4、provider暴露tools工具
package com.mcp.example; 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; /** * 配置暴露这个tool工具 */ @Configuration public class McpWebFluxServiceExampleConfig { @Bean public ToolCallbackProvider dateTimeTools(DateTimeService dateTimeService) { return MethodToolCallbackProvider.builder().toolObjects(dateTimeService).build(); } }
5、启动服务后控制台
看到注册了2个工具。

二、创建MCP Client项目
1、pom配置
<?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>3.4.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.mcp.example</groupId> <artifactId>mcp-client-example</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mcp-client-example</name> <description>mcp-client-example</description> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </repository> <repository> <name>Central Portal Snapshots</name> <id>central-portal-snapshots</id> <url>https://central.sonatype.com/repository/maven-snapshots/</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </repository> </repositories> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-bom</artifactId> <version>1.0.0-M7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud.ai</groupId> <artifactId>spring-ai-alibaba-starter</artifactId> <version>1.0.0-M6.1</version> </dependency> <!--<dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-openai-spring-boot-starter</artifactId> <version>1.0.0-M6</version> </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-mcp-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、yml配置
spring:
ai:
dashscope:
api-key: sk-xxx
mcp:
client:
sse:
connections:
server1:
url: http://localhost:8080
toolcallback:
enabled: true
server:
port: 8081
3、通过大模型访问mcp
package com.mcp.example; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor; import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor; import org.springframework.ai.chat.memory.InMemoryChatMemory; import org.springframework.ai.tool.ToolCallbackProvider; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/") public class ChatController { private final ChatClient dashScopeChatClient; public ChatController(ChatClient.Builder chatClientBuilder, ToolCallbackProvider tools) { this.dashScopeChatClient = chatClientBuilder .defaultTools(tools) .build(); } @GetMapping("/simple/chat") public String simpleChat(@RequestParam(value = "query", defaultValue = "10分钟后,设置一个闹铃。")String query) { return dashScopeChatClient.prompt(query).call().content(); } }
4、测试不同的请求
先在mcp-server的打断点,看是否会访问到getCurrentDateTIme()方法。
先访问 http://127.0.0.1:8081/simple/chat?query=你是谁
再访问 http://127.0.0.1:8081/simple/chat?query=明天星期几
会发现问题“你是谁”,不会调用getCurrentDateTIme()方法, 而下面涉及到时间有关。则会请求到getCurrentDateTIme()方法。

下面这个是没有使用mcp的大模型项目,可以看到没有给出准确的时间回答。
访问 http://127.0.0.1:18081/simple/chat?query=明天星期几
正常大模型问“明天星期几”不会给出准确的时间。因为它不知道今天是什么时间。


浙公网安备 33010602011771号