内容
# 注意:该项目已经毕业并迁移到 [MCP Java SDK](https://github.com/modelcontextprotocol/java-sdk/tree/main/mcp) 和 [Spring AI MCP](https://docs.spring.io/spring-ai/reference/api/mcp/mcp-overview.html)。请到那里见面!该仓库现已归档。
# Java & Spring MCP
[](https://github.com/spring-projects-experimental/spring-ai-mcp/actions/workflows/continuous-integration.yml)
一组提供Java SDK和Spring框架集成的项目,适用于 [模型上下文协议](https://modelcontextprotocol.org/docs/concepts/architecture)。它使Java应用程序能够通过标准化的接口与AI模型和工具交互,支持同步和异步通信模式。
## 📚 参考文档
有关综合指南和API文档,请访问 [Spring AI MCP 参考文档](https://docs.spring.io/spring-ai-mcp/reference/overview.html)。
<img src="mcp-docs/src/main/antora/modules/ROOT/images/spring-ai-mcp-clinet-architecture.jpg" width="600">
<img src="mcp-docs/src/main/antora/modules/ROOT/images/spring-ai-mcp-server-architecture.jpg" width="600">
## 项目
### [MCP Java SDK](https://docs.spring.io/spring-ai-mcp/reference/mcp.html)
模型上下文协议规范的Java实现。它包括:
- 同步和异步 [MCP 客户端](https://docs.spring.io/spring-ai-mcp/reference/mcp.html#_mcp_client) 和 [MCP 服务器](https://docs.spring.io/spring-ai-mcp/reference/mcp.html#_mcp_server) 实现
- 标准MCP操作支持(工具发现、资源管理、提示处理、结构化日志记录)。支持请求和通知处理。
- [标准输入输出](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#stdio) 和 [服务器发送事件](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse) 传输实现。
### MCP 传输
#### 核心传输
- 基于标准输入输出 (`StdioClientTransport`,`StdioServerTransport`) 用于基于进程的通信
- 基于Java HttpClient的服务器发送事件客户端 (`HttpClientSseClientTransport`) 用于HTTP流媒体
- 基于Servlet的服务器发送事件服务器 (`HttpServletSseServerTransport`) 用于HTTP SSE服务器流媒体,使用传统的Servlet API
#### 可选的服务器发送事件传输
- [WebFlux SSE 传输](https://github.com/spring-projects-experimental/spring-ai-mcp/tree/main/mcp-transport/mcp-webflux-sse-transport) - 使用Spring WebFlux的响应式HTTP流媒体(客户端和服务器)
- [WebMvc SSE 传输](https://github.com/spring-projects-experimental/spring-ai-mcp/tree/main/mcp-transport/mcp-webmvc-sse-transport) - 基于Spring MVC的HTTP SSE传输(仅服务器)。您可以使用核心 `HttpClientSseClientTransport` 传输作为SSE客户端。
### [Spring AI MCP](https://docs.spring.io/spring-ai-mcp/reference/spring-mcp.html)
Spring集成模块提供Spring特定的功能:
- 与Spring AI的功能调用系统的集成
- MCP客户端的Spring友好抽象
- 自动配置(待完善)
## 安装
将以下依赖项添加到您的Maven项目中:
```xml
<!-- 核心MCP -->
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>mcp</artifactId>
</dependency>
<!-- 可选:WebFlux SSE传输 -->
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>mcp-webflux-sse-transport</artifactId>
</dependency>
<!-- 可选:WebMVC SSE传输 -->
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>mcp-webmvc-sse-transport</artifactId>
</dependency>
<!-- 可选:Spring AI集成 -->
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-ai-mcp</artifactId>
</dependency>
```
这是里程碑版本,在Maven Central上不可用。
将此仓库添加到您的POM中:
```xml
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
```
请参阅 [依赖管理](https://docs.spring.io/spring-ai-mcp/reference/dependency-management.html) 页面以获取更多信息。
## 示例演示
在 [spring-ai-examples/model-context-protocol](https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol) 仓库中探索这些MCP示例:
- [SQLite 简单示例](https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/sqlite/simple) - 演示LLM与数据库的集成
- [SQLite 聊天机器人](https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/sqlite/chatbot) - 交互式聊天机器人与SQLite数据库交互
- [文件系统](https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/filesystem) - 使LLM能够与本地文件系统文件夹和文件交互
- [Brave](https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/brave) - 使自然语言交互与Brave Search结合,允许您执行互联网搜索。
- [主题公园API示例](https://github.com/habuma/spring-ai-examples/tree/main/spring-ai-mcp) - 展示如何使用Spring AI创建MCP服务器和客户端,公开主题公园API工具
- [Http SSE客户端 + WebMvc SSE服务器](https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/mcp-webmvc-server) - 展示如何使用不同功能的MCP WebMvc服务器和HttpClient客户端。
- [WebFlux SSE客户端 + WebFlux SSE服务器](https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/mcp-webflux-server) - 展示如何使用不同功能的MCP WebFlux服务器和客户端
- [HttpClient SSE客户端 + Servlet SSE服务器](https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/mcp-servlet-server) - 展示如何使用不同功能的MCP Servlet SSE服务器和HttpClient SSE客户端
## 文档
- [Java MCP SDK文档](mcp/README.md)
- [参考文档](docs/ref-index.md)
- [Spring集成文档](spring-ai-mcp/README.md)
## 开发
- 从源代码构建
```bash
mvn clean install
```
- 运行测试
```bash
mvn test
```
## 贡献
这是一个实验性的Spring项目。欢迎贡献!请:
1. 分叉仓库
2. 创建功能分支
3. 提交拉取请求
## 团队
- 克里斯蒂安·茨洛夫(Christian Tzolov)
- Dariusz Jędrzejczyk
## 链接
- [GitHub仓库](https://github.com/spring-projects-experimental/spring-ai-mcp)
- [问题跟踪器](https://github.com/spring-projects-experimental/spring-ai-mcp/issues)
- [CI/CD](https://github.com/spring-projects-experimental/spring-ai-mcp/actions)
## 许可证
该项目在 [Apache License 2.0](LICENSE) 下获得许可。