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.
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.
The SDK consists of several key components:
MCP Java SDK: Core implementation of the Model Context Protocol specification
MCP Transports: Multiple transport implementations
Spring AI MCP: Spring-specific integration
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'
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();
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
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
For more detailed information, refer to the Spring AI MCP Reference Documentation, which includes comprehensive guides and API documentation.