‼️IMPORTANT‼️
该项目的代码源为: spring ai示例| starter stdio服务器, 开源许可证遵循原始项目。
本项目代码来源:spring ai示例| starter stdio服务器, 开源许可证遵循原始项目
春季AI MCP天气STDIO服务器
一个Spring Boot启动项目演示如何构建一个模型上下文协议(MCP)服务器,该服务器使用美国国家气象局(weather.gov)API提供与天气相关的工具。该项目展示了Spring AI MCP Server Boot Starter的STDIO传输实现功能。
有关更多信息,请参阅 MCP服务器启动启动器 参考文件。
先决条件
- Java 17或更高版本
- Maven 3.6或更高版本
- 理解Spring Boot和Spring AI概念
- (可选)用于AI助手集成的Claude Desktop
关于Spring AI MCP服务器启动启动器
这 spring-ai-mcp-server-spring-boot-starter 提供:
- MCP服务器组件的自动配置
- 支持同步和异步操作模式
- STDIO传输层实现
- 通过Spring beans实现灵活的工具注册
- 更改通知功能
项目结构
src/
├── main/
│ ├── java/
│ │ └── org/springframework/ai/mcp/sample/server/
│ │ ├── McpServerApplication.java # Main application class with tool registration
│ │ └── WeatherService.java # Weather service implementation with MCP tools
│ └── resources/
│ └── application.properties # Server and transport configuration
└── test/
└── java/
└── org/springframework/ai/mcp/sample/client/
└── ClientStdio.java # Test client implementation建立和运行
服务器使用STDIO传输模式,通常由客户端自动启动。要构建服务器jar:
./mvnw clean install -DskipTests工具实施
该项目演示了如何使用Spring的依赖注入和自动配置来实现和注册MCP工具:
@Service
public class WeatherService {
@Tool(description = "Get weather forecast for a specific latitude/longitude")
public String getWeatherForecastByLocation(
double latitude, // Latitude coordinate
double longitude // Longitude coordinate
) {
// Implementation
}
@Tool(description = "Get weather alerts for a US state")
public String getAlerts(
String state // Two-letter US state code (e.g., CA, NY)
) {
// Implementation
}
}
@SpringBootApplication
public class McpServerApplication {
@Bean
public List weatherTools(WeatherService weatherService) {
return ToolCallbacks.from(weatherService);
}
}自动配置会自动将这些工具注册到MCP服务器。您可以让多个bean生成Toolcallback列表,自动配置将合并它们。
可用工具
1.天气预报工具
@Tool(description = "Get weather forecast for a specific latitude/longitude")
public String getWeatherForecastByLocation(
double latitude, // Latitude coordinate
double longitude // Longitude coordinate
) {
// Returns detailed forecast including:
// - Temperature and unit
// - Wind speed and direction
// - Detailed forecast description
}
// Example usage:
CallToolResult forecast = client.callTool(
new CallToolRequest("getWeatherForecastByLocation",
Map.of(
"latitude", 47.6062, // Seattle coordinates
"longitude", -122.3321
)
)
);2.天气警报工具
@Tool(description = "Get weather alerts for a US state")
public String getAlerts(
String state // Two-letter US state code (e.g., CA, NY)
) {
// Returns active alerts including:
// - Event type
// - Affected area
// - Severity
// - Description
// - Safety instructions
}
// Example usage:
CallToolResult alerts = client.callTool(
new CallToolRequest("getAlerts",
Map.of("state", "NY")
)
);客户端集成
Java客户端示例
手动创建MCP客户端
// Create server parameters
ServerParameters stdioParams = ServerParameters.builder("java")
.args("-Dspring.ai.mcp.server.transport=STDIO",
"-Dspring.main.web-application-type=none",
"-Dlogging.pattern.console=",
"-jar",
"target/mcp-weather-stdio-server-0.0.1-SNAPSHOT.jar")
.build();
// Initialize transport and client
var transport = new StdioClientTransport(stdioParams);
var client = McpClient.sync(transport).build();这 ClientStdio.java 演示如何手动实现MCP客户端。
为了获得更好的开发体验,请考虑使用 MCP客户端启动程序这些启动器能够自动配置到MCP服务器的多个STDIO和/或SSE连接。请参阅 启动器默认客户端 和 starter webflux客户端 例如,项目。
使用MCP客户端引导启动器
使用 启动器默认客户端 与天气联系 starter-stdio-server:
- 跟随
starter-default-client自述构建说明mcp-starter-default-client-0.0.1-SNAPSHOT.jar客户端应用程序。
- 使用配置文件运行客户端:
java -Dspring.ai.mcp.client.stdio.connections.server1.command=java \
-Dspring.ai.mcp.client.stdio.connections.server1.args=-jar,/Users/christiantzolov/Dev/projects/spring-ai-examples/model-context-protocol/weather/starter-stdio-server/target/mcp-weather-stdio-server-0.0.1-SNAPSHOT.jar \
-Dai.user.input='What is the weather in NY?' \
-Dlogging.pattern.console= \
-jar mcp-starter-default-client-0.0.1-SNAPSHOT.jarClaude桌面集成
要与Claude Desktop集成,请将以下配置添加到您的Claude Desktop设置中:
{
"mcpServers": {
"spring-ai-mcp-weather": {
"command": "java",
"args": [
"-Dspring.ai.mcp.server.stdio=true",
"-Dspring.main.web-application-type=none",
"-Dlogging.pattern.console=",
"-jar",
"/absolute/path/to/mcp-weather-stdio-server-0.0.1-SNAPSHOT.jar"
]
}
}
}替换 /absolute/path/to/ 使用构建的jar文件的实际路径。
配置
应用程序属性
所有属性都以前缀 spring.ai.mcp.server:
# Required STDIO Configuration
spring.main.web-application-type=none
spring.main.banner-mode=off
logging.pattern.console=
# Server Configuration
spring.ai.mcp.server.enabled=true
spring.ai.mcp.server.name=my-weather-server
spring.ai.mcp.server.version=0.0.1
# SYNC or ASYNC
spring.ai.mcp.server.type=SYNC
spring.ai.mcp.server.resource-change-notification=true
spring.ai.mcp.server.tool-change-notification=true
spring.ai.mcp.server.prompt-change-notification=true
# Optional file logging
logging.file.name=mcp-weather-stdio-server.log关键配置说明
- STDIO模式要求
- 禁用web应用程序类型(spring.main.web-application-type=none) - 禁用Spring横幅(spring.main.banner-mode=off) - 清除控制台日志模式(logging.pattern.console=)
- 服务器类型
- SYNC (默认):使用 McpSyncServer 用于简单的请求-响应模式 - ASYNC:用途 McpAsyncServer 用于项目反应堆支持下的非阻塞运行
