MCP春季启动服务器
   
模型上下文协议(MCP)的Spring Boot服务器实现,通过MCP接口提供天气信息服务。
特性
- 通过MCP提供天气信息服务
- RESTful API端点
- 服务器发送事件(SSE)支持
- 自动刀具注册
- Spring Boot 3.x集成
需求
- JDK 17或更高版本
- Maven 3.6.x或更高版本
- 弹簧靴3.2.0或更高版本
入门指南
克隆存储库
git clone https://github.com/yourusername/mcp-springboot-server.git
cd mcp-springboot-server构建项目
mvn clean install运行应用程序
mvn spring-boot:run默认情况下,服务器将在端口8080上启动。
配置
主要配置属性位于 application.properties:
server.port=8080
spring.application.name=mcp-demo
spring.ai.mcp.server.enabled=true有关更多配置选项,请查看 application.properties 文件。
API文档
气象局
气象局提供以下MCP方法:
getWeather(String cityName):获取特定城市的天气信息getWeather1(String cityName):获取天气信息的替代方法
测试客户端中的示例用法:
var transport = new HttpClientSseClientTransport("http://localhost:8080");
var client = McpClient.sync(transport).build();项目结构
src/
├── main/
│ ├── java/
│ │ └── com/unionhole/mcpserver/
│ │ ├── config/
│ │ ├── service/
│ │ └── McpDemoApplication.java
│ └── resources/
│ └── application.properties
└── test/
└── java/
└── com/unionhole/mcpserver/
└── ClientSseTest.java贡献
- 分叉存储库
- 创建功能分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add some amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
许可证
此项目根据Apache许可证2.0获得许可-请参阅 许可证 文件以获取详细信息。
作者
邹
- 电子邮件: 18301545237@163.com
- github: @詹姆斯祖
致谢
- 春靴团队
- 模型上下文协议团队
- 此项目的所有贡献者
框架说明
1. Maven 依赖配置
项目使用了以下主要依赖:
org.springframework.ai
spring-ai-core
${spring-ai.version}
org.springframework.ai
spring-ai-starter-mcp-server-webmvc
${spring-ai.version}
com.unionhole
mcp-facade-generator
${mcp-facade.version}
2. MCP 配置说明
在 application.properties 中配置 MCP 服务器:
# MCP 服务器配置
spring.ai.mcp.server.enabled=true
spring.ai.mcp.server.resource-change-notification=true
spring.ai.mcp.server.prompt-change-notification=true
spring.ai.mcp.server.tool-change-notification=true
spring.ai.mcp.server.name=mcp-demo-service
spring.ai.mcp.server.version=1.0.0
spring.ai.mcp.server.type=SYNC
spring.ai.mcp.server.sse-message-endpoint=/mcp/messages3. MCP Tools 配置
通过 McpServerConfig 类配置 MCP Tools:
@Configuration
public class McpServerConfig {
@Bean
public ToolCallbackProvider autoRegisterTools(ApplicationContext applicationContext) {
// 获取所有带有 @Component 注解且类名以 Facade 结尾的 bean
String[] beanNames = applicationContext.getBeanNamesForAnnotation(Component.class);
List facadeBeans = new ArrayList<>();
for (String beanName : beanNames) {
if (beanName.endsWith("Facade")) {
facadeBeans.add(applicationContext.getBean(beanName));
}
}
return MethodToolCallbackProvider.builder()
.toolObjects(facadeBeans.toArray())
.build();
}
}4. 业务服务开发
- 创建服务类并添加
@MCPService注解:
@MCPService
@Service
public class WeatherService {
public String getWeather(String cityName) {
// 实现业务逻辑
return "Weather info for " + cityName;
}
}- 使用
@MCPMethod注解标记需要暴露的方法:
@MCPMethod(description = "获取天气信息")
public String getWeather(String cityName) {
// 方法实现
}5. 客户端测试
使用提供的 ClientSseTest 类进行测试:
public class ClientSseTest {
public static void main(String[] args) {
var transport = new HttpClientSseClientTransport("http://localhost:8080");
var client = McpClient.sync(transport).build();
try {
client.initialize();
client.ping();
// 列出可用的工具
McpSchema.ListToolsResult toolsList = client.listTools();
System.out.println("Available Tools = " + toolsList);
client.closeGracefully();
} catch (Exception e) {
e.printStackTrace();
}
}
}注意事项
- 确保正确配置了 Spring AI 和 MCP 的版本
- 所有 Facade 类都应该使用
@Component注解 - 建议使用
@MCPMethod注解来提供方法的描述信息 - 异常处理应该在服务层统一处理
常见问题解决
1. 编译时出现 IllegalArgumentException
如果在编译过程中遇到以下错误:
java: java.lang.IllegalArgumentException解决方案:
在 IntelliJ IDEA 中添加 VM 选项:
- 打开 设置(Ctrl+Alt+S)
- 导航到 构建、执行、部署>编译器
- 在 "Build process VM options" 字段中添加:
-Djps.track.ap.dependencies=false- 点击 Apply 和 OK
- 重新构建项目
参考文档
______________________________________________________________________
版权所有©2024邹。保留所有权利。
