idea:2025OS:win11JDK:17Maven:3.8.5使用springboot AI实现一个伪查询天气的MCP Server协议是SSE。代码结构McpDemoHttpSseApplication.javapackage com.david.mcpdemohttpsse; import org.springframework.ai.tool.ToolCallbackProvider; import org.springframework.ai.tool.method.MethodToolCallbackProvider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; SpringBootApplication public class McpDemoHttpSseApplication { public static void main(String[] args) { SpringApplication.run(McpDemoHttpSseApplication.class, args); } Bean public ToolCallbackProvider toolCallbackProvider(WeatherService weatherService) { return MethodToolCallbackProvider.builder().toolObjects(weatherService).build(); } }WeatherService.javapackage com.david.mcpdemohttpsse; import org.springframework.ai.tool.annotation.Tool; import org.springframework.ai.tool.annotation.ToolParam; import org.springframework.stereotype.Service; import java.util.concurrent.ThreadLocalRandom; Service public class WeatherService { Tool(name weather_query, description 根据位置返回该位置的天气信息) public String weatherQuery(ToolParam(description 位置, required true) String location) { // 天气、温度 随机生成 int temperature ThreadLocalRandom.current().nextInt(10, 40); String[] weathers {晴, 多云, 阴, 小雨, 中雨, 大雨, 雨夹雪, 大雪}; String weatherText weathers[ThreadLocalRandom.current().nextInt(weathers.length)]; return location%s,temperature%d°C,weatherText%s.formatted(location, temperature, weatherText); } }application.ymlserver: port: 8081 spring: application: name: mcp-demo-http-sse ai: mcp: server: name: weather-mcp-server # 下面两个地址是默认值 sse-endpoint: /sse sse-message-endpoint: /mcp/message logging: level: root: INFO io.modelcontextprotocol: DEBUG com.david.mcpdemohttpsse: DEBUGpom.xml?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.4.2/version relativePath/ /parent groupIdcom.david/groupId artifactIdmcp-demo-http-sse/artifactId version0.0.1-SNAPSHOT/version namemcp-demo-http-sse/name descriptionmcp-demo-http-sse/description url/ licenses license/ /licenses developers developer/ /developers scm connection/ developerConnection/ tag/ url/ /scm properties java.version17/java.version spring-ai.version1.0.0-M7/spring-ai.version /properties dependencies dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-mcp-server/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-mcp-server-webmvc/artifactId /dependency /dependencies dependencyManagement dependencies dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-bom/artifactId version${spring-ai.version}/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement build plugins plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId /plugin /plugins /build /projectCherryStudio 配置MCP server点保存后再打开开关如果失败了开关是无法开启的我再启用过程成就遇到了一直超时重启下CherryStudio就好了配置聊天MCP工具助手下面小锤子的图标选择“手动”然后选择对应的mcp大面有绿色的小勾才选是选上了发送询问天气同时再代码里打个断点进入到断点了放开断点回到CherryStudio查看聊天结果