一、背景

在实现 MCP 服务器之前,我们先思考几个问题:

  • 🔍 你有没有试过让 ChatGPT 帮你查询数据库?
  • 🛠️ 你有没有想过让 AI助手帮你操作 Excel 文件?
  • 🌐 你有没有希望 AI 能够调用你公司内部的 API?

如果答案是 “有”,那么你很可能遇到了这样的困扰:AI模型很聪明,但它无法直接操作外部工具

这里就需要用到 MCP 了,具体 MCP 的详细介绍可以参考如下文章,这里我们主要介绍一下如何使用 SpringBoot 自己动手 实现一个用于 加减乘除 的 MCP 服务器

大模型(一)什么是 MCP?如何使用 Charry Studio 集成 MCP?https://blog.csdn.net/qq_33204709/article/details/150774029


二、知识回顾:什么是 MCP

MCP(Model Context Protocol),即 “模型上下文协议”,是一个开放、标准化的协议,由 Claude 的母公司 Anthropic 于 2024 年底开元发布的。简单来说,MCP 就是 AI 大模型的标准化工具箱,大模型可以利用这些工具与外界互动、获取信息并且完成具体任务(就像 USB-C 让不同设备能够通过相同的接口连接一样)。MCP 的目标是创建一个通用标准,使 AI 应用程序的开发和集成变得更加简单和统一。

从上图可以看出,MCP 就是以更标准的方式让 LLM Chat 使用不同工具。


三、SpringBoot实战

为了让大家能先有个印象,我们先看一下实战之后的完整项目文件结构:

3.1 初始化 SpringBoot

首先,我们打开 Spring 的初始化界面:https://start.spring.io/

进行如下配置:

在添加 MCP Server 配置的时候,我们点击 ADD DEPENDENCIES... 之后,输入 mcp 进行搜索,选择 Model Context Protocol Server 这一项即可,如下所示:

点击 GENERATE 之后会下载初始化好的代码包,如下所示:

解压之后就可以得到一个 SpringBoot 项目:


3.2 修改 Maven 依赖

我们使用 IDEA 打开下载好的代码包,编辑一下 pom.xml,将 spring-ai-starter-mcp-server 改为 spring-ai-starter-mcp-server-webmvc。因为由官网文档可以得知,spring-ai-starter-mcp-server 只支持标准输入输出(STDIO),这里我们需要使用的是 SSE 交互方式。

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>

修改之后,完整 pom.xml 如下所示:

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 https://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.5</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>springboot-mcp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-mcp</name>
	<description>Demo project for Spring Boot</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>17</java.version>
		<spring-ai.version>1.0.1</spring-ai.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.ai</groupId>
			<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</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>

3.3 修改 application.yml

由于小编习惯使用 yml 格式的配置文件,这里我们将 application.properties 重命名为 application.yml,填入如下内容:

server:
  port: 8088

spring:
  application:
    name: springboot-mcp

  ai:
    mcp:
        server:
          enabled: true
          name: local-mcp-server
          version: 1.0.0
          # sse请求地址
          sse-endpoint: /api/v1/sse
          sse-message-endpoint: /api/vi/mcp
          capabilities:
            tool: true

logging:
  level:
    io.modelcontextprotocol: TRACE
    org.springframework.ai.mcp: TRACE

3.4 编写工具类 MathTool

这里我们编写一个实现了 加减乘除 算法的工具类,其中 @Tool 注解是用于解释每个方法的作用,方便大模型理解在什么场景下可以调用这个方法。完整代码如下:

MathTool.java

import org.springframework.ai.tool.annotation.Tool;

public class MathTool {

    @Tool(description = "两个数字相加")
    public Integer addNumbers(Integer a, Integer b) {
        return a + b;
    }

    @Tool(description = "两个数字相减")
    public Integer subtractNumbers(Integer a, Integer b) {
        return a - b;
    }

    @Tool(description = "两个数字相乘")
    public Integer multiplyNumbers(Integer a, Integer b) {
        return a * b;
    }

    @Tool(description = "两个数字相除")
    public Double divideNumbers(Double a, Double b) {
        if (b == 0) {
            throw new IllegalArgumentException("除数不能为零");
        }
        return a / b;
    }
}

3.5 将工具类注册到 MCP

我们编写好工具类之后,需要再将工具类注册到 Spring 的 IOC 容器,这样才可以通过 SSE 接口进行使用。为了方便,我们直接将 @Bean 注册代码写到 SpringbootMcpApplication 类中。这里我们使用了 MethodToolCallbackProvider 来完成工具类到 SSE 接口的绑定,完整代码如下:

SpringbootMcpApplication.java

import com.example.tool.MathTool;
import org.springframework.ai.tool.ToolCallbackProvider;
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 SpringbootMcpApplication {

    public static void main(String[] args) {
       SpringApplication.run(SpringbootMcpApplication.class, args);
    }

    @Bean
    public ToolCallbackProvider mathTool() {
       return MethodToolCallbackProvider.builder().toolObjects(new MathTool()).build();
    }

}

3.6 启动项目

启动项目,日志打印如下,说明启动成功:


四、MCP 使用测试

4.1 Postman 测试

打开 Postman 测试,由于需要使用 MCP 功能,可能低版本并不支持,大家可以参考一下小编使用的是 11.60.2 版本。

在 Postman 中,点击 New,选择 MCP 进行创建。

创建好 MCP 之后,我们需要做三件事:

  1. 切换请求方式为 HTTP;
  2. 输入本地 SSE 地址:http://localhost:8088/api/vi/sse
  3. 点击 Connect 进行连接。

我们可以看到下面 Response 中显示了连接成功,在 Tools 栏中也显示了我们的 MCP 服务器中有哪些工具。

注意看,这里每个方法名下面的中文描述,就是我们在 @Tool(description="") 中输入的内容。

选择我们想要运行的工具,输入参数之后,点击 Run 即可运行,右下角会显示响应的结果。

可以看到,IDEA 的控制台中也打印了相关的日志,测试完毕。


4.2 Charry Studio 测试

我们打开 Charry Studio 之后,点击右上角的设置。

选择 MCP设置 -> + 添加服务器 -> 快速创建

输入名称之后,在 类型 处,选择 服务器发送事件 (sse)

在 URL 中,输入我们的 SSE 接口路径:http://localhost:8088/api/v1/sse,点击 保存

出现 “服务器更新成功” 的提示之后,我们可以点击按钮开启 MCP 服务器。

MCP 服务器开启成功之后,按钮会变为绿色,并且显示版本号为 1.0.0

开启 MCP 服务器之后,就可以开始使用了。我们在会话中,点击 MCP 服务器,选择我们设置好的 local-mcp-server

然后,在会话中进行相关的描述,例如:

计算一下1+2

大模型就会自动调用 MCP 服务器的相关接口进行计算,同时 IDEA 中也会有相关的日志打印。

至此,恭喜你已经成功创建并验证了一个用于计算加减乘除的 MCP 服务器,快去开发自己需要的 MCP 服务器吧。

如果大家想了解一下补充大模型和 SSE 接口的交互原理,可以参考官方文档,已经描述的很详细了:

整理完毕,完结撒花~🌻





参考地址:

1.基于SpringAI开发Java版mcp服务,https://www.bilibili.com/video/BV1yT8qzMEbd/

2.一个简单的java基于spring-ai构建mcp server以及mcp client案例,https://www.cnblogs.com/hetutu-5238/p/18913677

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐