搭建spring-ai的mcp服务,引用官网的包
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>org.my.mcp.tools</groupId> 8 <artifactId>mcp_tools</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <properties> 12 <java.version>21</java.version> 13 <spring-boot.version>3.2.5</spring-boot.version> 14 <maven.compiler.source>21</maven.compiler.source> 15 <maven.compiler.target>21</maven.compiler.target> 16 </properties> 17 <dependencies> 18 19 <dependency> 20 <groupId>org.springframework.ai</groupId> 21 <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId> 22 <version>2.0.0</version> 23 </dependency> 24 25 </dependencies> 26 27</project>
启动类:
1package org.my.mcp.tools; 2 3import org.my.mcp.tools.service.WeatherService; 4import org.slf4j.Logger; 5import org.slf4j.LoggerFactory; 6import org.springframework.ai.tool.ToolCallbackProvider; 7import org.springframework.ai.tool.method.MethodToolCallbackProvider; 8import org.springframework.boot.SpringApplication; 9import org.springframework.boot.autoconfigure.SpringBootApplication; 10import org.springframework.context.annotation.Bean; 11 12 13@SpringBootApplication 14public class ServerStart { 15 private static final Logger logger = LoggerFactory.getLogger(ServerStart.class); 16 17 public static void main(String[] args) { 18 SpringApplication.run(ServerStart.class, args); 19 } 20 21 @Bean 22 public ToolCallbackProvider weatherTools(WeatherService weatherService) { 23 return MethodToolCallbackProvider.builder().toolObjects(weatherService).build(); 24 } 25} 26
启动后报错:jackson冲突
Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonSerializeAs [in thread "background-preinit"]
经过Dependency Analyzer分析后,需要约束一下版本,解决包冲突:
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>org.my.mcp.tools</groupId> 8 <artifactId>mcp_tools</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <properties> 12 <java.version>21</java.version> 13 <spring-boot.version>3.2.5</spring-boot.version> 14 <maven.compiler.source>21</maven.compiler.source> 15 <maven.compiler.target>21</maven.compiler.target> 16 </properties> 17 <dependencyManagement> 18 <dependencies> 19 <dependency> 20 <artifactId>jackson-databind</artifactId> 21 <groupId>tools.jackson.core</groupId> 22 <version>3.1.4</version> 23 </dependency> 24 <dependency> 25 <artifactId>slf4j-api</artifactId> 26 <groupId>org.slf4j</groupId> 27 <version>2.0.17</version> 28 </dependency> 29 <dependency> 30 <artifactId>jackson-annotations</artifactId> 31 <groupId>com.fasterxml.jackson.core</groupId> 32 <version>2.21</version> 33 </dependency> 34 <dependency> 35 <artifactId>jackson-core</artifactId> 36 <groupId>tools.jackson.core</groupId> 37 <version>3.1.4</version> 38 </dependency> 39 </dependencies> 40 </dependencyManagement> 41 <dependencies> 42 <dependency> 43 <groupId>org.springframework.ai</groupId> 44 <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId> 45 <version>2.0.0</version> 46 </dependency> 47 </dependencies> 48 49</project>
启动成功:
1Connected to the target VM, address: '127.0.0.1:50454', transport: 'socket' 2 3 . ____ _ __ _ _ 4 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ 5( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 6 \\/ ___)| |_)| | | | | || (_| | ) ) ) ) 7 ' |____| .__|_| |_|_| |_\__, | / / / / 8 =========|_|==============|___/=/_/_/_/ 9 10 :: Spring Boot :: (v4.1.0) 11 12[serverAnnotatedMethodBeanPostProcessor]? Check the corresponding BeanPostProcessor declaration and its dependencies/advisors. If this bean does not have to be post-processed, declare it with ROLE_INFRASTRUCTURE. 132026-06-24T09:30:10.116+08:00 WARN 4520 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'serverAnnotatedBeanRegistry' of type [org.springframework.ai.mcp.server.common.autoconfigure.annotations.McpServerAnnotationScannerAutoConfiguration$ServerMcpAnnotatedBeans] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). Is this bean getting eagerly injected/applied to a currently created BeanPostProcessor [serverAnnotatedMethodBeanPostProcessor]? Check the corresponding BeanPostProcessor declaration and its dependencies/advisors. If this bean does not have to be post-processed, declare it with ROLE_INFRASTRUCTURE. 142026-06-24T09:30:10.126+08:00 WARN 4520 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.ai.mcp.server.annotation-scanner-org.springframework.ai.mcp.server.common.autoconfigure.annotations.McpServerAnnotationScannerProperties' of type [org.springframework.ai.mcp.server.common.autoconfigure.annotations.McpServerAnnotationScannerProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). Is this bean getting eagerly injected/applied to a currently created BeanPostProcessor [serverAnnotatedMethodBeanPostProcessor]? Check the corresponding BeanPostProcessor declaration and its dependencies/advisors. If this bean does not have to be post-processed, declare it with ROLE_INFRASTRUCTURE. 152026-06-24T09:30:10.424+08:00 INFO 4520 --- [ main] o.s.boot.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http) 162026-06-24T09:30:10.440+08:00 INFO 4520 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 172026-06-24T09:30:10.440+08:00 INFO 4520 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/11.0.22] 182026-06-24T09:30:10.499+08:00 INFO 4520 --- [ main] b.w.c.s.WebApplicationContextInitializer : Root WebApplicationContext: initialization completed in 1058 ms 192026-06-24T09:30:11.124+08:00 INFO 4520 --- [ main] o.s.a.m.s.c.a.McpServerAutoConfiguration : Enable tools capabilities, notification: true 202026-06-24T09:30:11.212+08:00 WARN 4520 --- [ main] o.s.a.m.a.p.tool.SyncMcpToolProvider : No tool methods found in the provided tool objects: [] 212026-06-24T09:30:11.213+08:00 INFO 4520 --- [ main] o.s.a.m.s.c.a.McpServerAutoConfiguration : Registered tools: 1 222026-06-24T09:30:11.213+08:00 INFO 4520 --- [ main] o.s.a.m.s.c.a.McpServerAutoConfiguration : Enable resources capabilities, notification: true 232026-06-24T09:30:11.217+08:00 INFO 4520 --- [ main] o.s.a.m.s.c.a.McpServerAutoConfiguration : Enable resources templates capabilities, notification: true 242026-06-24T09:30:11.219+08:00 INFO 4520 --- [ main] o.s.a.m.s.c.a.McpServerAutoConfiguration : Enable prompts capabilities, notification: true 252026-06-24T09:30:11.222+08:00 INFO 4520 --- [ main] o.s.a.m.s.c.a.McpServerAutoConfiguration : Enable completions capabilities 262026-06-24T09:30:11.405+08:00 INFO 4520 --- [ main] o.s.boot.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/' 272026-06-24T09:30:11.410+08:00 INFO 4520 --- [ main] org.my.mcp.tools.ServerStart : Started ServerStart in 2.578 seconds (process running for 3.187)