MCP WebFlux入门
使用Spring WebFlux和Java构建模型上下文协议(MCP)服务器的入门模板。
🚀 快速开始
- 克隆存储库
git clone https://github.com/TsengX/mcp-webflux-starter.git
cd mcp-webflux-starter- 构建并运行
./mvnw spring-boot:run- 测试服务器
# The server will start on http://localhost:8090
# SSE endpoint: http://localhost:8090/my-weather-server/sse✨ 特性
- 气象局:获取美国各地的天气预报和警报
- SSE 运输:服务器发送事件支持实时通信
- Spring WebFlux 的:响应式、无阻塞的web框架
- Maven构建:标准Maven项目结构
- 准备部署:生产就绪配置
📂 项目结构
mcp-webflux-starter/
├── src/main/java/com/example/mcp/
│ ├── McpServerApplication.java # Main application class
│ ├── WeatherService.java # MCP tools implementation
│ └── McpClientTest.java # Client test example
├── src/main/resources/
│ └── application.properties # Configuration
├── pom.xml # Maven dependencies
└── README.md # This file🛠️ 可用工具
天气工具
getWeatherForecastByLocation-获取特定坐标的天气预报getAlerts-获取美国各州的天气警报
⚙️ 配置
编辑 src/main/resources/application.properties 自定义:
server.port=8090
# Add your custom properties here🔧 发展
添加新工具
- 打开
WeatherService.java - 添加新的工具方法
@McpTool注释 - 执行工具逻辑
- 重新启动服务器
例子:
@McpTool(description = "Your new tool description")
public String yourNewTool(@McpToolParameter(name = "param", description = "Parameter description") String param) {
// Your implementation here
return "result";
}使用MCP客户端进行测试
使用随附的 McpClientTest.java 要测试您的服务器:
String baseUrl = "http://localhost:8090/my-weather-server";
McpSyncClient client = McpClient.sync(HttpClientSseClientTransport.builder(baseUrl)
.sseEndpoint("/my-weather-server").build()).build();
client.initialize();
McpSchema.ListToolsResult tools = client.listTools();📋 需求
- Java 17或更高版本
- Maven 3.6+
- 弹簧靴3.4.5
📜 许可证
MIT许可证-有关详细信息,请参阅许可证文件
🤝 贡献
- 分叉存储库
- 创建功能分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
