1 添加maven依赖
<?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 http://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.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>jdbc-mcp-server</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<spring-ai.version>1.0.0-M6</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.1.0</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2 配置文件application.properties内容如下:
spring.ai.mcp.server.name=jdbc-mcp-server
# 不是本地进程通信方式,使用sse方式
spring.ai.mcp.server.stdio=false
spring.data.jdbc.dialect=mysql
spring.datasource.url=jdbc:mysql://localhost:3306/gm?useSSL=false&serverTimezone=UTC
spring.datasource.username=gongmin
spring.datasource.password=gongmin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
3 申明mcp server tools:
package org.example.mcp.service;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* TODO
*
* @author gongmin
* @date 2025/4/16 17:39
*/
@Service
public class JdbcQueryService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Tool(description = "查询数据库中所有的表")
public List<Map<String, Object>> queryAllTables() {
String sql = "SELECT TABLE_NAME , TABLE_COMMENT FROM information_schema.tables WHERE TABLE_SCHEMA = DATABASE()";
return jdbcTemplate.queryForList(sql);
}
@Tool(description = "查询数据库中的表的数据")
public List<Map<String, Object>> queryTable(@ToolParam(description = "表名") String tableName) {
String sql = "SELECT * FROM " + tableName;
return jdbcTemplate.queryForList(sql);
}
@Tool(description = "实现两个数的加法")
public int add(@ToolParam(description = "数值1") int num1, @ToolParam(description = "数值2") int num2) {
return num1 + num2;
}
}
4 注册mcp server tools
package org.example.mcp.config;
import org.example.mcp.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;
/**
* TODO
*
* @author gongmin
* @date 2025/4/16 17:40
*/
@Configuration
public class McpConfig {
@Bean
ToolCallbackProvider jdbcQueryTools(JdbcQueryService jdbcQueryService) {
return MethodToolCallbackProvider
.builder()
.toolObjects(jdbcQueryService)
.build();
}
}
5 申明配置类注册Bean
package org.example.mcp.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
* TODO
*
* @author gongmin
* @date 2025/4/16 20:50
*/
@Configuration
public class DatabaseConfig {
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
6 启动类
package org.example.mcp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* TODO
*
* @author gongmin
* @date 2025/4/16 19:56
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}