spring-mcp-example

zengzeliang/spring-mcp-example

3.2

If you are the rightful owner of spring-mcp-example and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.

This is a comprehensive example project for a Spring AI Model Context Protocol (MCP) server and client.

Tools
2
Resources
0
Prompts
0

Spring MCP Example

这是一个完整的 Spring AI MCP (Model Context Protocol) 服务器和客户端示例项目。

项目结构

spring-mcp-example/
├── mcp-server/          # MCP 服务器
│   ├── src/
│   └── pom.xml
├── mcp-client/          # MCP 客户端
│   ├── src/
│   └── pom.xml
└── README.md

快速开始

1. 环境要求

  • JDK 17 或更高版本
  • Maven 3.6 或更高版本

2. 启动服务器

cd mcp-server
mvn spring-boot:run

服务器将在 http://localhost:8080 启动,MCP SSE 端点在 http://localhost:8080/mcp/sse

3. 运行客户端

在新的终端窗口:

cd mcp-client
mvn spring-boot:run

服务器提供的工具

服务器提供了两个示例工具:

1. getWeather

获取指定城市的天气信息

参数:

  • city (必需): 城市名称(拼音)

示例:

{
  "city": "beijing"
}

2. getAirQuality

获取指定城市的空气质量信息

参数:

  • city (必需): 城市名称

示例:

{
  "city": "beijing"
}

技术栈

  • Spring Boot 3.5.7
  • Spring AI 1.0.0-M7
  • Model Context Protocol SDK 0.10.0
  • Java 17

常见问题

Q: 客户端报 404 错误怎么办?

A: 确保:

  1. 服务器已经启动并且没有错误
  2. 服务器端口是 8080
  3. 客户端连接的地址是 http://127.0.0.1:8080/mcp/sse

Q: 如何添加新的工具?

A: 在服务器的 ToolComponent 类中添加新方法,使用 @Tool 注解标注即可。

配置说明

服务器配置 (mcp-server/src/main/resources/application.yml)

server:
  port: 8080

spring:
  ai:
    mcp:
      server:
        enabled: true
        type: async

客户端配置

客户端使用编程方式连接,无需额外配置文件。

许可证

MIT License