AI聊天客户端

基于Spring AI和Anthropic Claude模型(或OpenAI)的MCP协议客户端,用于与支持MCP协议的服务器集成,实现AI驱动的聊天交互。

  • by ogulcanarbc
  • Java
  • 本地部署
  • 44 星标
  • 更新于
在 GitHub 查看 ogulcanarbc/spring-ai-mcp-client

使用说明

spring ai mcp客户端

模型上下文协议(MCP)客户端应用程序利用Spring AI和Anthropic Claude模型(或OpenAI)。它与支持MCP协议的服务器集成,以实现AI驱动的聊天交互。

什么是Spring MCP客户端

MCP客户端是模型上下文协议(MCP)架构中的关键组件,负责建立和管理与MCP服务器的连接。 它实现了协议的客户端,处理:

- Protocol version negotiation to ensure compatibility with servers
- Capability negotiation to determine available features
- Message transport and JSON-RPC communication
- Tool discovery and execution
- Resource access and management
- Prompt system interactions
- Optional features like roots management and sampling support
- Supports openai and anthropic models

需求

Java 17+
Maven or Gradle
Docker (Optional)
Anthropic API key Or OpenAI API key

安装

1.克隆存储库

    git clone <repo-url>
    cd spring-ai-mcp-client

2.MCP服务器配置

MCP服务器信息必须按如下方式添加到MCP-servers.json文件中。

{
    "mcpServers":{
        "any-mcp-server":{
          "command":"node",
          "args":[
            "any-mcp-server/build/index.js"
          ]
        }
    }
}

3.弹簧配置

应按如下方式配置application.yml文件:

spring:
  ai:
    mcp:
      client:
        enabled: true
        name: any-mcp-server # MCP server name
        version: 1.0.0
        type: SYNC
        request-timeout: 20s
        stdio:
          root-change-notification: true
          servers-configuration: classpath:mcp-servers.json # MCP server config such/same as claude desktop configs.
    anthropic:
      api-key: ${ANTHROPIC_API_KEY}
#   openai:
#     api-key:  ${OPENAI_API_KEY}  
server:
  port: 8081

4.运行应用程序

mvn clean install
mvn spring-boot:run

用法

应用程序运行后,可以通过以下端点访问它:

  • http://localhost:8091/ai

您可以使用以下cURL命令开始聊天:

curl -X GET "http://localhost:8081/ai?message=Hello" -H "Accept: application/json"