MCP Java SDK

repository·main·Indexed 25 days ago

https://github.com/modelcontextprotocol/java-sdk

A 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.

Tokens
25.2K
Snippets
66
Records
104
Agent score
86%

What's inside modelcontextprotocol-java-sdk

  1. Overview of MCP Java SDK

    main
    The 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.
  2. Overview of MCP Java SDK features

    main

    The 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.
  3. Understand the MCP Java SDK Architecture

    main

    The SDK uses a layered architecture to separate protocol logic from communication methods:

    • Client/Server Layer (McpClient/McpServer): Handles high-level protocol operations using McpSession for 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 (via mcp-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+)
  4. Understand the MCP Java SDK Architecture

    main

    The 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.json in mcp-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.
  5. Spring HTTP Client Architecture and Scenario Routing

    main

    The 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 via McpClientOAuth2Configurer.
    • PreRegistrationConfiguration: Activated only for auth/pre-registration. It uses the Client Credentials flow with pre-registered credentials provided via the MCP_CONFORMANCE_CONTEXT environment 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)
  6. Run MCP Java SDK Server Conformance Tests

    main

    To 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/conformance CLI 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 active
  7. Run MCP Java SDK Client Conformance Tests

    main

    To validate an MCP Java Client implementation, build the client JAR using Maven and then use the @modelcontextprotocol/conformance CLI 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 the client command.

    # 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
    done
  8. Migrate Jackson dependencies for MCP Java SDK 1.0.0

    main

    In version 1.0.0, the mcp aggregator module defaults to Jackson 3.

    If your project requires Jackson 2 (com.fasterxml.jackson 2.x), you must stop using the mcp aggregator and instead depend on the individual modules mcp-core and mcp-json-jackson2.

    If you are ready to use Jackson 3, you can continue using the mcp aggregator.

    <!-- 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>