MCP Java SDK
repository·main·Indexed 25 days ago
https://github.com/modelcontextprotocol/java-sdkA Java implementation of the Model Context Protocol (MCP) that provides a standardized interface for Java applications to interact with AI models and tools. It supports synchronous and asynchronous communication, requires Java 17 or higher, and includes Spring Boot integration via Spring AI MCP for client/server starters, annotations, and OAuth 2.0 security.
What's inside modelcontextprotocol-java-sdk
- The Java MCP SDK is a Java implementation of the Model Context Protocol (MCP). It allows developers to integrate language models and AI tools with their Java applications using the standardized MCP interface.
Overview of MCP Java SDK
mainThe MCP Java SDK provides integration for the Model Context Protocol (MCP), enabling Java applications to interact with AI models and tools through a standardized interface. It supports both synchronous and asynchronous communication patterns. The SDK requires Java 17 or higher.Overview of MCP Java SDK features
mainThe MCP Java SDK provides implementations for both MCP Clients and MCP Servers, enabling standardized integration between AI models and tools.
Key capabilities include:
- Protocol Support: Version compatibility negotiation, Tools (discovery, execution, schema validation), Resources (URI templates), Roots management, Prompts handling, Sampling, Elicitation (user input requests), Completions (argument autocompletion), Progress notifications, and Structured Logging.
- Programming Paradigms: Supports both Synchronous and Asynchronous programming.
- Extensibility: Pluggable JSON serialization (Jackson 2.x/3.x) and pluggable authorization hooks for server security.
- Security: Includes DNS rebinding protection via Host/Origin header validation.
Understand the MCP Java SDK Architecture
mainThe SDK uses a layered architecture to separate protocol logic from communication methods:
- Client/Server Layer (
McpClient/McpServer): Handles high-level protocol operations usingMcpSessionfor both synchronous and asynchronous operations. - Session Layer (
McpSession): Manages the state and communication patterns between the client and server. - Transport Layer (
McpTransport): Responsible for JSON-RPC message serialization and deserialization. Supported transports include:StdioTransport(viamcp-core)- HTTP SSE transports (via dedicated transport modules like Java HttpClient or Servlet)
- Streamable HTTP transports for bidirectional communication
- Spring WebFlux and Spring WebMVC transports (provided via Spring AI 2.0+)
- Client/Server Layer (
Understand the MCP Java SDK Architecture
mainThe MCP Java SDK is a reference implementation of the Model Context Protocol (MCP) specification. It is designed to be pragmatic, interoperable, and pluggable.
Key architectural pillars include:
- JSON Serialization: Uses an abstraction layer (
io.modelcontextprotocol.jsoninmcp-core) with Jackson as the default implementation (mcp-json-jackson3). - Programming Model: Built on Reactive Streams (using Project Reactor internally) to support asynchronous, non-blocking, and streaming interactions. It also provides a synchronous facade for simple blocking use cases.
- Observability: Uses SLF4J for logging and Reactor Context for propagating observability metadata (like correlation IDs) across asynchronous boundaries.
- Transports:
- Clients: Transport-agnostic APIs. The core module provides a default implementation using JDK HttpClient (Java 11+).
- Servers: Transport-agnostic APIs. The core module provides a default implementation using Jakarta Servlet.
- Authorization: The SDK provides pluggable authorization hooks for MCP servers but does not include a built-in implementation, allowing integration with Spring Security, MicroProfile JWT, etc.
- JSON Serialization: Uses an abstraction layer (
Spring HTTP Client Architecture and Scenario Routing
mainThe client is a Spring Boot application that uses conditional configuration to route scenarios:
- DefaultConfiguration: Activated for all scenarios except
auth/pre-registration. It uses the OAuth2 Authorization Code flow with dynamic client registration viaMcpClientOAuth2Configurer. - PreRegistrationConfiguration: Activated only for
auth/pre-registration. It uses the Client Credentials flow with pre-registered credentials provided via theMCP_CONFORMANCE_CONTEXTenvironment variable.
Key Dependencies:
- Spring Boot 4.0 with Spring Security OAuth2 Client
- Spring AI MCP Client (
spring-ai-starter-mcp-client) mcp-client-security(community library for MCP-specific OAuth2 integration)
- DefaultConfiguration: Activated for all scenarios except
Build the documentation static site
mainTo generate the static HTML files for deployment, run the build command. The output will be located in the
site/directory.mkdocs buildUse the mcp convenience bundle for minimal setup
mainFor a minimal implementation that includes both the core logic and Jackson 3.x JSON serialization, depend on themcpartifact. This is the recommended starting point for non-Spring applications.Build the JDK HTTP Client conformance test JAR
mainTo build the executable JAR for the JDK HTTP Client conformance test client, use the Maven wrapper from the root of the repository. This creates an executable JAR in the
target/directory of the module.cd conformance-tests/client-jdk-http-client ../../mvnw clean package -DskipTestsRun MCP Java SDK Server Conformance Tests
mainTo validate an MCP Java Server implementation, you can run the active conformance suite against a running server instance. This requires starting the server via Maven and then using the
@modelcontextprotocol/conformanceCLI tool to execute tests against the server's URL.Note: The server must be running on a reachable URL (e.g.,
http://localhost:8080/mcp) before starting the test command.# Start server ./mvnw compile -pl conformance-tests/server-servlet -am exec:java # Run tests (in another terminal) npx @modelcontextprotocol/conformance server --url http://localhost:8080/mcp --suite activeRun MCP Java SDK Client Conformance Tests
mainTo validate an MCP Java Client implementation, build the client JAR using Maven and then use the
@modelcontextprotocol/conformanceCLI to run specific scenarios against the client's command execution.Note: You must iterate through the desired scenarios (e.g.,
initialize,tools_call,elicitation-sep1034-client-defaults,sse-retry) using theclientcommand.# Build cd conformance-tests/client-jdk-http-client ../../mvnw clean package -DskipTests # Run all scenarios for scenario in initialize tools_call elicitation-sep1034-client-defaults sse-retry; do npx @modelcontextprotocol/conformance client \ --command "java -jar target/client-jdk-http-client-1.1.0-SNAPSHOT.jar" \ --scenario $scenario doneMigrate Jackson dependencies for MCP Java SDK 1.0.0
mainIn version 1.0.0, the
mcpaggregator module defaults to Jackson 3.If your project requires Jackson 2 (
com.fasterxml.jackson2.x), you must stop using themcpaggregator and instead depend on the individual modulesmcp-coreandmcp-json-jackson2.If you are ready to use Jackson 3, you can continue using the
mcpaggregator.<!-- For Jackson 2 users --> <dependency> <groupId>io.modelcontextprotocol.sdk</groupId> <artifactId>mcp-core</artifactId> <version>1.0.0-RC3</version> </dependency> <dependency> <groupId>io.modelcontextprotocol.sdk</groupId> <artifactId>mcp-json-jackson2</artifactId> <version>1.0.0-RC3</version> </dependency> <!-- For Jackson 3 users --> <dependency> <groupId>io.modelcontextprotocol.sdk</groupId> <artifactId>mcp</artifactId> <version>1.0.0-RC3</version> </dependency>