Search K
Appearance
Appearance
首先引入Spring AI
的依赖,Java
版本要求最低17
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zhaochao</groupId>
<artifactId>mcp-server-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mcp-server-app</name>
<description>mcp-server-app</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
<version>1.0.0-M8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.zhaochao.mcpserverapp.McpServerAppApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
stdio
默认是关闭的,默认是走SSE
模式的
application.yaml
server:
port: 8000
spring:
application:
name: "mcp-server"
ai:
mcp:
server:
stdio: false
name: location-mcp-server
version: 0.0.1
编写一个测试的tools
com.zhaochao.mcpserverapp.service.JdbcQueryService
package com.zhaochao.mcpserverapp.service;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;
@Service
public class JdbcQueryService {
@Tool(description = "查询天气预报")
public String getWeather(@ToolParam(description = "城市位置") String location){
return location + "现在的天气是:晴,28°";
}
}
添加配置文件,将编写的tools
方法抛出去
com.zhaochao.mcpserverapp.config.McpConfig
package com.zhaochao.mcpserverapp.config;
import com.zhaochao.mcpserverapp.service.JdbcQueryService;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class McpConfig {
@Bean
ToolCallbackProvider toolCallbackProvider(JdbcQueryService jdbcQueryService) {
return MethodToolCallbackProvider.builder().toolObjects(jdbcQueryService).build();
};
}
项目可以直接运行,或者生成jar
包后运行,可以使用post man
先测试http://localhost:8000/sse
接口是否可以正常连接
然后在任意的模型调用平台配置MCP
相关服务,就可以正常调用了,模型会自动识别到该MCP
服务下有多少tools