MCP Java 笔记

MCP Java 笔记

1.条件与环境

1.1 前提条件

软件/账号 是否必须
JDK 17+ Y
Spring Boot 3.4+ Y (使用spring-ai-mcp时必须、如使用MCP官方Java SDK则要求)
Maven 3.9+ Y
IDEA N

1.2 本文运行环境

平台/软件 版本
操作系统 Windows 10 企业版 22H2 19045.4046 64位
IDEA 2023.2.4 (UE)
JDK 17.0.13
spring-ai-mcp 1.0.0-M7 (依赖Spring Boot 3.4.4)
Maven 3.9.9
Cursor 0.48.7

2.使用spring-ai-starter-mcp-server-webmvc新建MCP Server

2.1 构建项目

通过Spring脚手架创建项目:

image-20250418135232073

2.2 编写MCP服务代码

在IDEA中打开项目,分别编写1个简单的Controller和Service:

HelloController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.example.mcp.server.controller;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

@Tool(description = "Say hello")
@GetMapping("/{name}")
public String sayHi(@ToolParam(description = "Say hello to user") @PathVariable String name) {
return "hello " + name;
}
}

DemoService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.example.mcp.server.service;

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

@Service
public class DemoService {

@Tool(description = "Get something")
public String getSomething(String param1, int param2) {
// Implementation
return param1;
}
}

2.3 添加ToolCallbackProvider bean

在Application中添加ToolCallbackProvider bean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.example.mcp.server;

import com.example.mcp.server.controller.HelloController;
import com.example.mcp.server.service.DemoService;
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 DemoMcpServerWebmvcApplication {

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

@Bean
ToolCallbackProvider toolCallbackProvider(HelloController helloController, DemoService demoService) {
return MethodToolCallbackProvider.builder()
.toolObjects(helloController, demoService)
.build();
}
}

2.4 测试服务

1.运行服务:

image-20250418135902405

2.通过Cursor连接服务:

image-20250418123435306

image-20250418140033378

3.通过spring-rest-to-mcp将Spring RESTful服务转换为MCP服务

思考:当项目中有很多个Controller和Service时,注册到ToolCallbackProvider bean时需要手动将它们一一传入、颇费功夫,有没有自动发现这些服务并注册的方法呢?更进一步,有没有将原来的Controller和Service自动转换为MCP服务的工具呢?一番搜索,果然有高手已经开发了将Controller转换为MCP服务的工具,详见一条命令搞定!存量 Spring REST 服务秒变 MCP 服务,酷!

3.1 安装工具到本地maven仓库

1.下载工具源码工程:

1
2
3
git clone https://github.com/addozhang/spring-rest-to-mcp.git
cd spring-rest-to-mcp
mvn clean install

注:运行要求JDK17+和Maven3.6+

3.2 转换Spring RESTful服务为MCP服务

可以使用自己的项目、也可以使用作者提供的项目【点击直达】进行试用;

注:待转换的项目需要使用SpringBoot 3.x版本并使用Maven作为构建工具

在项目目录下运行2次下列命令(第1次运行更新pom.xml、第2次运行进行相关代码添加):

1
2
3
4
mvn org.openrewrite.maven:rewrite-maven-plugin:6.4.0:run \
-Drewrite.activeRecipes=RewriteWebToMCP \
-Drewrite.recipeArtifactCoordinates=com.atbug.rewrite:spring-rest-to-mcp:1.0-SNAPSHOT \
-Drewrite.exportDatatables=true

注:作者博客中写的是-Drewrite.recipeArtifactCoordinates=com.atbug.rewrite:web-to-mcp:1.0-SNAPSHOT需将其中web-to-mcp改为spring-rest-to-mcp,作者可能后期修改了‎artifactId‎

3.3 测试服务

1.运行服务:

image-20250418144134158

2.通过Cursor连接服务:

image-20250418144256632

4.Nacos MCP Registry实现“0改动”将应用接口转换为MCP服务

思考:spring-rest-to-mcp属于开发态的转换工具、归根到底还是需要修改原来的代码,有没有部署态的工具可以做到这种转换呢?答案是必须的,详见Nacos 发布 MCP Registry,实现存量应用接口“0改动”升级到 MCP 协议

待研习

相关链接