This repository provides a Spring AI-based Model Context Protocol (MCP) client for interacting with MCP-supported servers, enabling AI chat interactions using Anthropic and OpenAI models. This repository provides a Spring
This Model Context Protocol (MCP) client application leverages Spring AI and Anthropic Claude (or OpenAI) models, integrating with MCP protocol-supported servers for AI-powered chat interactions.
The MCP Client facilitates connections with MCP servers, implementing the client-side protocol by handling version and capability negotiation, message transport via JSON-RPC, tool discovery/execution, resource access, prompt system interactions, and optional features like roots management and sampling support. It supports both OpenAI and Anthropic models.
Java 17+
Maven or Gradle
Docker (Optional)
Anthropic API key Or OpenAI API key    git clone <repo-url>
    cd spring-ai-mcp-clientConfigure the mcp-servers.json file with MCP server details, including the command and arguments to execute the server.
{
    "mcpServers":{
        "any-mcp-server":{
          "command":"node",
          "args":[
            "any-mcp-server/build/index.js"
          ]
        }
    }
}Configure application.yml to enable the MCP client, specify the server name, version, type (SYNC), request timeout, and the location of the server configuration file. Provide your Anthropic API key (or OpenAI API key).
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: 8081mvn clean install
mvn spring-boot:runAccess the running application via http://localhost:8081/ai. Initiate a chat using a cURL command like:
curl -X GET "http://localhost:8081/ai?message=Hello" -H "Accept: application/json"
ogulcanarbc/spring-ai-mcp-client
February 22, 2025
March 28, 2025
Java