Model Context Protocol (MCP)

New to MCP? Start with our Getting Started with MCP guide for a quick introduction and hands-on examples.

The Model Context Protocol (MCP) is a standardized protocol that enables AI models to interact with external tools and resources in a structured way. Think of it as a bridge between your AI models and the real world - allowing them to access databases, APIs, file systems, and other external services through a consistent interface. It supports multiple transport mechanisms to provide flexibility across different environments.spring-doc.cn

The MCP Java SDK provides a Java implementation of the Model Context Protocol, enabling standardized interaction with AI models and tools through both synchronous and asynchronous communication patterns.spring-doc.cn

Spring AI embraces MCP with comprehensive support through dedicated Boot Starters and MCP Java Annotations, making it easier than ever to build sophisticated AI-powered applications that can seamlessly connect to external systems. This means Spring developers can participate in both sides of the MCP ecosystem - building AI applications that consume MCP servers and creating MCP servers that expose Spring-based services to the wider AI community. Bootstrap your AI applications with MCP support using Spring Initializer.spring-doc.cn

MCP Java SDK Architecture

This section provides an overview for the MCP Java SDK architecture. For the Spring AI MCP integration, refer to the Spring AI MCP Boot Starters documentation.

The Java MCP implementation follows a three-layer architecture that separates concerns for maintainability and flexibility:spring-doc.cn

MCP Stack Architecture
Figure 1. MCP Stack Architecture

Client/Server Layer (Top)

The top layer handles the main application logic and protocol operations:spring-doc.cn

  • McpClient - Manages client-side operations and server connectionsspring-doc.cn

  • McpServer - Handles server-side protocol operations and client requestsspring-doc.cn

  • Both components utilize the session layer below for communication managementspring-doc.cn

Session Layer (Middle)

The middle layer manages communication patterns and maintains connection state:spring-doc.cn

  • McpSession - Core session management interfacespring-doc.cn

  • McpClientSession - Client-specific session implementationspring-doc.cn

  • McpServerSession - Server-specific session implementationspring-doc.cn

Transport Layer (Bottom)

The bottom layer handles the actual message transport and serialization:spring-doc.cn

  • McpTransport - Manages JSON-RPC message serialization and deserializationspring-doc.cn

  • Supports multiple transport implementations (STDIO, HTTP/SSE, Streamable-HTTP, etc.)spring-doc.cn

  • Provides the foundation for all higher-level communicationspring-doc.cn

MCP Client

The MCP Client is a key component in the Model Context Protocol (MCP) architecture, responsible for establishing and managing connections with MCP servers. It implements the client-side of the protocol, handling:spring-doc.cn

Java MCP Client Architecture
MCP Server

The MCP Server is a foundational component in the Model Context Protocol (MCP) architecture that provides tools, resources, and capabilities to clients. It implements the server-side of the protocol, responsible for:spring-doc.cn

Java MCP Server Architecture

For detailed implementation guidance, using the low-level MCP Client/Server APIs, refer to the MCP Java SDK documentation. For simplified setup using Spring Boot, use the MCP Boot Starters described below.spring-doc.cn

Spring AI MCP Integration

Spring AI provides MCP integration through the following Spring Boot starters:spring-doc.cn

Client Starters

  • spring-ai-starter-mcp-client - Core starter providing STDIO, Servlet-based Streamable-HTTP, Stateless Streamable-HTTP and SSE supportspring-doc.cn

  • spring-ai-starter-mcp-client-webflux - WebFlux-based Streamable-HTTP, Stateless Streamable-HTTP and SSE transport implementationspring-doc.cn

Server Starters

STDIO

Server Type Dependency Property

Standard Input/Output (STDIO)spring-doc.cn

spring-ai-starter-mcp-serverspring-doc.cn

spring.ai.mcp.server.stdio=truespring-doc.cn

WebMVC

Server Typespring-doc.cn

Dependencyspring-doc.cn

Propertyspring-doc.cn

SSE WebMVCspring-doc.cn

spring-ai-starter-mcp-server-webmvcspring-doc.cn

spring.ai.mcp.server.protocol=SSE or emptyspring-doc.cn

Streamable-HTTP WebMVCspring-doc.cn

spring-ai-starter-mcp-server-webmvcspring-doc.cn

spring.ai.mcp.server.protocol=STREAMABLEspring-doc.cn

Stateless Streamable-HTTP WebMVCspring-doc.cn

spring-ai-starter-mcp-server-webmvcspring-doc.cn

spring.ai.mcp.server.protocol=STATELESSspring-doc.cn

WebMVC (Reactive)

Server Typespring-doc.cn

Dependencyspring-doc.cn

Propertyspring-doc.cn

SSE WebFluxspring-doc.cn

spring-ai-starter-mcp-server-webfluxspring-doc.cn

spring.ai.mcp.server.protocol=SSE or emptyspring-doc.cn

Streamable-HTTP WebFluxspring-doc.cn

spring-ai-starter-mcp-server-webfluxspring-doc.cn

spring.ai.mcp.server.protocol=STREAMABLEspring-doc.cn

Stateless Streamable-HTTP WebFluxspring-doc.cn

spring-ai-starter-mcp-server-webfluxspring-doc.cn

spring.ai.mcp.server.protocol=STATELESSspring-doc.cn

Spring AI MCP Annotations

In addition to the programmatic MCP client & server configuration, Spring AI provides annotation-based method handling for MCP servers and clients through the MCP Annotations module. This approach simplifies the creation and registration of MCP operations using a clean, declarative programming model with Java annotations.spring-doc.cn

The MCP Annotations module enables developers to:spring-doc.cn

  • Create MCP tools, resources, and prompts using simple annotationsspring-doc.cn

  • Handle client-side notifications and requests declarativelyspring-doc.cn

  • Reduce boilerplate code and improve maintainabilityspring-doc.cn

  • Automatically generate JSON schemas for tool parametersspring-doc.cn

  • Access special parameters and context informationspring-doc.cn

Key features include:spring-doc.cn