使用说明
该项目使用以下代码实现了一个简单的计算器服务 模型上下文协议(MCP) 随着 Spring Boot 和 WebFlux 的,通过SSE(服务器发送事件)为MCP工具提供服务。
它旨在对MCP工具注册、服务传输以及与 MCP检查员.
🚀 逐步设置
1.✅ 使用Spring Initializr初始化项目
首选 https://start.spring.io 并填写以下字段:
-
项目:Maven
-
语言Java
-
Spring Boot3.2.5(或更高版本)
-
群组:
com.example -
人工制品:
calculator -
名字:
calculator -
包装:罐子
-
Java版本: 17
-
依赖项:
- Spring响应式Web(WebFlux 的)
- 弹簧靴执行器
点击 生成,解压缩项目,并在IntelliJ IDEA中打开它。
[!注意] Spring Initializr只添加了Maven Central中可用的稳定的官方依赖项。默认情况下,不包括MCP等实验性或早期访问库。
2.✅ 手动添加MCP依赖关系
编辑 pom.xml 文件并添加以下依赖项:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-webflux-spring-boot-starter</artifactId>
<version>1.0.0-M6</version>
</dependency>
[!重要] 这种依赖关系是 未自动包含 在Spring Initializr中,因为它是一个较新项目(Spring AI)的一部分。您必须添加它 手动地 以启用MCP功能。
[!提示] 版本
1.0.0-M6可以直接从Maven Central获得,因此不需要自定义存储库。
添加后, 同步Maven 在IntelliJ中(或运行 ./mvnw clean install).
此外,您必须包含这些依赖关系:
<dependencies>
<!-- Web server and reactive stack -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- MCP WebFlux Server -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-webflux-spring-boot-starter</artifactId>
</dependency>
<!-- Tool annotation support -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-tools</artifactId>
</dependency>
<!-- Optional: for chatbot features -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-client-chat</artifactId>
</dependency>
<!-- Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.✅ 创建配置文件
首选 src/main/resources 并打开(或创建)文件 application.properties,然后粘贴:
spring.application.name=calculator
spring.ai.mcp.server.transport=sse
[!重要] 此消息将告诉Spring Boot启用MCP服务器,并通过SSE在
/sse终点。
[!警告] 如果你忘记了这一点,检查器会连接,但找不到任何工具,或者
/sse将返回404。
4.✅ 创建MCP工具类
在……里面 src/main/java/com/example/calculator/,创建一个名为的文件 CalculatorTool.java 内容如下:
package com.example.calculator;
import org.springframework.ai.mcp.tool.Tool;
import org.springframework.stereotype.Component;
@Component
public class CalculatorTool {
public CalculatorTool() {
System.out.println("✅ CalculatorTool loaded");
}
@Tool
public double add(double a, double b) {
return a + b;
}
@Tool
public double subtract(double a, double b) {
return a - b;
}
@Tool
public double multiply(double a, double b) {
return a * b;
}
@Tool
public double divide(double a, double b) {
if (b == 0) throw new IllegalArgumentException("Cannot divide by zero");
return a / b;
}
}
[!小心] 确保这门课在 相同的包或子包 作为您的主要应用程序类。否则,Spring将无法检测到它。
5.✅ 构建和运行项目
从项目根目录内的终端:
./mvnw clean install -U
./mvnw spring-boot:run
或者:
./mvnw package -DskipTests
java -jar target/calculator-0.0.1-SNAPSHOT.jar
[!提示] 使用
-U强制Maven更新依赖关系,以防它之前缓存了失败的查找。
🛠️ 使用MCP检查器
1.安装并启动检查器
npx @modelcontextprotocol/inspector
2.复制代理会话令牌
在Inspector开始的终端中,查找以下行:
Proxy Session Token: abcd1234-efgh5678-ijkl9012
复制该令牌。
[!重要] 如果不使用运行时提供的确切会话令牌,检查器将连接,但返回错误或表示没有可用的工具。
3.配置检查器UI
在浏览器中(通常在 http://localhost:6274):
-
运输类型:
SSE -
网址:
http://localhost:8080/sse -
打开
Authentication章节- 代理会话令牌:粘贴您复制的令牌
- 承载令牌:留空
-
点击 连接
[!小心] 不要将代理会话令牌留空,即使是在本地,否则检查器将无法列出您的工具。
4.运行工具
- 点击 列出工具
- 选择一种方法,如下所示
calculatorTool.add - 提供输入JSON:
{
"a": 5,
"b": 3
}
- 点击 运行工具
你应该得到:
{
"result": 8
}
[!注意] 工具名称基于驼峰大小写中的Java类名(
CalculatorTool成为calculatorTool).
💡 注意事项、提示和注意事项
🛠️ 调试:
-
如果Inspector已连接,但未列出任何工具:
- 确保您的工具类带有注释
@Component - 确保方法带有注释
@Tool - 确保Spring Boot扫描了包
- 确保您的工具类带有注释
-
如果
/sse给出404:- 检查
application.properties包含spring.ai.mcp.server.transport=sse
- 检查
[!警告] 请勿重命名或移动
CalculatorTool.java在你的基本套餐之外。春天不会扫描它。
🧪 手动测试终点:
curl http://localhost:8080/sse
✅ 开发技巧:
在工具类构造函数中打印一行以验证其是否已加载:
System.out.println("✅ CalculatorTool loaded");
