Spring AI Spring Boot Auto Configuration modules providing automatic setup for AI models, vector stores, MCP, and retry capabilities
MCP server autoconfiguration provides automatic setup for exposing Spring AI capabilities as MCP servers with support for multiple transport mechanisms and both synchronous and asynchronous operation modes.
MCP server modules enable Spring Boot applications to expose tools, prompts, and resources via the Model Context Protocol. The autoconfiguration handles server lifecycle, transport selection, and integration with Spring AI components with comprehensive annotation support and automatic registration.
MCP server functionality is split across three modules for flexibility:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-mcp-server-common</artifactId>
<version>1.1.2</version>
</dependency><dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-mcp-server-webflux</artifactId>
<version>1.1.2</version>
</dependency><dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-mcp-server-webmvc</artifactId>
<version>1.1.2</version>
</dependency>Main autoconfiguration for MCP server support.
/**
* Autoconfigures Model Context Protocol (MCP) server support
*
* Conditional Requirements:
* - @ConditionalOnClass: io.modelcontextprotocol.spec.McpSchema
* - @ConditionalOnProperty: spring.ai.mcp.server.enabled=true (default)
*
* Configuration Properties: spring.ai.mcp.server.*
*
* @AutoConfiguration
* @ConditionalOnClass(McpSchema.class)
* @EnableConfigurationProperties(McpServerProperties.class)
*/
@AutoConfiguration
@ConditionalOnClass(McpSchema.class)
@EnableConfigurationProperties(McpServerProperties.class)
class McpServerAutoConfiguration {
// Bean definitions for MCP server infrastructure
}
/**
* Stdio server transport provider
* Only created when transport=STDIO
*
* @Bean
* @ConditionalOnProperty(prefix = "spring.ai.mcp.server", name = "transport",
* havingValue = "STDIO")
* @return Stdio transport provider for process-based communication
*/
@Bean
@ConditionalOnProperty(prefix = "spring.ai.mcp.server", name = "transport",
havingValue = "STDIO")
McpServerTransportProviderBase stdioServerTransport();
/**
* Builder for server capabilities configuration
*
* @Bean
* @ConditionalOnMissingBean
* @return Builder for customizing server capabilities
*/
@Bean
@ConditionalOnMissingBean
McpSchema.ServerCapabilities.Builder capabilitiesBuilder();
/**
* Synchronous MCP server
* Created when type=SYNC (default)
*
* @Bean
* @ConditionalOnProperty(prefix = "spring.ai.mcp.server", name = "type",
* havingValue = "SYNC", matchIfMissing = true)
* @param transport Server transport provider (stdio, SSE, HTTP)
* @param specFactory Factory for creating server specification from registered components
* @param properties Server configuration properties
* @return Configured McpSyncServer instance
*/
@Bean
@ConditionalOnProperty(prefix = "spring.ai.mcp.server", name = "type",
havingValue = "SYNC", matchIfMissing = true)
McpSyncServer mcpSyncServer(
McpServerTransportProviderBase transport,
McpServerSpecificationFactory specFactory,
McpServerProperties properties
);
/**
* Asynchronous MCP server
* Created when type=ASYNC
*
* @Bean
* @ConditionalOnProperty(prefix = "spring.ai.mcp.server", name = "type",
* havingValue = "ASYNC")
* @param transport Server transport provider
* @param specFactory Factory for creating server specification
* @param properties Server configuration properties
* @return Configured McpAsyncServer instance
*/
@Bean
@ConditionalOnProperty(prefix = "spring.ai.mcp.server", name = "type",
havingValue = "ASYNC")
McpAsyncServer mcpAsyncServer(
McpServerTransportProviderBase transport,
McpServerSpecificationFactory specFactory,
McpServerProperties properties
);Configures Jackson ObjectMapper for MCP server JSON serialization.
/**
* Autoconfigures ObjectMapper for MCP server
*
* @AutoConfiguration
* @ConditionalOnClass(McpSchema.class)
*/
@AutoConfiguration
@ConditionalOnClass(McpSchema.class)
class McpServerObjectMapperAutoConfiguration {
// Bean definitions for JSON serialization
}
/**
* Configured Jackson ObjectMapper for MCP server
* Can be overridden by providing a bean named "mcpServerObjectMapper"
*
* @Bean
* @ConditionalOnMissingBean(name = "mcpServerObjectMapper")
* @return ObjectMapper with MCP-specific configuration
*/
@Bean
@ConditionalOnMissingBean(name = "mcpServerObjectMapper")
ObjectMapper mcpServerObjectMapper();Scans for and registers MCP server tool/prompt/resource annotations.
/**
* Scans for MCP server annotations (@McpTool, @McpPrompt, @McpResource)
*
* Conditional Requirements:
* - @ConditionalOnClass: McpTool
* - @ConditionalOnProperty: spring.ai.mcp.server.annotation-scanner.enabled=true (default)
*
* @AutoConfiguration
* @ConditionalOnClass(McpTool.class)
*/
@AutoConfiguration
@ConditionalOnClass(McpTool.class)
class McpServerAnnotationScannerAutoConfiguration {
// Bean definitions for annotation scanning
}
/**
* Registry for MCP server tools
* Collects all @McpTool annotated methods
*
* @Bean
* @return Registry for server tools
*/
@Bean
ServerMcpToolsRegistry serverMcpToolsRegistry();
/**
* Registry for MCP server prompts
* Collects all @McpPrompt annotated methods
*
* @Bean
* @return Registry for server prompts
*/
@Bean
ServerMcpPromptsRegistry serverMcpPromptsRegistry();
/**
* Registry for MCP server resources
* Collects all @McpResource annotated methods
*
* @Bean
* @return Registry for server resources
*/
@Bean
ServerMcpResourcesRegistry serverMcpResourcesRegistry();Configuration prefix: spring.ai.mcp.server
/**
* Properties for MCP server configuration
*
* @ConfigurationProperties(prefix = "spring.ai.mcp.server")
*/
class McpServerProperties {
/** Enable/disable the MCP server (default: true) */
private boolean enabled = true;
/** Name of the MCP server instance (default: "spring-ai-mcp-server") */
private String name = "spring-ai-mcp-server";
/** Version of the MCP server instance (default: "1.0.0") */
private String version = "1.0.0";
/** Server type: SYNC or ASYNC (default: SYNC) */
private ServerType type = ServerType.SYNC;
/** Transport type: STDIO, SSE, or STREAMABLE_HTTP (default: STDIO) */
private TransportType transport = TransportType.STDIO;
/** Change notification configuration */
private ChangeNotification changeNotification = new ChangeNotification();
enum ServerType { SYNC, ASYNC }
enum TransportType { STDIO, SSE, STREAMABLE_HTTP }
static class ChangeNotification {
/** Master switch for change notifications (default: true) */
private boolean enabled = true;
/** Enable tool list change notifications (default: true) */
private boolean tools = true;
/** Enable prompt list change notifications (default: true) */
private boolean prompts = true;
/** Enable resource list change notifications (default: true) */
private boolean resources = true;
}
}# application.properties
spring.ai.mcp.server.enabled=true
spring.ai.mcp.server.type=SYNC
spring.ai.mcp.server.transport=STDIO
spring.ai.mcp.server.name=my-mcp-server
spring.ai.mcp.server.version=1.0.0# application.yml
spring:
ai:
mcp:
server:
enabled: true
type: SYNC
transport: SSE
name: my-mcp-server
sse:
endpoint: /mcp/ssespring.ai.mcp.server.enabled=true
spring.ai.mcp.server.type=SYNC
spring.ai.mcp.server.transport=STREAMABLE_HTTP
spring.ai.mcp.server.streamable-http.endpoint=/api/mcpimport org.springframework.ai.mcp.server.annotation.McpTool;
import org.springframework.stereotype.Component;
@Component
public class FileSystemTools {
@McpTool(
name = "read_file",
description = "Read contents of a file"
)
public String readFile(String path) {
// Read file implementation
return Files.readString(Path.of(path));
}
@McpTool(
name = "list_directory",
description = "List files in a directory"
)
public List<String> listDirectory(String path) {
// List directory implementation
return Arrays.stream(new File(path).listFiles())
.map(File::getName)
.collect(Collectors.toList());
}
}import org.springframework.ai.mcp.server.annotation.McpPrompt;
import org.springframework.stereotype.Component;
@Component
public class CodePrompts {
@McpPrompt(
name = "code_review",
description = "Generate a code review prompt"
)
public String codeReviewPrompt(String language, String code) {
return String.format(
"Review this %s code:\n\n%s\n\n" +
"Provide feedback on:\n" +
"1. Code quality\n" +
"2. Best practices\n" +
"3. Potential issues",
language, code
);
}
}import org.springframework.ai.mcp.server.annotation.McpResource;
import org.springframework.stereotype.Component;
@Component
public class DocumentationResources {
@McpResource(
uri = "docs://api/overview",
description = "API documentation overview"
)
public String getApiOverview() {
return "API Overview: This API provides...";
}
@McpResource(
uri = "docs://api/{endpoint}",
description = "Documentation for specific endpoint"
)
public String getEndpointDocs(String endpoint) {
return "Documentation for: " + endpoint;
}
}MCP server autoconfiguration provides comprehensive support for exposing Spring AI capabilities as MCP servers with: