简单 MCP Server 示例 (Java + Spring Boot + Spring AI)
RemoteAbout 简单 MCP Server 示例 (Java + Spring Boot + Spring AI)
This Java and Spring Boot project demonstrates an MCP server built with Spring AI and SSE communication. It provides weather, utility, and JSON-conversion tools for MCP clients.
Key features
- 01Java and Spring Boot example
- 02SSE communication
- 03Weather forecasts and alerts
- 04Echo, math, and timestamp tools
- 05JSON-to-Java code conversion
Use cases
- 01Query a location's weather forecast
- 02Check weather alerts by state
- 03Echo text or perform simple calculations
- 04Convert JSON strings to Java class code
MCP support
It exposes weather, utility, and JSON tools to MCP clients over an SSE endpoint.
Configuration
Remote servers need no local install. Pick your client, then copy the config or install in one click.
http://localhost:8080/sseclaude mcp add --transport http demo-oneinstepgo http://localhost:8080/sse{"mcpServers":{"demo-oneinstepgo":{"type":"http","url":"http://localhost:8080/sse"}}}Open in Cursor{"servers":{"demo-oneinstepgo":{"type":"http","url":"http://localhost:8080/sse"}}}Open in VS Code{"mcpServers":{"demo-oneinstepgo":{"type":"http","url":"http://localhost:8080/sse"}}}{"mcpServers":{"demo-oneinstepgo":{"serverUrl":"http://localhost:8080/sse"}}}Run commands in a terminal; paste JSON into the client’s MCP config and restart. Servers that need sign-in will prompt on first connect.
Tools
Details
From the project README简单 MCP Server 示例 (Java + Spring Boot + Spring AI)
项目目标
本项目旨在开发一个简单的模型上下文协议 (MCP) 服务器。此服务器将作为如何在 Java 和 Spring Boot 环境下构建 MCP 服务器的示例,使用 sse 进行通信。
技术栈
- Java: JDK 21
- Spring Boot: 3.3.12
- Spring AI:
spring-ai-starter-mcp-server-webflux用于 MCP 服务器功能 - 构建工具: Maven
- 通信协议: sse
核心依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-bom</artifactId>
<version>0.10.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Spring Boot Web Starter for SSE -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webflux</artifactId>
<version>${spring-ai.version}</version>
</dependency>
</dependencies>
关键组件
-
MCP 服务器配置:
- 设置
FastMCP或等效的 Spring AI 组件来初始化 MCP 服务器。 - 服务器通过 sse 进行通信。
- 设置
-
示例工具 (Tools):
- 实现了以下工具:
WeatherTool: 查询天气预报和天气警报SimpleTool: 提供回声、简单数学和时间戳功能JsonTool: 将 JSON 字符串转换为 Java 类代码,并根据 Java 类定义输出一个类型规范的示例 JSON 字符串
- 工具的定义遵循 MCP 规范,使用 Java 类型提示和注解来自动生成工具定义。
- 实现了以下工具:
项目结构
cem_mcp_server
├── src/main/java/com/example/mcpserver
│ ├── config/ # 配置类
│ ├── McpServerApplication.java # 主应用程序类
│ └── tools/ # 工具实现
│ ├── WeatherTool.java # 天气工具
│ └── SimpleTool.java # 简单工具
│ ├── JsonTool.java # JSON 工具
├── src/main/resources
│ └── application.yml # 应用配置
└── pom.xml # Maven 配置
编译和运行
使用 Maven 命令
mvn clean package spring-boot:run
测试和调试
服务器端点
- 连接根端点:
http://localhost:8080/sse- 连接MCP服务器
测试工具
可以使用支持 MCP 协议的客户端(如 Cursor 编辑器)连接到 MCP 服务器。服务器提供以下工具:
-
天气工具:
getWeatherForecastByLocation: 获取指定经纬度的天气预报getAlerts: 获取指定州的天气警报
-
简单工具:
echoTool: 回声工具,返回输入的消息simpleMathTool: 计算两个数字的和timestampTool: 返回当前时间
与 Cursor 集成
在 Cursor 编辑器中,可以通过以下步骤连接到此 MCP 服务器:
- 项目下新增文件
.cursor/mcp.json。{ "mcpServers": { "mcp-serve-demo": { "name": "本地 MCP 服务器", "url": "http://localhost:8080/sse", "enabled": true } } } - 打开 Cursor 设置
- 找到 MCP 相关配置,刷新本MCP工具。
- 现在可以在 Cursor 中使用本服务器提供的工具
- 提问:
- 纽约今天天气怎么样?
- 请根据我给出的java类定义,输出一个示例json
Comments
New to MCP?
From what the protocol is to installing your first server, these answers cover most first questions.
An open protocol that defines how AI applications connect to external tools and data in a uniform way. Clients talk to the model; MCP servers provide tools, resources and prompt templates. They exchange standard messages, so one server can be reused by many clients.