Back to MCP Catalog

Spring AI SDK MCP Server

Developer ToolsJava
Java SDK for integrating Spring applications with MCP-compliant AI models and tools

The Spring AI MCP SDK provides a comprehensive Java implementation of the Model Context Protocol (MCP) specification. It enables Java and Spring applications to seamlessly interact with AI models and tools through a standardized interface, supporting both synchronous and asynchronous communication patterns. The SDK includes client and server implementations, multiple transport options, and tight integration with Spring's ecosystem.

Overview

The Spring AI MCP SDK is a Java implementation of the Model Context Protocol (MCP) specification, designed to enable seamless integration between Java/Spring applications and MCP-compliant AI models and tools. This SDK supports both synchronous and asynchronous communication patterns and provides multiple transport options for flexibility in different deployment scenarios.

Key Components

The SDK consists of several key components:

  1. MCP Java SDK: Core implementation of the Model Context Protocol specification

    • Synchronous and asynchronous client and server implementations
    • Support for standard MCP operations (tool discovery, resource management, prompt handling, structured logging)
    • Request and notification handling capabilities
  2. MCP Transports: Multiple transport implementations

    • Stdio-based transport for process-based communication
    • HTTP SSE (Server-Sent Events) transports for streaming communication
    • WebFlux and WebMVC integrations for reactive and traditional Spring applications
  3. Spring AI MCP: Spring-specific integration

    • Integration with Spring AI's function calling system
    • Spring-friendly abstractions for MCP clients
    • Auto-configurations for easy setup

Installation

To use the Spring AI MCP SDK in your Maven project, add the following dependencies:

<!-- Core MCP -->
<dependency>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>mcp</artifactId>
    <version>0.1.0</version>
</dependency>

<!-- Choose one of the transport options based on your needs -->

<!-- For Stdio transport (process-based communication) -->
<!-- This is included in the core dependency -->

<!-- For HTTP SSE client transport -->
<dependency>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>mcp-http-sse-transport</artifactId>
    <version>0.1.0</version>
</dependency>

<!-- For WebFlux SSE transport (reactive applications) -->
<dependency>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>mcp-webflux-sse-transport</artifactId>
    <version>0.1.0</version>
</dependency>

<!-- For WebMVC SSE transport (traditional Spring MVC applications) -->
<dependency>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>mcp-webmvc-sse-transport</artifactId>
    <version>0.1.0</version>
</dependency>

<!-- Spring AI MCP integration -->
<dependency>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>spring-ai-mcp</artifactId>
    <version>0.1.0</version>
</dependency>

For Gradle projects, use the equivalent dependencies:

// Core MCP
implementation 'org.springframework.experimental:mcp:0.1.0'

// Choose transport options as needed
implementation 'org.springframework.experimental:mcp-http-sse-transport:0.1.0'
implementation 'org.springframework.experimental:mcp-webflux-sse-transport:0.1.0'
implementation 'org.springframework.experimental:mcp-webmvc-sse-transport:0.1.0'

// Spring AI MCP integration
implementation 'org.springframework.experimental:spring-ai-mcp:0.1.0'

Using the MCP Client

Here's a basic example of using the MCP client with the Stdio transport:

import org.springframework.experimental.mcp.client.McpClient;
import org.springframework.experimental.mcp.transport.stdio.StdioClientTransport;

// Create a client with Stdio transport
ProcessBuilder processBuilder = new ProcessBuilder("path/to/mcp/server");
StdioClientTransport transport = new StdioClientTransport(processBuilder);
McpClient client = new McpClient(transport);

// Initialize the client
client.initialize();

// Send a prompt
String response = client.prompt("Tell me about the Model Context Protocol");

// Clean up
client.close();

For HTTP SSE transport:

import org.springframework.experimental.mcp.client.McpClient;
import org.springframework.experimental.mcp.transport.http.HttpClientSseClientTransport;

// Create a client with HTTP SSE transport
HttpClientSseClientTransport transport = new HttpClientSseClientTransport("http://localhost:8080/mcp");
McpClient client = new McpClient(transport);

// Initialize the client
client.initialize();

// Send a prompt
String response = client.prompt("Tell me about the Model Context Protocol");

// Clean up
client.close();

Implementing an MCP Server

To implement an MCP server:

import org.springframework.experimental.mcp.server.McpServer;
import org.springframework.experimental.mcp.transport.stdio.StdioServerTransport;

// Create a server with Stdio transport
StdioServerTransport transport = new StdioServerTransport();
McpServer server = new McpServer(transport);

// Register a prompt handler
server.setPromptHandler(prompt -> {
    // Process the prompt and return a response
    return "Response to: " + prompt;
});

// Start the server
server.start();

// The server will run until the process is terminated

Spring Integration

For Spring applications, you can use the Spring AI MCP integration:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.experimental.ai.mcp.McpClient;
import org.springframework.stereotype.Service;

@Service
public class MyAiService {
    
    private final McpClient mcpClient;
    
    @Autowired
    public MyAiService(McpClient mcpClient) {
        this.mcpClient = mcpClient;
    }
    
    public String getAiResponse(String prompt) {
        return mcpClient.prompt(prompt);
    }
}

With Spring Boot auto-configuration (work in progress), you'll be able to configure the MCP client through application properties:

spring.ai.mcp.transport=http-sse
spring.ai.mcp.endpoint=http://localhost:8080/mcp

Additional Resources

For more detailed information, refer to the Spring AI MCP Reference Documentation, which includes comprehensive guides and API documentation.

Related MCPs

Apple Shortcuts
Developer ToolsJavaScript

Control Apple Shortcuts automations from AI assistants

Clojars Dependency Lookup
Developer ToolsJavaScript

Fetch dependency information from Clojars, the Clojure community's artifact repository

Simple Timeserver
Developer ToolsPython

Provides Claude with current time and timezone information

About Model Context Protocol

Model Context Protocol (MCP) allows AI models to access external tools and services, extending their capabilities beyond their training data.

Generate Cursor Documentation

Save time on coding by generating custom documentation and prompts for Cursor IDE.