MCP Server
Learn how to implement and configure a Model Context Protocol (MCP) server
Breaking Changes in 0.8.0 ⚠️
Note: Version 0.8.0 introduces several breaking changes including a new session-based architecture. If you’re upgrading from 0.7.0, please refer to the Migration Guide for detailed instructions.
Overview
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
- 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 and notifications
The Spring-AI MCP Server integration extends the MCP Java SDK to provide auto-configuration for MCP servdr functionality in Spring Boot applications.
The server supports both synchronous and asynchronous APIs, allowing for flexible integration in different application contexts.
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:
Logging Support
The server provides structured logging capabilities that allow sending log messages to clients with different severity levels:
Clients can control the minimum logging level they receive through the 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)
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:
The Tool specification includes a Tool definition with 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:
The resource specification compriese of resource definitions and resource read handler.
The resource definition including 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.
The prompt definition includes name (identifier for the prompt), description (purpose of the prompt), and list of arguments (parameters for templating).
The handler function processes requests and returns formatted templates.
The first argument is McpAsyncServerExchange
for client interaction, and the second argument is a GetPromptRequest
instance.
Using Sampling from a Server
To use Sampling capabilities, connect to a client that supports sampling. No special server configuration is needed, but verify client sampling support before making requests. Learn about client sampling support.
Once connected to a compatible client, the server can request language model generations:
The 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.
Error Handling
The SDK provides comprehensive error handling through the McpError class, covering protocol compatibility, transport communication, JSON-RPC messaging, tool execution, resource management, prompt handling, timeouts, and connection issues. This unified error handling approach ensures consistent and reliable error management across both synchronous and asynchronous operations.
Was this page helpful?