Spring AI MCP Server 基于 Model Context Protocol 标准,为 Java 应用提供了大模型工具调用的标准化能力。通过依赖配置、工具定义、客户端调用及核心源码分析,展示了如何在 Spring Boot 中快速集成 MCP Server。重点讲解了 WebMvc/WebFlux SSE 传输实现、自动装配机制以及 ToolCallback 注册流程,帮助开发者理解底层协议交互逻辑并落地实践。
月亮邮递员1 浏览
引言
Model Context Protocol (MCP) 为 LLM 应用提供了标准化的工具调用接口。Spring AI 在此基础上扩展了 Java SDK,并通过 Spring Boot Starter 简化了集成过程。本文将结合依赖配置、服务定义、客户端调用及核心源码,梳理 Spring AI MCP Server 的实现细节。
依赖引入
MCP 官方提供了 Java SDK,支持 Spring WebFlux 和 WebMvc 的 SSE 传输实现。在 Spring AI 生态中,我们需要引入对应的 Starter 依赖。
<dependencyManagement><dependencies><dependency><groupId>io.modelcontextprotocol.sdk</groupId><artifactId>mcp-bom</artifactId><version>0.8.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependency><groupId>io.modelcontextprotocol.sdk</groupId><artifactId>mcp</artifactId></dependency><!-- Spring WebFlux-based SSE client and server transport --><dependency><groupId>io.modelcontextprotocol.sdk
mcp-spring-webflux
io.modelcontextprotocol.sdk
mcp-spring-webmvc
16:18:34.707 [HttpClient-1-Worker-0] INFO io.modelcontextprotocol.client.McpAsyncClient--Server response with Protocol: 2024-11-05, Capabilities: ServerCapabilities[experimental=null, logging=LoggingCapabilities[], prompts=null, resources=null, tools=ToolCapabilities[listChanged=true]], Info: Implementation[name=my-weather-server, version=0.0.1] and Instructions null
AvailableTools=ListToolsResult[tools=[Tool[name=toUpperCase, description=Put the text to upper case, inputSchema=JsonSchema[type=object, properties={input={type=string}}, required=[input], additionalProperties=false]], Tool[name=getAlerts, description=Get weather alerts for a US state. InputisTwo-letter US state code (e.g. CA, NY), inputSchema=JsonSchema[type=object, properties={state={type=string}}, required=[state], additionalProperties=false]], Tool[name=getWeatherForecastByLocation, description=Get weather forecast for a specific latitude/longitude, inputSchema=JsonSchema[type=object, properties={latitude={type=number, format=double}, longitude={type=number, format=double}}, required=[latitude, longitude], additionalProperties=false]]], nextCursor=null]
WeatherForcast: CallToolResult[content=[TextContent[audience=null, priority=null, text="Overnight:\nTemperature: 50 F\nWind: 5 mph N\nForecast: Mostly cloudy, with a low around 50. North wind around 5 mph.\nWednesday:\nTemperature: 70 F\nWind: 2 to 7 mph NW\nForecast: A chance of rain showers between 9am and 5pm, then showers and thunderstorms. Some of the storms could be severe. Mostly cloudy. High near 70, with temperatures falling to around 68 in the afternoon. Northwest wind 2 to 7 mph. Chance of precipitation is 100%. New rainfall amounts between a quarter and half of an inch possible.\nWednesday Night:\nTemperature: 48 F\nWind: 7 to 10 mph SSW\nForecast: Showers and thunderstorms before 5am, then rain. Some of the storms could be severe. Cloudy. Low around 48, with temperatures rising to around 50 overnight. South southwest wind 7 to 10 mph, with gusts as high as 21 mph. Chance of precipitation is 100%. New rainfall amounts between a quarter and half of an inch possible.\nThursday:\nTemperature: 59 F\nWind: 9 mph S\nForecast: Rain. Cloudy, with a high near 59. South wind around 9 mph, with gusts as high as 21 mph. Chance of precipitation is 90%. New rainfall amounts between a tenth and quarter of an inch possible.\nThursday Night:\nTemperature: 47 F\nWind: 9 mph S\nForecast: Rain. Cloudy, with a low around 47. South wind around 9 mph, with gusts as high as 22 mph. Chance of precipitation is 90%. New rainfall amounts between a quarter and half of an inch possible.\nFriday:\nTemperature: 55 F\nWind: 9 to 13 mph S\nForecast: Rain. Cloudy, with a high near 55. Chance of precipitation is 100%. New rainfall amounts between a tenth and quarter of an inch possible.\nFriday Night:\nTemperature: 44 F\nWind: 6 to 10 mph S\nForecast: Rain. Mostly cloudy, with a low around 44. Chance of precipitation is 80%.\nSaturday:\nTemperature: 55 F\nWind: 7 mph S\nForecast: Rain likely. Partly sunny, with a high near 55.Saturday Night:Temperature: 41 FWind: 2 to 6 mph SEForecast: A chance of rain before 11pm. Mostly cloudy, with a low around 41.Sunday:Temperature: 59 FWind: 2 to 6 mph NNEForecast: A chance of rain after 11am. Mostly cloudy, with a high near 59.Sunday Night:Temperature: 45 FWind: 6 mph ESEForecast: Rain likely. Mostly cloudy, with a low around 45.Monday:Temperature: 54 FWind: 5 to 8 mph SForecast: Rain. Mostly cloudy, with a high near 54.Monday Night:Temperature: 42 FWind: 5 to 8 mph SForecast: Rain likely. Mostly cloudy, with a low around 42.Tuesday:Temperature: 54 FWind: 7 mph SSWForecast: Rain likely. Mostly cloudy, with a high near 54."]], isError=false]
AlertResponse=CallToolResult[content=[TextContent[audience=null, priority=null,]], isError=false]
核心源码解析
了解底层实现有助于排查问题。Spring AI MCP Server 的核心在于自动配置类与传输层的对接。