Model Context Protocol (MCP)
Model Context Protocol (MCP) 是一种标准化协议,它使 AI 模型能够以结构化的方式与外部工具和资源进行交互。它支持多种传输机制,以在不同环境中提供灵活性。
The Model Context Protocol (MCP) is a standardized protocol that enables AI models to interact with external tools and resources in a structured way. It supports multiple transport mechanisms to provide flexibility across different environments.
MCP Java SDK 提供了模型上下文协议的 Java 实现,通过同步和异步通信模式实现与 AI 模型和工具的标准化交互。
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 AI MCP
通过 Spring Boot 集成扩展了 MCP Java SDK,提供了 client 和 server 启动器。使用 Spring Initializer 启动您的支持 MCP 的 AI 应用程序。
Spring AI MCP
extends the MCP Java SDK with Spring Boot integration, providing both client and server starters.
Bootstrap your AI applications with MCP support using Spring Initializer.
MCP Java SDK 0.8.0 中的重大变更 ⚠️ Breaking Changes in MCP Java SDK 0.8.0 ⚠️ MCP Java SDK 0.8.0 版引入了一些重大变更,包括新的基于会话的架构。如果您正在从 Java SDK 0.7.0 升级,请参阅 Migration Guide 以获取详细说明。 MCP Java SDK version 0.8.0 introduces several breaking changes including a new session-based architecture. If you’re upgrading from Java SDK 0.7.0, please refer to the Migration Guide for detailed instructions. |
MCP Java SDK Architecture
本节概述了 MCP Java SDK architecture 。有关 Spring AI MCP 集成,请参阅 Spring AI MCP Boot Starters 文档。 |
This section provides an overview for the MCP Java SDK architecture. For the Spring AI MCP integration, refer to the _spring_ai_mcp_integration documentation. |
Java MCP 实现遵循三层架构:
The Java MCP implementation follows a three-layer architecture:
image::mcp/mcp-stack.svg[MCP Stack Architecture] |
* Client/Server Layer: The McpClient handles client-side operations while the McpServer manages server-side protocol operations. Both utilize McpSession for communication management. * Session Layer (McpSession): Manages communication patterns and state through the DefaultMcpSession implementation. * Transport Layer (McpTransport): Handles JSON-RPC message serialization and deserialization with support for multiple transport implementations. |
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: * Protocol version negotiation to ensure compatibility with servers * Capability negotiation to determine available features * Message transport and JSON-RPC communication * Tool discovery and execution * Resource access and management * Prompt system interactions * Optional features: Roots management Sampling support * Synchronous and asynchronous operations * Transport options: Stdio-based transport for process-based communication Java HttpClient-based SSE client transport ** WebFlux SSE client transport for reactive HTTP streaming |
image::mcp/java-mcp-client-architecture.jpg[Java MCP Client Architecture, width=500] |
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: * Server-side protocol operations implementation Tool exposure and discovery Resource management with URI-based access Prompt template provision and handling Capability negotiation with clients Structured logging and notifications * Concurrent client connection management * Synchronous and Asynchronous API support * Transport implementations: Stdio-based transport for process-based communication Servlet-based SSE server transport WebFlux SSE server transport for reactive HTTP streaming ** WebMVC SSE server transport for servlet-based HTTP streaming |
image::mcp/java-mcp-server-architecture.jpg[Java MCP Server Architecture, width=600] |
有关详细的实现指导,使用低级 MCP 客户端/服务器 API,请参阅 MCP Java SDK documentation 。有关使用 Spring Boot 进行简化设置,请使用下面描述的 MCP Boot Starters。
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 AI MCP Integration
Spring AI 通过以下 Spring Boot starters 提供 MCP 集成:
Spring AI provides MCP integration through the following Spring Boot starters:
Client Starters
-
spring-ai-starter-mcp-client
- 提供 STDIO 和基于 HTTP 的 SSE 支持的核心 starter -
spring-ai-starter-mcp-client
- Core starter providing STDIO and HTTP-based SSE support -
spring-ai-starter-mcp-client-webflux
- 基于 WebFlux 的 SSE 传输实现 -
spring-ai-starter-mcp-client-webflux
- WebFlux-based SSE transport implementation
Server Starters
-
spring-ai-starter-mcp-server
- 具有 STDIO 传输支持的核心服务器 -
spring-ai-starter-mcp-server
- Core server with STDIO transport support -
spring-ai-starter-mcp-server-webmvc
- 基于 Spring MVC 的 SSE 传输实现 -
spring-ai-starter-mcp-server-webmvc
- Spring MVC-based SSE transport implementation -
spring-ai-starter-mcp-server-webflux
- 基于 WebFlux 的 SSE 传输实现 -
spring-ai-starter-mcp-server-webflux
- WebFlux-based SSE transport implementation