SDK Contents
Java MCP Overview Java MCP Client [Java MCP Server]Server Features
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:- Exposing tools that clients can discover and execute. Supports input and output schemas and returns structured and unstructured content types.
- Managing resources with URI-based access patterns
- Providing prompt templates and handling prompt requests
- Supporting capability negotiation with clients
- Implementing server-side protocol operations
- Managing concurrent client connections
- Providing structured logging, progress tracking, and notifications
- Provides
STDIO
,Streamable-HTTP
andSSE
server transport implementations without requiring external web frameworks. - Optional, Spring-specific transport dependencies
io.modelcontextprotocol.sdk:mcp-spring-webflux
,io.modelcontextprotocol.sdk:mcp-spring-webmvc
for Spring AI users.
This quickstart demo, based on Spring AI MCP, will show
you how to build an MCP server.
Preserving thread-locals in handlers
McpSyncServer
delegates execution to an underlying McpAsyncServer
.
Execution of handlers for tools, resources, etc, may not happen on the thread calling the method.
In that case, thread-locals are lost, which may break certain features in frameworks relying on thread-bound work.
To ensure execution happens on the calling thread, and that thread-locals remain available, set McpServer.sync(...).immediateExecution(true)
.This is only relevant to Sync servers. Async servers use the reactive stack and should not rely on thread-locals.
Server Transport Providers
The transport layer in the MCP SDK is responsible for handling the communication between clients and servers. It provides different implementations to support various communication protocols and patterns. The SDK includes several built-in transport provider implementations:Create in-process based transport:Provides bidirectional JSON-RPC message handling over standard input/output streams with non-blocking message processing, serialization/deserialization, and graceful shutdown support.Key features:
- Bidirectional communication through stdin/stdout
- Process-based integration support
- Simple setup and configuration
- Lightweight implementation
Server Capabilities
The server can be configured with various capabilities:Tool Specification
The Model Context Protocol allows servers to expose tools that can be invoked by language models. The Java SDK allows implementing a Tool Specifications with their handler functions. Tools enable AI models to perform calculations, access external APIs, query databases, and manipulate files:name
, description
, and parameter schema
followed by a call handler that implements the tool’s logic.
The function’s first argument is McpAsyncServerExchange
for client interaction, and the second is a map of tool arguments.
Resource Specification
Specification of a resource with its handler function. Resources provide context to AI models by exposing data such as: File contents, Database records, API responses, System information, Application state. Example resource specification:name
, description
, and MIME type
.
The first argument of the function that handles resource read requests is an McpAsyncServerExchange
upon which the server can
interact with the connected client.
The second arguments is a McpSchema.ReadResourceRequest
.
Prompt Specification
As part of the Prompting capabilities, MCP provides a standardized way for servers to expose prompt templates to clients. The Prompt Specification is a structured template for AI model interactions that enables consistent message formatting, parameter substitution, context injection, response formatting, and instruction templating.McpAsyncServerExchange
for client interaction, and the second argument is a GetPromptRequest
instance.
Completion Specification
As part of the Completion capabilities, MCP provides a standardized way for servers to offer argument autocompletion suggestions for prompts and resource URIs.McpSchema.CompletionReference
definition defines the type (PromptReference
or ResourceReference
) and the identifier for the completion specification (e.g handler).
The handler function processes requests and returns the completion response.
The first argument is McpAsyncServerExchange
for client interaction, and the second argument is a CompleteRequest
instance.
Check the using completion to learn how to use the completion capabilities on the client side.
Using Sampling from a Server
To use Sampling capabilities, you need a compatible client that supports sampling. No special server configuration is needed, but verify client sampling support before making requests. Learn about client sampling support. When a compatible client connects to a stateful server, the server can request language model generations:CreateMessageRequest
object allows you to specify: Content
- the input text or image for the model,
Model Preferences
- hints and priorities for model selection, System Prompt
- instructions for the model’s behavior and
Max Tokens
- maximum length of the generated response.
Using Elicitation from a Server
To use Elicitation capabilities, you need a compatible client that supports elicitation. No special server configuration is needed, but verify client elicitation support before making requests. Learn about client elicitation support. When a compatible client connects to a stateful server, the server can request language model generations:Logging Support
The server provides structured logging capabilities that allow sending log messages to clients with different severity levels. The log notifications can only be sent from within an existing client session, such as tools, resources, and prompts calls. For example, we can send a log message from within a tool handler function. On the client side, you can register a logging consumer to receive log messages from the server and set the minimum logging level to filter messages.McpAsyncServerExchange
/McpSyncServerExchange
object in the tool/resource/prompt handler function:
mcpClient.setLoggingLevel(level)
request. Messages below the set level will be filtered out.
Supported logging levels (in order of increasing severity): DEBUG (0), INFO (1), NOTICE (2), WARNING (3), ERROR (4), CRITICAL (5), ALERT (6), EMERGENCY (7)