Spring AI调用MCP服务
本文最后更新于 2025年11月8日
未完待续
1.MCP概述
MCP(Model Context Protocol),即模型上下文协议,是一种开放标准,使大模型采用统一的标准化的方式与外部的工具和数据等进行通信交互。
之前提到,大模型可以通过Tools(Function calling)来实现一些获取信息和操作数据的功能,如果我们自定义好了一些公共的工具给别人用,例如提供实时日期信息、天气信息,股市交易信息、汇率信息等,想要开放给很多大模型来使用,如果没有标准化的接口,每产生一个大模型应用就要适配一次,MCP协议为解决这一问题而生,现在我们实现的工具只需要面向MCP接口协议进行开发,大模型也遵循MCP规范进行接入使用,这个问题就解决了,我们实现的服务就叫MCP服务端,大模型实现的就是MCP的客户端。
MCP协议产生于2024年,具体协议内容可见:https://modelcontextprotocol.io/docs/getting-started/intro
2.SSE调用MCP
pom中引入调用MCP需要的spring-ai-starter-mcp-client和spring-ai-mcp依赖
<?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.5.7</version>
</parent>
<groupId>org.example</groupId>
<artifactId>spring-ai-mcp</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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-model-deepseek</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp</artifactId>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>application.yml中配置一个modelscope上面开放的MCP工具12306-mcp
spring:
ai:
mcp:
client:
enabled: true
name: spring-ai-agent
type: async
sse:
connections:
12306-mcp:
url: https://mcp.api-inference.modelscope.net/
sse-endpoint: /********/sse
deepseek:
base-url: https://api.deepseek.com
api-key: ${DEEP_SEEK_KEY}
logging:
level:
io.modelcontextprotocol: DEBUG
org.springframework.ai.mcp: DEBUG配置类中,将外部MCP工具ToolCallbackProvider注入并和ChatClient进行绑定
package org.example.config;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public ChatClient chatClient(DeepSeekChatModel model, ChatMemory chatMemory, ToolCallbackProvider toolCallbackProvider) {
return ChatClient.builder(model)
.defaultAdvisors(
SimpleLoggerAdvisor.builder().build(),
MessageChatMemoryAdvisor.builder(chatMemory).build()
)
.defaultToolCallbacks(toolCallbackProvider)
.build();
}
}
大模型已经能在对话中调用MCP了

"如果文章对您有帮助,可以请作者喝杯咖啡吗?"
微信支付
支付宝
Spring AI调用MCP服务
https://blog.liuzijian.com/post/spring-ai/2025/11/08/spring-ai-mcp-client.html