Back to MCP directory
publicPublicdnsLocal runtime

spring-ai-mcp

Java与Spring框架对Model Context Protocol(MCP)的实现,提供标准化接口与AI模型交互,支持同步/异步通信模式。

article

README

🚀 Java & Spring MCP

这是一个用于 Java 和 Spring 框架与 Model Context Protocol (MCP) 集成的库。MCP 协议允许语言模型与外部工具和数据源交互,本项目提供了 Java 开发工具包 (SDK) 以及 Spring AI 的集成模块。

🚀 快速开始

本项目已毕业!请访问 新仓库 获取最新版本。

✨ 主要特性

  • 提供 Java 实现的 MCP 协议栈,包括核心协议和工具。
  • 支持多种传输层实现,如基于 HTTP 的 Server-Sent Events (SSE) 客户端、基于 Servlet 的 SSE 服务器等。
  • 集成 Spring AI,方便在 Spring 项目中使用。

📦 安装指南

要将本项目添加到您的 Maven 项目中,请在 pom.xml 中添加以下依赖:

<dependency>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>mcp-core</artifactId>
    <version>{latest_version}</version>
</dependency>

<!-- 如果您需要 HTTP 客户端 -->
<dependency>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>mcp-http-client-sse</artifactId>
    <version>{latest_version}</version>
</dependency>

<!-- 如果您需要 Servlet 服务器 -->
<dependency>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>mcp-servlet-sse</artifactId>
    <version>{latest_version}</version>
</dependency>

<!-- 如果您使用 Spring Web MVC -->
<dependency>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>mcp-webmvc</artifactId>
    <version>{latest_version}</version>
</dependency>

<!-- 如果您使用 Spring WebFlux -->
<dependency>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>mcp-webflux</artifactId>
    <version>{latest_version}</version>
</dependency>

要配置 Maven 仓库,请将以下内容添加到 settings.xml

<mirror>
    <id>habuma-repo</id>
    <url>https://repo.habuma.dev/maven-snapshot/</url>
    <mirrorOf>*</mirrorOf>
</mirror>

更多详细信息请参阅 依赖管理

💻 使用示例

spring-ai-examples/model-context-protocol 仓库中,您可以找到以下 MCP 示例:

  • SQLite Simple:展示 LLM 如何与 SQLite 数据库集成。
  • SQLite Chatbot:创建一个交互式聊天机器人,能够与 SQLite 数据库进行交互。
  • Filesystem:允许 LLM 与本地文件系统中的文件和文件夹交互。
  • Brave:实现与 Brave Search 的自然语言交互,支持互联网搜索功能。
  • Theme Park API 示例:展示如何使用 Spring AI 创建 MCP 服务器和客户端,并集成 Theme Park API 工具。
  • Http SSE 客户端 + WebMvc 服务器:展示如何创建和使用具有不同功能的 MCP WebMvc 服务器和 HttpClient 客户端。
  • WebFlux SSE 客户端 + WebFlux 服务器:展示如何创建和使用具有不同功能的 MCP WebFlux 服务器和客户端。
  • HttpClient SSE 客户端 + Servlet 服务器:展示如何创建和使用具有不同功能的 MCP Servlet 服务器和 HttpClient 客户端。

📚 详细文档

🔧 技术细节

项目结构

核心 MCP 模块

  • mcp:提供 Java 实现的 MCP 协议栈,包括核心协议和工具。

传输层模块

  • mcp-http-client-sse:基于 HTTP 的 Server-Sent Events (SSE) 客户端实现。
  • mcp-servlet-sse:基于 Servlet 的 SSE 服务器实现。
  • mcp-webmvc:Spring Web MVC 集成模块,用于创建 MCP 服务器。
  • mcp-webflux:Spring WebFlux 集成模块,用于创建 MCP 服务器。

Spring AI 集成模块

  • spring-ai-mcp:Spring AI 的 MCP 集成模块。

开发

从源码构建

mvn clean install

运行测试

mvn test

🤝 贡献

这是一个实验性的 Spring 项目。欢迎贡献!请按照以下步骤提交代码:

  1. 叉叉仓库
  2. 创建功能分支
  3. 提交拉取请求

📄 许可证

文档中未提及许可证相关信息。

⚠️ 重要提示

本项目仍处于早期阶段,可能会有不稳定性。使用前请仔细阅读文档并测试。

help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client