MCP Server using Spring Boot Java

MCP Server using Spring Boot Java

site icon
2025.03.20 2
Java用户管理开发效率
MCP Server using Spring Boot Java 是一个基于Spring Boot和Java 21的Model Context Protocol (MCP)服务器实现。该项目展示了如何创建一个MCP服务器,为AI助手提供用户管理工具。
View on GitHub

Overview

基本能力

产品定位

MCP Server using Spring Boot Java 是一个基于Spring Boot和Java 21的Model Context Protocol (MCP)服务器实现,主要用于为AI助手提供用户管理工具。

核心功能

  • 使用Spring AI实现MCP服务器功能
  • 通过MCP暴露用户管理工具
  • 同步通信模式
  • 支持Standard I/O和Server-Sent Events传输
  • 集成DummyJSON外部API获取用户数据

适用场景

  • AI助手的用户管理
  • 需要同步通信的应用
  • 需要集成外部API的应用

工具列表

  • getAllUsers - 获取所有用户(带分页)
  • getAllUsersDefault - 获取所有用户(默认分页)
  • getUserById - 通过ID获取单个用户
  • searchUsers - 通过查询搜索用户
  • addUser - 添加新用户
  • updateUser - 更新用户
  • deleteUser - 删除用户

常见问题解答

使用教程

使用依赖

  • Java 21或更高版本
  • Maven

安装教程

  1. 克隆仓库
git clone https://github.com/yourusername/mcp-spring-java.git
cd mcp-spring-java
  1. 构建项目
mvn clean package
  1. 运行应用
java -jar target/mcp-spring-java-0.0.1-SNAPSHOT.jar

调试方式

可以使用Postman测试MCP服务器端点: 1. 启动MCP服务器 2. 在Postman中创建新请求: - 方法: POST - URL: http://localhost:8090/mcp/message - Headers: - Content-Type: application/json - Accept: application/json 3. 请求体使用JSON格式调用任何可用工具

示例请求:

获取所有用户(默认分页)

{
  "message": {
    "toolCalls": [
      {
        "id": "call-123",
        "name": "getAllUsersDefault",
        "parameters": {}
      }
    ]
  }
}

通过ID获取用户

{
  "message": {
    "toolCalls": [
      {
        "id": "call-123",
        "name": "getUserById",
        "parameters": {
          "id": 1
        }
      }
    ]
  }
}

搜索用户

{
  "message": {
    "toolCalls": [
      {
        "id": "call-123",
        "name": "searchUsers",
        "parameters": {
          "query": "John"
        }
      }
    ]
  }
}

许可证

该项目遵循 Apache-2.0 开源许可条款,请参阅 Apache-2.0 了解完整条款。