spring-ai-docs/src/main/asciidoc/mcp.md
The Spring AI MCP module provides integration with the Model Context Protocol, allowing you to expose your AI tools and resources through a standardized protocol. This module is particularly useful when you want to make your Spring AI tools and resources available to MCP-compatible clients.
To use the MCP server functionality, add the following dependency to your project:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server</artifactId>
<version>${spring-ai.version}</version>
</dependency>
The MCP server can be configured using the following properties under the spring.ai.mcp.server prefix:
| Property | Default | Description |
|---|---|---|
enabled | false | Enable/disable the MCP server |
name | "mcp-server" | Name of the MCP server |
version | "1.0.0" | Version of the MCP server |
type | SYNC | Server type (SYNC or ASYNC) |
resource-change-notification | true | Enable/disable resource change notifications |
tool-change-notification | true | Enable/disable tool change notifications |
prompt-change-notification | true | Enable/disable prompt change notifications |
transport | STDIO | Transport type (STDIO, WEBMVC, or WEBFLUX) |
sse-message-endpoint | "/mcp/message" | Server-Sent Events (SSE) message endpoint for web transports |
The MCP server supports two operation modes:
The synchronous mode is the default option, suitable for most use cases where tools and resources are accessed sequentially:
spring:
ai:
mcp:
server:
type: SYNC
The asynchronous mode is designed for reactive applications and scenarios requiring non-blocking operations:
spring:
ai:
mcp:
server:
type: ASYNC
The MCP server supports three transport types:
The Standard Input/Output transport is the default option, suitable for command-line tools and local development:
spring:
ai:
mcp:
server:
transport: STDIO
The WebMvc transport uses Spring MVC's Server-Sent Events (SSE) for communication:
spring:
ai:
mcp:
server:
transport: WEBMVC
sse-message-endpoint: /mcp/message # Optional, defaults to /mcp/message
Required dependencies:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>mcp-spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
The WebFlux transport uses Spring WebFlux's Server-Sent Events for reactive communication:
spring:
ai:
mcp:
server:
transport: WEBFLUX
sse-message-endpoint: /mcp/message # Optional, defaults to /mcp/message
Required dependencies:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>mcp-spring-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
The MCP server provides several core features:
Here's an example of configuring the MCP server with WebMvc transport and custom settings:
spring:
ai:
mcp:
server:
enabled: true
name: "My AI Tools Server"
version: "1.0.0"
type: SYNC
transport: WEBMVC
sse-message-endpoint: /ai/mcp/events
resource-change-notification: true
tool-change-notification: true
prompt-change-notification: false
The MCP server auto-configuration is provided through:
McpServerAutoConfiguration: Core server configuration supporting both sync and async modesMcpWebMvcServerAutoConfiguration: WebMvc transport configuration (activated when WebMvc dependencies are present)McpWebFluxServerAutoConfiguration: WebFlux transport configuration (activated when WebFlux dependencies are present)The auto-configuration will automatically set up the appropriate server type and transport based on your configuration and available dependencies.
To expose your Spring AI tools and resources through the MCP server:
ToolCallback interface for your AI tools:@Component
public class MyAiTool implements ToolCallback {
// Implementation
}
The MCP server provides notifications for changes in:
You can enable/disable these notifications using the configuration properties. The notification system works with both sync and async server types, providing consistent change tracking regardless of the chosen operation mode.