1.概述
MCP(Model Context Protocol) 是一种开放协议,用于让 AI 模型(或 AI 客户端)安全、标准化地调用外部工具、服务或数据源。
你可以把它理解为:
🧠 “AI 的通用插件接口” —— 让大模型能像调用函数一样,使用你本地或后端的任意能力(查天气、读数据库、调 API、执行代码等)。
到目前位置springboot还没有正式出MCP的SDK,还在内测,估计2026年正式上线,我们可以使用内测版本。
2.springboot MCP服务
官方参考代码:
https://github.com/spring-projects/spring-ai-examples/blob/main/model-context-protocol/weather/starter-webmvc-server/src/main/java/org/springframework/ai/mcp/sample/server/WeatherService.java
我们这里实现了一个本地执行ddl的工具,可以让大模型根据我们的数据库需求生成好ddl之后,调用我们写的springboot mcp服务来自动在mysql数据库中创建表。
2.1 代码结构


2.2 代码内容
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 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.0</version>
<relativePath/>
</parent>
<groupId>com.daimazhan</groupId>
<artifactId>mpc-test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
**application.properties**:
```properties
# spring.main.web-application-type=none
# NOTE: You must disable the banner and the console logging
# to allow the STDIO transport to work !!!
spring.main.banner-mode=off
# logging.pattern.console=
# spring.ai.mcp.server.stdio=false
spring.ai.mcp.server.name=MCP-Test-Server
spring.ai.mcp.server.version=0.0.1
spring.ai.mcp.server.protocol=STREAMABLE
# spring.ai.mcp.server.protocol=STATELESS
logging.file.name=./model-context-protocol/weather/starter-webmvc-server/target/starter-webmvc-server.log
Main.java
package com.daimazhan;
import com.daimazhan.service.MysqlMCPService;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.function.FunctionToolCallback;
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 Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@Bean
public ToolCallbackProvider weatherTools(MysqlMCPService mysqlMCPService) {
return MethodToolCallbackProvider.builder().toolObjects(mysqlMCPService).build();
}
public record TextInput(String input) {
}
@Bean
public ToolCallback toUpperCase() {
return FunctionToolCallback.builder("toUpperCase", (TextInput input) -> input.input().toUpperCase())
.inputType(TextInput.class)
.description("Put the text to upper case")
.build();
}
}
MysqlUtil.java
package com.daimazhan.util;
import java.sql.*;
/**
* mysql 工具类
*/
public class MysqlUtil {
public static void executSql(String sql) throws SQLFeatureNotSupportedException{
String url = String.format("jdbc:mysql://%s:%s/%s?useInformationSchema=true&nullCatalogMeansCurrent=true&useSSL=false&serverTimezone=UTC","localhost",3306,"op-springboot-vue-framewor");
try (Connection conn = DriverManager.getConnection(url, "root", "meimima")) {
try (Statement stmt = conn.createStatement()) {
boolean result = stmt.execute(sql); // DDL 返回 false(无结果集)
System.out.println("Table 'users' created successfully.");
}
} catch (SQLException e) {
throw new SQLFeatureNotSupportedException(e);
}
}
}
MysqlMCPService.java
package com.daimazhan.service;
import com.daimazhan.util.MysqlUtil;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;
@Service
public class MysqlMCPService {
@Tool(description = "Execute a DDL SQL on the MySQL MCP database.")
public String executeDDLSql(String ddl) {
try {
MysqlUtil.executSql(ddl);
}catch (Exception e){
return "execute ddl failed: " + e.getMessage();
}
return "execute ddl successfully";
}
}
CorsConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsWebFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOriginPattern("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
config.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
}
2.3 启动
就这几个代码文件便可实现一个MCP 服务,请记得修改MysqlUtil中的数据库配置,我是写死到代码中的。
主要是利用spring AI的tool注解标明方法是一个MCP工具服务,springboot内部会默认启动http mcp服务,地址是:http://localhost:8080/mcp
如图:


3.vscode配置
VS Code 1.102 版本开始,VS Code 中的 MCP 支持已全面可用。
可以在设置里面检查一下是否开启:


确定启用之后,然后我们去一个项目目录下,或者新创建一个目录,然后使用vscode打开该目录,然后我们创建.vscode目录,并且在.vscode目录下创建mcp.json文件,内容如下:
{
"servers": {
"DB-MCP": {
"type": "http",
"url": "http://localhost:8080/mcp"
}
}
}
如图:


settings.json是自动生成的,可以不用管他。
然后我们可以点击json里面的启动按钮,服务便可启动,如图:


并且可以从日志里面看到我们提供的多少工具。
并且也可以从插件中看到我们的MCP服务,如图:


4.copilot配置
我们可以配置copilot大模型来集成我们的MCP服务,当我们有创建表的需求时,调用使用copilot大模型来创建即可。
搜先打开copilot对话框,然后点击工具图标,如图:


然后勾选我们的MCP服务工具,如图:


然后便可使用,比如我们输入:
==帮我创建一张角色表,表名是role_t,字段有id (bigint) 自增, name(varchar(300)),sort(int), deleted(char(1)),create_time(datetime),update_time(datetime)==
如图:




这样便成功了,并且很方便。