参考官网文档:https://docs.spring.io/spring-ai/reference/api/mcp/mcp-server-boot-starter-docs.html。

1.引入SpringAI并引入MCP的包

plugins {
    kotlin("jvm") version "1.9.25"
    kotlin("plugin.spring") version "1.9.25"
    id("org.springframework.boot") version "3.4.4"
    id("io.spring.dependency-management") version "1.1.7"
}

group = "com.wanna.project.ai"
version = "0.0.1-SNAPSHOT"

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

configurations {
    compileOnly {
        extendsFrom(configurations.annotationProcessor.get())
    }
}

repositories {
    mavenCentral()
}

extra["springAiVersion"] = "1.0.0-M7"

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.springframework.ai:spring-ai-starter-model-openai")
    implementation("org.springframework.ai:spring-ai-starter-mcp-server-webmvc")
    compileOnly("org.projectlombok:lombok")
    annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
    annotationProcessor("org.projectlombok:lombok")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

dependencyManagement {
    imports {
        mavenBom("org.springframework.ai:spring-ai-bom:${property("springAiVersion")}")
    }
}

kotlin {
    compilerOptions {
        freeCompilerArgs.addAll("-Xjsr305=strict")
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

最关键的,一定要引入SpringAI的mcp的依赖,mcp-server可以以不同的方式去进行暴露服务,命令行/SSE。对应的,我们可以引入命令行(stdio),可以引入WebMVC/WebFlux相关的依赖,我们下面使用的是WebMVC的方式进行暴露。

    implementation("org.springframework.ai:spring-ai-starter-mcp-server-webmvc")

在application.properties中新增如下的SpringBoot配置,配置MCP Server的相关配置信息:

spring.ai.mcp.server.name=webmvc-mcp-server
spring.ai.mcp.server.version=1.0.0
spring.ai.mcp.server.type=sync
spring.ai.mcp.server.sse-message-endpoint=/mcp/messages

2.新建AIToolService管理和AI之间进行交互的工具(Function)

新建如下的Service:

@Service
class AIToolService {

    @Tool(name = "getWeather", description = "根据城市名称查询天气信息")
    fun getWeather(@ToolParam(description = "城市") city: String): String {
        return "城市 $city 的天气是晴朗的,25℃"
    }

    @Tool(name = "getFlightNum", description = "根据航线查询合适的航班")
    fun getFlightNum(
        @ToolParam(description = "出发城市三字码(例如PEK)") depCity: String,
        @ToolParam(description = "到达城市三字码(例如HKG)") arrCity: String
    ): String {
        return "CX345,CX393"
    }

}

其中如下的注解配置,代表对外提供一个getWeather的函数,description是它的描述信息,方便AI大模型可以根据关键词信息去找到我们对应的工具进行执行。

 @Tool(name = "getWeather", description = "根据城市名称查询天气信息")

在参数当中的@ToolParam注解,则用来说明这个参数的作用。

@ToolParam(description = "城市") 

3. 注册ToolCallbackProvider暴露成为MCP-Server

我们配置如下的Spring配置类,将AIToolService去注册成为ToolCallbackProvider。

@Configuration(proxyBeanMethods = false)
class McpToolServerConfiguration {

    @Bean
    fun weatherTools(aiToolService: AIToolService): ToolCallbackProvider {
        return MethodToolCallbackProvider.builder().toolObjects(aiToolService).build()
    }
}

在启动时,会有如下的日志提示我们MCP Server注册成功:

2025-04-22T03:50:42.881+08:00  INFO 74972 --- [my-ai-project] [           main] o.s.a.m.s.a.McpServerAutoConfiguration   : Registered tools: 2, notification: true

4.使用MCP客户端连接测试

客户端我们这里使用的是Cherry Studio,对个人开发者免费。Cherry Studio可以方便我们管理各个大模型,以及MCP服务,以及众多和AI相关的服务。

image-mkhg.png

接着,填入MCP服务端的地址,也就是我们的SpringBoot服务的地址http://localhost:8080/sse,注意要加后缀/sse

保存并启用后,发现可以在Tool中找到我们刚刚通过SpringBoot注册的MCP服务。

image-u7nd.png

展开还能看到更详细的入参信息:

image-l9mh.png

接着,我们使用GPT-4o,和大模型进行交互,可以发现大模型可以成功调用我们本地注册的MCP服务,并且有明确告诉我们使用的是getFlightNum这个函数Tool。

image-29cs.png

对于连续的两次FunctionCall调用,大模型都能帮我们解决。

image-4zd4.png