普通视图

Received before yesterday

Spring AI实现MCP Server

2025年11月9日 00:00

未完待续

基于Spring AI 1.1.0版本,实现三种MCP Server

1.SSE/Streamable-HTTP模式MCP Server

引入依赖spring-ai-starter-mcp-server-webmvc

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>3.5.7</version></parent><dependencyManagement>    <dependencies>        <dependency>            <groupId>org.springframework.ai</groupId>            <artifactId>spring-ai-bom</artifactId>            <version>1.1.0</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-mcp-server-webmvc</artifactId>    </dependency>    <!-- Lombok -->    <dependency>        <groupId>org.projectlombok</groupId>        <artifactId>lombok</artifactId>    </dependency></dependencies><build>    <plugins>        <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-compiler-plugin</artifactId>            <configuration>                <source>21</source>                <target>21</target>                <encoding>UTF-8</encoding>            </configuration>        </plugin>    </plugins></build>

application.yml下配置server有关的配置

spring:  application:    name: spring-ai-mcp-server  ai:    mcp:      server:        name: spring-ai-mcp-server        version: 1.0.0        type: async        sse-endpoint: /sse        protocol: sseserver:  port: 8080

编写工具方法,通过Tool注解声明为工具方法

package org.example.mcp.tools;import lombok.extern.slf4j.Slf4j;import org.springframework.ai.tool.annotation.Tool;import org.springframework.stereotype.Component;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;@Component@Slf4jpublic class DateTimeTool {    @Tool(description = "获取当前日期和时间(GMT+8)")    public String current() {        return LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME);    }}

通过ToolCallbackProvider将工具类放入MCP Server

package org.example.config;import org.example.mcp.tools.DateTimeTool;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;@Configurationpublic class McpConfig {    @Bean    public ToolCallbackProvider provider(DateTimeTool dateTimeTool) {        return MethodToolCallbackProvider.builder().toolObjects(                dateTimeTool        ).build();    }}

集成MCP Server到Cherry Studio,配合大模型进行调用

当采用Streamable-HTTP协议时,将配置更改为这样即可,Cherry Studio配置也需要同步更改

spring:  application:    name: spring-ai-mcp-server  ai:    mcp:      server:        name: spring-ai-mcp-server        version: 1.0.0        type: async        protocol: streamable        streamable-http:          mcp-endpoint: /mcp-endpoint

2.Stdio模式的MCP Server实现

参考

  1. https://docs.spring.io/spring-ai/reference/api/mcp/mcp-streamable-http-server-boot-starter-docs.html

Spring AI集成MCP Client

2025年11月8日 00:00

未完待续

1.MCP概述

MCP(Model Context Protocol),即模型上下文协议,是一种开放标准,使大模型采用统一的标准化的方式与外部的工具和数据等进行通信交互。

之前提到,大模型可以通过Tools(Function calling)来实现一些获取信息和操作数据的功能,如果我们自定义好了一些公共的工具给别人用,例如提供实时日期信息、天气信息,股市交易信息、汇率信息等,想要开放给很多大模型来使用,如果没有标准化的接口,每产生一个大模型应用就要适配一次,MCP协议为解决这一问题而生,现在我们实现的工具只需要面向MCP接口协议进行开发,大模型也遵循MCP规范进行接入使用,这个问题就解决了,我们实现的服务就叫MCP服务端,大模型实现的就是MCP的客户端。

MCP协议产生于2024年,具体协议内容可见:https://modelcontextprotocol.io/docs/getting-started/intro

2.调用MCP

MCP调用方式有三种,SSE,streamable-http和Stdio,SSE和streamable-http以http方式调用部署好的远程MCP服务器上的MCP,Stdio是将MCP的源码下载到本地打成软件包,使用Spring AI驱动npx或uvx等命令来本地调用软件包中的MCP,其中常见的TypeScript编写的MCP需要由npx调用,Python编写的MCP需要由uvx调用,其他语言也有其他语言MCP的调用方式。

我使用的Spring AI 1.0.3版本不支持streamable-http,在远程调用modelscope时需要在modelscope上修改接口为SSE模式。从1.1.0版本开始支持了streamable-http

以部署在modelscope上面的12306-mcp为例,分别介绍SSE远程调用和Stdio模式本地调用。12306-mcp是一个查询铁路12306平台,返回列车订票信息的MCP应用

2.1 SSE调用MCP

pom中引入调用MCP需要的spring-ai-starter-mcp-client依赖

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>3.5.7</version></parent><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>    <!-- Lombok -->    <dependency>        <groupId>org.projectlombok</groupId>        <artifactId>lombok</artifactId>    </dependency></dependencies><build>    <plugins>        <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-compiler-plugin</artifactId>            <configuration>                <source>21</source>                <target>21</target>                <encoding>UTF-8</encoding>            </configuration>        </plugin>    </plugins></build>

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;@Configurationpublic 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();    }}

对话接口和以往完全一样

package org.example.controller;import jakarta.annotation.Resource;import org.springframework.ai.chat.client.ChatClient;import org.springframework.ai.chat.memory.ChatMemory;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import reactor.core.publisher.Flux;@RestController@RequestMapping("ai")public class ChatController {    @Resource    private ChatClient chatClient;    //127.0.0.1:8080/ai/chat-stream?msg=你是谁&chatId=001    @GetMapping(value = "chat-stream", produces = "text/html;charset=utf-8")    public Flux<String> stream(String msg, String chatId) {        return chatClient.prompt()                .user(msg)                .advisors(advisor -> advisor.param(ChatMemory.CONVERSATION_ID, chatId))                .stream()                .content();    }}

大模型已经能在对话中调用MCP了

2.2 Stdio调用MCP

因为是本地调用,所以需要先将这个MCP的源码clone并安装到本地,因为这个MCP是TS语言编写,因此还需要用npm将其安装到本地。

git clone https://github.com/Joooook/12306-mcp.gitcd 12306-mcp npm i

运行前,如未安装npx,还需要全局安装npx,用于被Spring AI驱动本地运行MCP

npm i -g npx

根据MCP的标准,Stdio模式将MCP按一定格式配置到JSON文件中

{  "mcpServers": {    "12306-mcp": {      "args": [        "-y",        "12306-mcp"      ],      "command": "npx"    }  }}

modelscope上面给出的JSON格式是Mac/Linux的,如果是Windows系统,需要修改:

{  "mcpServers": {    "12306-mcp": {      "command": "cmd",      "args": [        "/c",        "npx",        "-y",        "12306-mcp"      ]    }  }}

将配置文件放入类路径下,同application.yml放在一级,这里将这个json文件命名为mcp-server.json,并将配置放入spring ai

spring:  ai:    mcp:      client:        enabled: true        name: spring-ai-agent        type: sync        stdio:          servers-configuration: classpath:mcp-server.json    deepseek:      base-url: https://api.deepseek.com      api-key: ${DEEP_SEEK_KEY}logging:  level:    io.modelcontextprotocol: DEBUG    org.springframework.ai.mcp: DEBUG

启动后,可见日志

2025-11-09T12:15:07.418+08:00  INFO 39432 --- [pool-5-thread-1] i.m.c.transport.StdioClientTransport     : STDERR Message received: 12306 MCP Server running on stdio @Joooook

运行起来是相同的效果

2.3 续:Streamable-HTTP调用MCP

2025年11月14日前后,Spring AI 1.0.0发布,支持了Streamable-HTTP方式,只需要修改版本号,然后做以下配置即可:

spring:  ai:    mcp:      client:        enabled: true        name: spring-ai-agent        type: async        streamable-http:          connections:            12306-mcp:              url: https://mcp.api-inference.modelscope.net/              endpoint: /********/mcp
❌