使用说明
使用WebFlux Starter的Spring AI MCP天气服务器示例
此示例项目演示了如何使用带有WebFlux传输的Spring AI MCP server Boot Starter创建MCP服务器。它实现了一个天气服务,该服务公开了使用国家气象局API检索天气信息的工具。
有关更多信息,请参阅 MCP服务器启动启动器 参考文件。
概述
样品展示了:
- 与集成
spring-ai-mcp-server-webflux-spring-boot-starter - 支持SSE(服务器发送事件)和STDIO传输
- 使用Spring AI的自动工具注册
@Tool注释 - 两个与天气相关的工具:
- 按位置(纬度/经度)获取天气预报
- 获取美国各州的天气警报
依赖项
该项目需要Spring AI MCP服务器WebFlux引导启动器:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-webflux-spring-boot-starter</artifactId>
</dependency>
此启动器提供:
- 使用Spring WebFlux的响应式传输(
WebFluxSseServerTransport) - 自动配置的反应式SSE端点
- 可选STDIO传输
- 包含
spring-boot-starter-webflux和mcp-spring-webflux依赖项
建设项目
使用Maven构建项目:
./mvnw clean install -DskipTests
运行服务器
服务器支持两种传输模式:
WebFlux SSE模式(默认)
java -jar target/mcp-weather-starter-webflux-server-0.0.1-SNAPSHOT.jar
STDIO模式
要启用STDIO传输,请设置相应的属性:
java -Dspring.ai.mcp.server.stdio=true -Dspring.main.web-application-type=none -jar target/mcp-weather-starter-webflux-server-0.0.1-SNAPSHOT.jar
配置
通过配置服务器 application.properties:
# Server identification
spring.ai.mcp.server.name=my-weather-server
spring.ai.mcp.server.version=0.0.1
# Server type (SYNC/ASYNC)
spring.ai.mcp.server.type=SYNC
# Transport configuration
spring.ai.mcp.server.stdio=false
spring.ai.mcp.server.sse-message-endpoint=/mcp/message
# Change notifications
spring.ai.mcp.server.resource-change-notification=true
spring.ai.mcp.server.tool-change-notification=true
spring.ai.mcp.server.prompt-change-notification=true
# Logging (required for STDIO transport)
spring.main.banner-mode=off
logging.file.name=./target/starter-webflux-server.log
可用工具
天气预报工具
- 姓名:
getWeatherForecastByLocation - 描述:获取特定纬度/经度的天气预报
- 参数:
latitude:双纬度坐标longitude:双经度坐标
- 例子:
CallToolResult forecastResult = client.callTool(new CallToolRequest("getWeatherForecastByLocation",
Map.of("latitude", 47.6062, "longitude", -122.3321)));
天气警报工具
- 姓名:
getAlerts - 描述:获取美国某个州的天气警报
- 参数:
state:String-两个字母的美国州代码(例如,CA、NY)
- 例子:
CallToolResult alertResult = client.callTool(new CallToolRequest("getAlerts",
Map.of("state", "NY")));
服务器实现
服务器使用Spring Boot和Spring AI的工具注释进行自动工具注册:
@SpringBootApplication
public class McpServerApplication {
public static void main(String[] args) {
SpringApplication.run(McpServerApplication.class, args);
}
@Bean
public List<ToolCallback> weatherTools(WeatherService weatherService) {
return List.of(ToolCallbacks.from(weatherService));
}
}
这 WeatherService 使用 @Tool 注释:
@Service
public class WeatherService {
@Tool(description = "Get weather forecast for a specific latitude/longitude")
public String getWeatherForecastByLocation(double latitude, double longitude) {
// Implementation using weather.gov API
}
@Tool(description = "Get weather alerts for a US state. Input is Two-letter US state code (e.g., CA, NY)")
public String getAlerts(String state) {
// Implementation using weather.gov API
}
}
MCP客户端
您可以使用STDIO或SSE传输连接到天气服务器:
手动客户端
WebFlux SSE客户端
对于使用SSE传输的服务器:
var transport = new WebFluxSseClientTransport(WebClient.builder().baseUrl("http://localhost:8080"));
var client = McpClient.sync(transport).build();
STDIO客户端
对于使用STDIO传输的服务器:
var stdioParams = ServerParameters.builder("java")
.args("-Dspring.ai.mcp.server.stdio=true",
"-Dspring.main.web-application-type=none",
"-Dspring.main.banner-mode=off",
"-Dlogging.pattern.console=",
"-jar",
"target/mcp-weather-starter-webflux-server-0.0.1-SNAPSHOT.jar")
.build();
var transport = new StdioClientTransport(stdioParams);
var client = McpClient.sync(transport).build();
示例项目包括示例客户端实现:
- SampleClient.java:手动MCP客户端实施
- 客户-dio.ava:STDIO传输连接
- ClientSse.java:SSE传输连接
为了获得更好的开发体验,请考虑使用 MCP客户端启动程序这些启动器能够自动配置到MCP服务器的多个STDIO和/或SSE连接。看 启动器默认客户端 和 starter webflux客户端 例如,项目。
启动启动器客户端
让我们使用 starter webflux客户端 客户端连接到我们的天气 starter-webflux-server.
跟随 starter-webflux-client readme构建说明 mcp-starter-webflux-client-0.0.1-SNAPSHOT.jar 客户端应用程序。
STDIO传输
- 创建一个
mcp-servers-config.json包含以下内容的配置文件:
{
"mcpServers": {
"weather-starter-webflux-server": {
"command": "java",
"args": [
"-Dspring.ai.mcp.server.stdio=true",
"-Dspring.main.web-application-type=none",
"-Dlogging.pattern.console=",
"-jar",
"/absolute/path/to/mcp-weather-starter-webflux-server-0.0.1-SNAPSHOT.jar"
]
}
}
}
- 使用配置文件运行客户端:
java -Dspring.ai.mcp.client.stdio.servers-configuration=file:mcp-servers-config.json \
-Dai.user.input='What is the weather in NY?' \
-Dlogging.pattern.console= \
-jar mcp-starter-webflux-client-0.0.1-SNAPSHOT.jar
SSE(WebFlux)传输
- 启动
mcp-weather-starter-webflux-server:
java -jar mcp-weather-starter-webflux-server-0.0.1-SNAPSHOT.jar
在端口8080上启动MCP服务器。
- 在另一个控制台中启动配置了SSE传输的客户端:
java -Dspring.ai.mcp.client.sse.connections.weather-server.url=http://localhost:8080 \
-Dlogging.pattern.console= \
-Dai.user.input='What is the weather in NY?' \
-jar mcp-starter-webflux-client-0.0.1-SNAPSHOT.jar
