Improve docs
This commit is contained in:
parent
48e4cf4b13
commit
df35c78aeb
7 changed files with 425 additions and 361 deletions
|
|
@ -1,66 +0,0 @@
|
|||
---
|
||||
title: Dependency Management
|
||||
description: Guide to managing dependencies for the Model Context Protocol (MCP) Java SDK
|
||||
---
|
||||
|
||||
# Dependency Management
|
||||
|
||||
## Bill of Materials (BOM)
|
||||
|
||||
The Bill of Materials (BOM) declares the recommended versions of all the dependencies used by a given release.
|
||||
Using the BOM from your application's build script avoids the need for you to specify and maintain the dependency versions yourself.
|
||||
Instead, the version of the BOM you're using determines the utilized dependency versions.
|
||||
It also ensures that you're using supported and tested versions of the dependencies by default, unless you choose to override them.
|
||||
|
||||
Add the BOM to your project:
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Maven">
|
||||
```xml
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.modelcontextprotocol.sdk</groupId>
|
||||
<artifactId>mcp-bom</artifactId>
|
||||
<version>0.6.0-SNAPSHOT</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
```
|
||||
</Tab>
|
||||
|
||||
<Tab title="Gradle">
|
||||
```groovy
|
||||
dependencies {
|
||||
implementation platform("org.modelcontextprotocol.sdk:mcp-bom:0.6.0-SNAPSHOT")
|
||||
//...
|
||||
}
|
||||
```
|
||||
|
||||
Gradle users can also use the Spring AI MCP BOM by leveraging Gradle (5.0+) native support for declaring dependency constraints using a Maven BOM.
|
||||
This is implemented by adding a 'platform' dependency handler method to the dependencies section of your Gradle build script.
|
||||
As shown in the snippet below this can then be followed by version-less declarations of the Starter Dependencies for the one or more spring-ai modules you wish to use, e.g. spring-ai-openai.
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
Replace the version number with the version of the BOM you want to use.
|
||||
|
||||
## Available Dependencies
|
||||
|
||||
The following dependencies are available and managed by the BOM:
|
||||
|
||||
### Core Dependencies
|
||||
|
||||
- `org.modelcontextprotocol.sdk:mcp` - Core MCP library providing the base functionality and APIs for Model Context Protocol implementation.
|
||||
|
||||
### Transport Dependencies
|
||||
|
||||
- `org.modelcontextprotocol.sdk:mcp-spring-webflux` - WebFlux-based Server-Sent Events (SSE) transport implementation for reactive applications.
|
||||
- `org.modelcontextprotocol.sdk:mcp-spring-webmvc` - WebMVC-based Server-Sent Events (SSE) transport implementation for servlet-based applications.
|
||||
|
||||
### Testing Dependencies
|
||||
|
||||
- `org.modelcontextprotocol.sdk:mcp-test` - Testing utilities and support for MCP-based applications.
|
||||
|
||||
|
|
@ -1,139 +0,0 @@
|
|||
---
|
||||
title: MCP Server Capabilities
|
||||
description: Configure and manage Model Context Protocol (MCP) server capabilities including tools, resources, and prompts
|
||||
---
|
||||
|
||||
# Model Context Protocol Server Capabilities
|
||||
|
||||
The server can be configured with various capabilities:
|
||||
|
||||
```java
|
||||
var capabilities = ServerCapabilities.builder()
|
||||
.resources(false, true) // Resource support with list changes notifications
|
||||
.tools(true) // Tool support with list changes notifications
|
||||
.prompts(true) // Prompt support with list changes notifications
|
||||
.logging() // Enable logging support (enabled by default with loging level INFO)
|
||||
.build();
|
||||
```
|
||||
|
||||
## Logging Support
|
||||
|
||||
The server provides structured logging capabilities that allow sending log messages to clients with different severity levels:
|
||||
|
||||
```java
|
||||
// Send a log message to clients
|
||||
server.loggingNotification(LoggingMessageNotification.builder()
|
||||
.level(LoggingLevel.INFO)
|
||||
.logger("custom-logger")
|
||||
.data("Custom log message")
|
||||
.build());
|
||||
```
|
||||
|
||||
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 Registration
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Sync">
|
||||
```java
|
||||
// Sync tool registration
|
||||
var syncToolRegistration = new McpServerFeatures.SyncToolRegistration(
|
||||
new Tool("calculator", "Basic calculator", Map.of(
|
||||
"operation", "string",
|
||||
"a", "number",
|
||||
"b", "number"
|
||||
)),
|
||||
arguments -> {
|
||||
// Tool implementation
|
||||
return new CallToolResult(result, false);
|
||||
}
|
||||
);
|
||||
```
|
||||
</Tab>
|
||||
|
||||
<Tab title="Async">
|
||||
```java
|
||||
// Async tool registration
|
||||
var asyncToolRegistration = new McpServerFeatures.AsyncToolRegistration(
|
||||
new Tool("calculator", "Basic calculator", Map.of(
|
||||
"operation", "string",
|
||||
"a", "number",
|
||||
"b", "number"
|
||||
)),
|
||||
arguments -> {
|
||||
// Tool implementation
|
||||
return Mono.just(new CallToolResult(result, false));
|
||||
}
|
||||
);
|
||||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## Resource Registration
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Sync">
|
||||
```java
|
||||
// Sync resource registration
|
||||
var syncResourceRegistration = new McpServerFeatures.SyncResourceRegistration(
|
||||
new Resource("custom://resource", "name", "description", "mime-type", null),
|
||||
request -> {
|
||||
// Resource read implementation
|
||||
return new ReadResourceResult(contents);
|
||||
}
|
||||
);
|
||||
```
|
||||
</Tab>
|
||||
|
||||
<Tab title="Async">
|
||||
```java
|
||||
// Async resource registration
|
||||
var asyncResourceRegistration = new McpServerFeatures.AsyncResourceRegistration(
|
||||
new Resource("custom://resource", "name", "description", "mime-type", null),
|
||||
request -> {
|
||||
// Resource read implementation
|
||||
return Mono.just(new ReadResourceResult(contents));
|
||||
}
|
||||
);
|
||||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## Prompt Registration
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Sync">
|
||||
```java
|
||||
// Sync prompt registration
|
||||
var syncPromptRegistration = new McpServerFeatures.SyncPromptRegistration(
|
||||
new Prompt("greeting", "description", List.of(
|
||||
new PromptArgument("name", "description", true)
|
||||
)),
|
||||
request -> {
|
||||
// Prompt implementation
|
||||
return new GetPromptResult(description, messages);
|
||||
}
|
||||
);
|
||||
```
|
||||
</Tab>
|
||||
|
||||
<Tab title="Async">
|
||||
```java
|
||||
// Async prompt registration
|
||||
var asyncPromptRegistration = new McpServerFeatures.AsyncPromptRegistration(
|
||||
new Prompt("greeting", "description", List.of(
|
||||
new PromptArgument("name", "description", true)
|
||||
)),
|
||||
request -> {
|
||||
// Prompt implementation
|
||||
return Mono.just(new GetPromptResult(description, messages));
|
||||
}
|
||||
);
|
||||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
# 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.
|
||||
|
|
@ -120,7 +120,97 @@ client.initialize()
|
|||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## Client Features
|
||||
## Client Transport
|
||||
|
||||
The transport layer handles the communication between MCP clients and servers, providing different implementations for various use cases. The client transport manages message serialization, connection establishment, and protocol-specific communication patterns.
|
||||
|
||||
<Tabs>
|
||||
<Tab title="STDIO">
|
||||
Creates transport for in-process based communication
|
||||
```java
|
||||
ServerParameters params = ServerParameters.builder("npx")
|
||||
.args("-y", "@modelcontextprotocol/server-everything", "dir")
|
||||
.build();
|
||||
McpTransport transport = new StdioClientTransport(params);
|
||||
```
|
||||
</Tab>
|
||||
<Tab title="SSE (HttpClient)">
|
||||
Creates a framework agnostic (pure Java API) SSE client transport. Included in the core mcp module.
|
||||
```java
|
||||
McpTransport transport = new HttpClientSseClientTransport("http://your-mcp-server");
|
||||
```
|
||||
</Tab>
|
||||
<Tab title="SSE (WebFlux)">
|
||||
Creates WebFlux-based SSE client transport. Requires the mcp-webflux-sse-transport dependency.
|
||||
```java
|
||||
WebClient.Builder webClientBuilder = WebClient.builder()
|
||||
.baseUrl("http://your-mcp-server");
|
||||
McpTransport transport = new WebFluxSseClientTransport(webClientBuilder);
|
||||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## Client Capabilities
|
||||
|
||||
The client can be configured with various capabilities:
|
||||
|
||||
```java
|
||||
var capabilities = ClientCapabilities.builder()
|
||||
.roots(true) // Enable filesystem roots support with list changes notifications
|
||||
.sampling() // Enable LLM sampling support
|
||||
.build();
|
||||
```
|
||||
|
||||
### Roots Support
|
||||
|
||||
Roots define the boundaries of where servers can operate within the filesystem:
|
||||
|
||||
```java
|
||||
// Add a root dynamically
|
||||
client.addRoot(new Root("file:///path", "description"));
|
||||
|
||||
// Remove a root
|
||||
client.removeRoot("file:///path");
|
||||
|
||||
// Notify server of roots changes
|
||||
client.rootsListChangedNotification();
|
||||
```
|
||||
|
||||
The roots capability allows servers to:
|
||||
|
||||
- Request the list of accessible filesystem roots
|
||||
- Receive notifications when the root list changes
|
||||
- Understand which directories and files they have access to
|
||||
|
||||
|
||||
### Sampling Support
|
||||
|
||||
Sampling enables servers to request LLM interactions ("completions" or "generations") through the client:
|
||||
|
||||
```java
|
||||
// Configure sampling handler
|
||||
Function<CreateMessageRequest, CreateMessageResult> samplingHandler = request -> {
|
||||
// Sampling implementation that interfaces with LLM
|
||||
return new CreateMessageResult(response);
|
||||
};
|
||||
|
||||
// Create client with sampling support
|
||||
var client = McpClient.sync(transport)
|
||||
.capabilities(ClientCapabilities.builder()
|
||||
.sampling()
|
||||
.build())
|
||||
.sampling(samplingHandler)
|
||||
.build();
|
||||
```
|
||||
|
||||
This capability allows:
|
||||
- Servers to leverage AI capabilities without requiring API keys
|
||||
- Clients to maintain control over model access and permissions
|
||||
- Support for both text and image-based interactions
|
||||
- Optional inclusion of MCP server context in prompts
|
||||
|
||||
|
||||
## Using MCP Clients
|
||||
|
||||
### Tool Execution
|
||||
|
||||
|
|
@ -134,7 +224,7 @@ var tools = client.listTools();
|
|||
tools.forEach(tool -> System.out.println(tool.getName()));
|
||||
|
||||
// Execute a tool
|
||||
var result = client.executeTool("calculator", Map.of(
|
||||
var result = client.callTool("calculator", Map.of(
|
||||
"operation", "add",
|
||||
"a", 1,
|
||||
"b", 2
|
||||
|
|
@ -151,7 +241,7 @@ client.listTools()
|
|||
.subscribe();
|
||||
|
||||
// Execute a tool
|
||||
client.executeTool("calculator", Map.of(
|
||||
client.callTool("calculator", Map.of(
|
||||
"operation", "add",
|
||||
"a", 1,
|
||||
"b", 2
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ title: MCP Java SDK
|
|||
description: Java SDK implementation of the [Model Context Protocol](https://modelcontextprotocol.io/introduction), enabling seamless integration with language models and AI tools.
|
||||
---
|
||||
|
||||
# Features
|
||||
## Features
|
||||
|
||||
- Synchronous and Asynchronous MCP Client and MCP Server implementations
|
||||
- Standard MCP operations support:
|
||||
|
|
@ -22,7 +22,7 @@ description: Java SDK implementation of the [Model Context Protocol](https://mod
|
|||
- WebFlux SSE transport for reactive HTTP streaming
|
||||
- WebMVC SSE transport for servlet-based HTTP streaming
|
||||
|
||||
# Architecture
|
||||
## Architecture
|
||||
|
||||

|
||||
|
||||
|
|
@ -44,7 +44,7 @@ Key Interactions:
|
|||
- **Message Flow**: JSON-RPC message handling with validation, type-safe response processing, and error handling.
|
||||
- **Resource Management**: Resource discovery, URI template-based access, subscription system, and content retrieval.
|
||||
|
||||
# Dependencies
|
||||
## Dependencies
|
||||
|
||||
Add the following dependency to your Maven project:
|
||||
|
||||
|
|
@ -76,3 +76,58 @@ For HTTP SSE transport implementations, add one of the following dependencies:
|
|||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
### Bill of Materials (BOM)
|
||||
|
||||
The Bill of Materials (BOM) declares the recommended versions of all the dependencies used by a given release.
|
||||
Using the BOM from your application's build script avoids the need for you to specify and maintain the dependency versions yourself.
|
||||
Instead, the version of the BOM you're using determines the utilized dependency versions.
|
||||
It also ensures that you're using supported and tested versions of the dependencies by default, unless you choose to override them.
|
||||
|
||||
Add the BOM to your project:
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Maven">
|
||||
```xml
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.modelcontextprotocol.sdk</groupId>
|
||||
<artifactId>mcp-bom</artifactId>
|
||||
<version>0.6.0-SNAPSHOT</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
```
|
||||
</Tab>
|
||||
|
||||
<Tab title="Gradle">
|
||||
```groovy
|
||||
dependencies {
|
||||
implementation platform("org.modelcontextprotocol.sdk:mcp-bom:0.6.0-SNAPSHOT")
|
||||
//...
|
||||
}
|
||||
```
|
||||
|
||||
Gradle users can also use the Spring AI MCP BOM by leveraging Gradle (5.0+) native support for declaring dependency constraints using a Maven BOM.
|
||||
This is implemented by adding a 'platform' dependency handler method to the dependencies section of your Gradle build script.
|
||||
As shown in the snippet below this can then be followed by version-less declarations of the Starter Dependencies for the one or more spring-ai modules you wish to use, e.g. spring-ai-openai.
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
Replace the version number with the version of the BOM you want to use.
|
||||
|
||||
### Available Dependencies
|
||||
|
||||
The following dependencies are available and managed by the BOM:
|
||||
|
||||
- Core Dependencies
|
||||
- `org.modelcontextprotocol.sdk:mcp` - Core MCP library providing the base functionality and APIs for Model Context Protocol implementation.
|
||||
- Transport Dependencies
|
||||
- `org.modelcontextprotocol.sdk:mcp-spring-webflux` - WebFlux-based Server-Sent Events (SSE) transport implementation for reactive applications.
|
||||
- `org.modelcontextprotocol.sdk:mcp-spring-webmvc` - WebMVC-based Server-Sent Events (SSE) transport implementation for servlet-based applications.
|
||||
- Testing Dependencies
|
||||
- `org.modelcontextprotocol.sdk:mcp-test` - Testing utilities and support for MCP-based applications.
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ title: MCP Server
|
|||
description: Learn how to implement and configure a Model Context Protocol (MCP) server
|
||||
---
|
||||
|
||||
# Model Context Protocol Server
|
||||
## 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:
|
||||
|
||||
|
|
@ -96,3 +96,274 @@ asyncServer.close()
|
|||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
|
||||
## Server Transport
|
||||
|
||||
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 implementations:
|
||||
|
||||
<Tabs>
|
||||
|
||||
<Tab title="STDIO">
|
||||
<>
|
||||
Create in-process based transport:
|
||||
|
||||
```java
|
||||
StdioServerTransport transport = new StdioServerTransport(new ObjectMapper());
|
||||
```
|
||||
|
||||
Provides bidirectional JSON-RPC message handling over standard input/output streams with non-blocking message processing, serialization/deserialization, and graceful shutdown support.
|
||||
|
||||
Key features:
|
||||
<ul>
|
||||
<li>Bidirectional communication through stdin/stdout</li>
|
||||
<li>Process-based integration support</li>
|
||||
<li>Simple setup and configuration</li>
|
||||
<li>Lightweight implementation</li>
|
||||
</ul>
|
||||
</>
|
||||
</Tab>
|
||||
|
||||
<Tab title="SSE (WebFlux)">
|
||||
<>
|
||||
<p>Creates WebFlux-based SSE server transport.<br />Requires the <code>mcp-spring-webflux</code> dependency.</p>
|
||||
|
||||
```java
|
||||
@Configuration
|
||||
class McpConfig {
|
||||
@Bean
|
||||
WebFluxSseServerTransport webFluxSseServerTransport(ObjectMapper mapper) {
|
||||
return new WebFluxSseServerTransport(mapper, "/mcp/message");
|
||||
}
|
||||
|
||||
@Bean
|
||||
RouterFunction<?> mcpRouterFunction(WebFluxSseServerTransport transport) {
|
||||
return transport.getRouterFunction();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p>Implements the MCP HTTP with SSE transport specification, providing:</p>
|
||||
<ul>
|
||||
<li>Reactive HTTP streaming with WebFlux</li>
|
||||
<li>Concurrent client connections through SSE endpoints</li>
|
||||
<li>Message routing and session management</li>
|
||||
<li>Graceful shutdown capabilities</li>
|
||||
</ul>
|
||||
</>
|
||||
</Tab>
|
||||
|
||||
|
||||
<Tab title="SSE (WebMvc)">
|
||||
<>
|
||||
<p>Creates WebMvc-based SSE server transport.<br />Requires the <code>mcp-spring-webmvc</code> dependency.</p>
|
||||
|
||||
```java
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
class McpConfig {
|
||||
@Bean
|
||||
WebMvcSseServerTransport webMvcSseServerTransport(ObjectMapper mapper) {
|
||||
return new WebMvcSseServerTransport(mapper, "/mcp/message");
|
||||
}
|
||||
|
||||
@Bean
|
||||
RouterFunction<ServerResponse> mcpRouterFunction(WebMvcSseServerTransport transport) {
|
||||
return transport.getRouterFunction();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p>Implements the MCP HTTP with SSE transport specification, providing:</p>
|
||||
<ul>
|
||||
<li>Server-side event streaming</li>
|
||||
<li>Integration with Spring WebMVC</li>
|
||||
<li>Support for traditional web applications</li>
|
||||
<li>Synchronous operation handling</li>
|
||||
</ul>
|
||||
</>
|
||||
</Tab>
|
||||
|
||||
|
||||
<Tab title="SSE (Servlet)">
|
||||
<>
|
||||
<p>
|
||||
Creates a Servlet-based SSE server transport.<br />
|
||||
Included in the core <code>mcp</code> module.<br />
|
||||
The <code>HttpServletSseServerTransport</code> can be used with any Servlet container.
|
||||
To use it with a Spring Web application, you can register it as a Servlet bean:
|
||||
</p>
|
||||
|
||||
```java
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
public class McpServerConfig implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public HttpServletSseServerTransport servletSseServerTransport() {
|
||||
return new HttpServletSseServerTransport(new ObjectMapper(), "/mcp/message");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServletRegistrationBean customServletBean(HttpServletSseServerTransport servlet) {
|
||||
return new ServletRegistrationBean(servlet);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p>
|
||||
Implements the MCP HTTP with SSE transport specification using the traditional Servlet API, providing:
|
||||
</p>
|
||||
<ul>
|
||||
<li>Asynchronous message handling using Servlet 6.0 async support</li>
|
||||
<li>Session management for multiple client connections</li>
|
||||
<li>
|
||||
Two types of endpoints:
|
||||
<ul>
|
||||
<li>SSE endpoint (<code>/sse</code>) for server-to-client events</li>
|
||||
<li>Message endpoint (configurable) for client-to-server requests</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>Error handling and response formatting</li>
|
||||
<li>Graceful shutdown support</li>
|
||||
</ul>
|
||||
</>
|
||||
</Tab>
|
||||
|
||||
</Tabs>
|
||||
|
||||
|
||||
## Server Capabilities
|
||||
|
||||
The server can be configured with various capabilities:
|
||||
|
||||
```java
|
||||
var capabilities = ServerCapabilities.builder()
|
||||
.resources(false, true) // Resource support with list changes notifications
|
||||
.tools(true) // Tool support with list changes notifications
|
||||
.prompts(true) // Prompt support with list changes notifications
|
||||
.logging() // Enable logging support (enabled by default with loging level INFO)
|
||||
.build();
|
||||
```
|
||||
|
||||
### Logging Support
|
||||
|
||||
The server provides structured logging capabilities that allow sending log messages to clients with different severity levels:
|
||||
|
||||
```java
|
||||
// Send a log message to clients
|
||||
server.loggingNotification(LoggingMessageNotification.builder()
|
||||
.level(LoggingLevel.INFO)
|
||||
.logger("custom-logger")
|
||||
.data("Custom log message")
|
||||
.build());
|
||||
```
|
||||
|
||||
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 Registration
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Sync">
|
||||
```java
|
||||
// Sync tool registration
|
||||
var syncToolRegistration = new McpServerFeatures.SyncToolRegistration(
|
||||
new Tool("calculator", "Basic calculator", Map.of(
|
||||
"operation", "string",
|
||||
"a", "number",
|
||||
"b", "number"
|
||||
)),
|
||||
arguments -> {
|
||||
// Tool implementation
|
||||
return new CallToolResult(result, false);
|
||||
}
|
||||
);
|
||||
```
|
||||
</Tab>
|
||||
|
||||
<Tab title="Async">
|
||||
```java
|
||||
// Async tool registration
|
||||
var asyncToolRegistration = new McpServerFeatures.AsyncToolRegistration(
|
||||
new Tool("calculator", "Basic calculator", Map.of(
|
||||
"operation", "string",
|
||||
"a", "number",
|
||||
"b", "number"
|
||||
)),
|
||||
arguments -> {
|
||||
// Tool implementation
|
||||
return Mono.just(new CallToolResult(result, false));
|
||||
}
|
||||
);
|
||||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
### Resource Registration
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Sync">
|
||||
```java
|
||||
// Sync resource registration
|
||||
var syncResourceRegistration = new McpServerFeatures.SyncResourceRegistration(
|
||||
new Resource("custom://resource", "name", "description", "mime-type", null),
|
||||
request -> {
|
||||
// Resource read implementation
|
||||
return new ReadResourceResult(contents);
|
||||
}
|
||||
);
|
||||
```
|
||||
</Tab>
|
||||
|
||||
<Tab title="Async">
|
||||
```java
|
||||
// Async resource registration
|
||||
var asyncResourceRegistration = new McpServerFeatures.AsyncResourceRegistration(
|
||||
new Resource("custom://resource", "name", "description", "mime-type", null),
|
||||
request -> {
|
||||
// Resource read implementation
|
||||
return Mono.just(new ReadResourceResult(contents));
|
||||
}
|
||||
);
|
||||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
### Prompt Registration
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Sync">
|
||||
```java
|
||||
// Sync prompt registration
|
||||
var syncPromptRegistration = new McpServerFeatures.SyncPromptRegistration(
|
||||
new Prompt("greeting", "description", List.of(
|
||||
new PromptArgument("name", "description", true)
|
||||
)),
|
||||
request -> {
|
||||
// Prompt implementation
|
||||
return new GetPromptResult(description, messages);
|
||||
}
|
||||
);
|
||||
```
|
||||
</Tab>
|
||||
|
||||
<Tab title="Async">
|
||||
```java
|
||||
// Async prompt registration
|
||||
var asyncPromptRegistration = new McpServerFeatures.AsyncPromptRegistration(
|
||||
new Prompt("greeting", "description", List.of(
|
||||
new PromptArgument("name", "description", true)
|
||||
)),
|
||||
request -> {
|
||||
// Prompt implementation
|
||||
return Mono.just(new GetPromptResult(description, messages));
|
||||
}
|
||||
);
|
||||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
## 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.
|
||||
|
|
|
|||
|
|
@ -1,144 +0,0 @@
|
|||
---
|
||||
title: MCP Transport
|
||||
description: Understanding the Model Context Protocol (MCP) transport layer and available implementations
|
||||
---
|
||||
|
||||
# Model Context Protocol Transport Layer
|
||||
|
||||
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 implementations:
|
||||
|
||||
<Tabs>
|
||||
|
||||
<Tab title="STDIO">
|
||||
<>
|
||||
Create in-process based transport:
|
||||
|
||||
```java
|
||||
StdioServerTransport transport = new StdioServerTransport(new ObjectMapper());
|
||||
```
|
||||
|
||||
Provides bidirectional JSON-RPC message handling over standard input/output streams with non-blocking message processing, serialization/deserialization, and graceful shutdown support.
|
||||
|
||||
Key features:
|
||||
<ul>
|
||||
<li>Bidirectional communication through stdin/stdout</li>
|
||||
<li>Process-based integration support</li>
|
||||
<li>Simple setup and configuration</li>
|
||||
<li>Lightweight implementation</li>
|
||||
</ul>
|
||||
</>
|
||||
</Tab>
|
||||
|
||||
<Tab title="SSE (WebFlux)">
|
||||
<>
|
||||
<p>Creates WebFlux-based SSE server transport.<br />Requires the <code>mcp-spring-webflux</code> dependency.</p>
|
||||
|
||||
<pre>
|
||||
<code className="language-java">
|
||||
{`@Configuration
|
||||
class McpConfig {
|
||||
@Bean
|
||||
WebFluxSseServerTransport webFluxSseServerTransport(ObjectMapper mapper) {
|
||||
return new WebFluxSseServerTransport(mapper, "/mcp/message");
|
||||
}
|
||||
|
||||
@Bean
|
||||
RouterFunction<?> mcpRouterFunction(WebFluxSseServerTransport transport) {
|
||||
return transport.getRouterFunction();
|
||||
}
|
||||
}`}
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
<p>Implements the MCP HTTP with SSE transport specification, providing:</p>
|
||||
<ul>
|
||||
<li>Reactive HTTP streaming with WebFlux</li>
|
||||
<li>Concurrent client connections through SSE endpoints</li>
|
||||
<li>Message routing and session management</li>
|
||||
<li>Graceful shutdown capabilities</li>
|
||||
</ul>
|
||||
</>
|
||||
</Tab>
|
||||
|
||||
|
||||
<Tab title="SSE (WebMvc)">
|
||||
<>
|
||||
<p>Creates WebMvc-based SSE server transport.<br />Requires the <code>mcp-spring-webmvc</code> dependency.</p>
|
||||
|
||||
<pre>
|
||||
<code className="language-java">
|
||||
{`@Configuration
|
||||
@EnableWebMvc
|
||||
class McpConfig {
|
||||
@Bean
|
||||
WebMvcSseServerTransport webMvcSseServerTransport(ObjectMapper mapper) {
|
||||
return new WebMvcSseServerTransport(mapper, "/mcp/message");
|
||||
}
|
||||
|
||||
@Bean
|
||||
RouterFunction<ServerResponse> mcpRouterFunction(WebMvcSseServerTransport transport) {
|
||||
return transport.getRouterFunction();
|
||||
}
|
||||
}`}
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
<p>Implements the MCP HTTP with SSE transport specification, providing:</p>
|
||||
<ul>
|
||||
<li>Server-side event streaming</li>
|
||||
<li>Integration with Spring WebMVC</li>
|
||||
<li>Support for traditional web applications</li>
|
||||
<li>Synchronous operation handling</li>
|
||||
</ul>
|
||||
</>
|
||||
</Tab>
|
||||
|
||||
|
||||
<Tab title="SSE (Servlet)">
|
||||
<>
|
||||
<p>
|
||||
Creates a Servlet-based SSE server transport.<br />
|
||||
Included in the core <code>mcp</code> module.<br />
|
||||
The <code>HttpServletSseServerTransport</code> can be used with any Servlet container.
|
||||
To use it with a Spring Web application, you can register it as a Servlet bean:
|
||||
</p>
|
||||
|
||||
<pre>
|
||||
<code className="language-java">
|
||||
{`@Configuration
|
||||
@EnableWebMvc
|
||||
public class McpServerConfig implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public HttpServletSseServerTransport servletSseServerTransport() {
|
||||
return new HttpServletSseServerTransport(new ObjectMapper(), "/mcp/message");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServletRegistrationBean customServletBean(HttpServletSseServerTransport servlet) {
|
||||
return new ServletRegistrationBean(servlet);
|
||||
}
|
||||
}`}
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
<p>
|
||||
Implements the MCP HTTP with SSE transport specification using the traditional Servlet API, providing:
|
||||
</p>
|
||||
<ul>
|
||||
<li>Asynchronous message handling using Servlet 6.0 async support</li>
|
||||
<li>Session management for multiple client connections</li>
|
||||
<li>
|
||||
Two types of endpoints:
|
||||
<ul>
|
||||
<li>SSE endpoint (<code>/sse</code>) for server-to-client events</li>
|
||||
<li>Message endpoint (configurable) for client-to-server requests</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>Error handling and response formatting</li>
|
||||
<li>Graceful shutdown support</li>
|
||||
</ul>
|
||||
</>
|
||||
</Tab>
|
||||
|
||||
</Tabs>
|
||||
|
|
@ -23,8 +23,7 @@
|
|||
{
|
||||
"group": "Getting Started",
|
||||
"pages": [
|
||||
"docs/overview",
|
||||
"docs/dependency-management"
|
||||
"docs/overview"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -32,9 +31,7 @@
|
|||
"pages": [
|
||||
"docs/mcp-overview",
|
||||
"docs/mcp-client",
|
||||
"docs/mcp-server",
|
||||
"docs/mcp-transport",
|
||||
"docs/mcp-capabilities"
|
||||
"docs/mcp-server"
|
||||
]
|
||||
}
|
||||
]
|
||||
|
|
|
|||
Loading…
Reference in a new issue