|
此版本仍在开发中,尚未被视为稳定版。为了获取最新的快照版本,请使用Spring AI 1.1.3! |
MCP 服务器注解
MCP服务器注解提供了一种声明式的方法,使用Java注解来实现MCP服务器功能。 这些注解简化了工具、资源、提示及完成处理器的创建过程。
服务器注解
@工具箱Mcp
@McpTool 注解标记了一个方法作为带有自动JSON模式生成的MCP工具实现。
基本用法
@Component
public class CalculatorTools {
@McpTool(name = "add", description = "Add two numbers together")
public int add(
@McpToolParam(description = "First number", required = true) int a,
@McpToolParam(description = "Second number", required = true) int b) {
return a + b;
}
}
高级特性
@McpTool(name = "calculate-area",
description = "Calculate the area of a rectangle",
annotations = McpTool.McpAnnotations(
title = "Rectangle Area Calculator",
readOnlyHint = true,
destructiveHint = false,
idempotentHint = true
))
public AreaResult calculateRectangleArea(
@McpToolParam(description = "Width", required = true) double width,
@McpToolParam(description = "Height", required = true) double height) {
return new AreaResult(width * height, "square units");
}
带有请求上下文
工具可以访问请求上下文以进行高级操作:
@McpTool(name = "process-data", description = "Process data with request context")
public String processData(
McpSyncRequestContext context,
@McpToolParam(description = "Data to process", required = true) String data) {
// Send logging notification
context.info("Processing data: " + data);
// Send progress notification (using convenient method)
context.progress(p -> p.progress(0.5).total(1.0).message("Processing..."));
// Ping the client
context.ping();
return "Processed: " + data.toUpperCase();
}
动态模式支持
工具可以接受CallToolRequest来处理运行时架构:
@McpTool(name = "flexible-tool", description = "Process dynamic schema")
public CallToolResult processDynamic(CallToolRequest request) {
Map<String, Object> args = request.arguments();
// Process based on runtime schema
String result = "Processed " + args.size() + " arguments dynamically";
return CallToolResult.builder()
.addTextContent(result)
.build();
}
进度跟踪
工具可以接收进度Tokens,以便跟踪长时间运行的操作:
@McpTool(name = "long-task", description = "Long-running task with progress")
public String performLongTask(
McpSyncRequestContext context,
@McpToolParam(description = "Task name", required = true) String taskName) {
// Access progress token from context
String progressToken = context.request().progressToken();
if (progressToken != null) {
context.progress(p -> p.progress(0.0).total(1.0).message("Starting task"));
// Perform work...
context.progress(p -> p.progress(1.0).total(1.0).message("Task completed"));
}
return "Task " + taskName + " completed";
}
@Mcp资源
@McpResource注解通过URI模板提供对资源的访问。
基本用法
@Component
public class ResourceProvider {
@McpResource(
uri = "config://{key}",
name = "Configuration",
description = "Provides configuration data")
public String getConfig(String key) {
return configData.get(key);
}
}
使用ReadResourceResult
@McpResource(
uri = "user-profile://{username}",
name = "User Profile",
description = "Provides user profile information")
public ReadResourceResult getUserProfile(String username) {
String profileData = loadUserProfile(username);
return new ReadResourceResult(List.of(
new TextResourceContents(
"user-profile://" + username,
"application/json",
profileData)
));
}
带有请求上下文
@McpResource(
uri = "data://{id}",
name = "Data Resource",
description = "Resource with request context")
public ReadResourceResult getData(
McpSyncRequestContext context,
String id) {
// Send logging notification using convenient method
context.info("Accessing resource: " + id);
// Ping the client
context.ping();
String data = fetchData(id);
return new ReadResourceResult(List.of(
new TextResourceContents("data://" + id, "text/plain", data)
));
}
@Mcp提示
@McpPrompt 注解为AI交互生成提示消息。
基本用法
@Component
public class PromptProvider {
@McpPrompt(
name = "greeting",
description = "Generate a greeting message")
public GetPromptResult greeting(
@McpArg(name = "name", description = "User's name", required = true)
String name) {
String message = "Hello, " + name + "! How can I help you today?";
return new GetPromptResult(
"Greeting",
List.of(new PromptMessage(Role.ASSISTANT, new TextContent(message)))
);
}
}
带可选参数
@McpPrompt(
name = "personalized-message",
description = "Generate a personalized message")
public GetPromptResult personalizedMessage(
@McpArg(name = "name", required = true) String name,
@McpArg(name = "age", required = false) Integer age,
@McpArg(name = "interests", required = false) String interests) {
StringBuilder message = new StringBuilder();
message.append("Hello, ").append(name).append("!\n\n");
if (age != null) {
message.append("At ").append(age).append(" years old, ");
// Add age-specific content
}
if (interests != null && !interests.isEmpty()) {
message.append("Your interest in ").append(interests);
// Add interest-specific content
}
return new GetPromptResult(
"Personalized Message",
List.of(new PromptMessage(Role.ASSISTANT, new TextContent(message.toString())))
);
}
@完成的Mcp
@McpComplete 注解为提示提供了自动完成功能。
基本用法
@Component
public class CompletionProvider {
@McpComplete(prompt = "city-search")
public List<String> completeCityName(String prefix) {
return cities.stream()
.filter(city -> city.toLowerCase().startsWith(prefix.toLowerCase()))
.limit(10)
.toList();
}
}
使用CompleteRequest.CompleteArgument
@McpComplete(prompt = "travel-planner")
public List<String> completeTravelDestination(CompleteRequest.CompleteArgument argument) {
String prefix = argument.value().toLowerCase();
String argumentName = argument.name();
// Different completions based on argument name
if ("city".equals(argumentName)) {
return completeCities(prefix);
} else if ("country".equals(argumentName)) {
return completeCountries(prefix);
}
return List.of();
}
带有完整结果
@McpComplete(prompt = "code-completion")
public CompleteResult completeCode(String prefix) {
List<String> completions = generateCodeCompletions(prefix);
return new CompleteResult(
new CompleteResult.CompleteCompletion(
completions,
completions.size(), // total
hasMoreCompletions // hasMore flag
)
);
}
无状态与有状态实现对比
统一请求上下文(推荐)
使用 McpSyncRequestContext 或 McpAsyncRequestContext 以实现同时适用于有状态和无状态操作的统一接口:
public record UserInfo(String name, String email, int age) {}
@McpTool(name = "unified-tool", description = "Tool with unified request context")
public String unifiedTool(
McpSyncRequestContext context,
@McpToolParam(description = "Input", required = true) String input) {
// Access request and metadata
String progressToken = context.request().progressToken();
// Logging with convenient methods
context.info("Processing: " + input);
// Progress notifications (Note client should set a progress token
// with its request to be able to receive progress updates)
context.progress(50); // Simple percentage
// Ping client
context.ping();
// Check capabilities before using
if (context.elicitEnabled()) {
// Request user input (only in stateful mode)
StructuredElicitResult<UserInfo> elicitResult = context.elicit(UserInfo.class);
if (elicitResult.action() == ElicitResult.Action.ACCEPT) {
// Use elicited data
}
}
if (context.sampleEnabled()) {
// Request LLM sampling (only in stateful mode)
CreateMessageResult samplingResult = context.sample("Generate response");
// Use sampling result
}
return "Processed with unified context";
}
简单操作(无上下文)
对于简单操作,您可以完全省略上下文参数:
@McpTool(name = "simple-add", description = "Simple addition")
public int simpleAdd(
@McpToolParam(description = "First number", required = true) int a,
@McpToolParam(description = "Second number", required = true) int b) {
return a + b;
}
轻量级无状态(带Mcp传输上下文)
对于无需状态且需要最少传输上下文的操作:
@McpTool(name = "stateless-tool", description = "Stateless with transport context")
public String statelessTool(
McpTransportContext context,
@McpToolParam(description = "Input", required = true) String input) {
// Access transport-level context only
// No bidirectional operations (roots, elicitation, sampling)
return "Processed: " + input;
}
| 无状态服务器不支持双向操作: |
因此,在无状态模式下使用 McpSyncRequestContext 或 McpAsyncRequestContext 的方法将被忽略。
按服务器类型过滤方法
MCP注解框架会根据服务器类型和方法特性自动过滤标注的方法,确保每种服务器配置仅注册适当的方法。 对于每个被过滤的方法,都会记录一条警告信息以辅助调试。
同步与异步过滤器
同步服务器
同步服务器(配置为 spring.ai.mcp.server.type=SYNC)使用以下特点的同步提供程序:
-
接受具有非响应式返回类型的methods:
-
基本类型(
int,double,boolean) -
对象类型(
String,Integer,自定义POJO) -
MCP类型(
CallToolResult,ReadResourceResult,GetPromptResult,CompleteResult) -
集合(
List<String>,Map<String, Object>)
-
-
筛选出具有响应式返回类型的方法:
-
Mono<T> -
Flux<T> -
Publisher<T>
-
@Component
public class SyncTools {
@McpTool(name = "sync-tool", description = "Synchronous tool")
public String syncTool(String input) {
// This method WILL be registered on sync servers
return "Processed: " + input;
}
@McpTool(name = "async-tool", description = "Async tool")
public Mono<String> asyncTool(String input) {
// This method will be FILTERED OUT on sync servers
// A warning will be logged
return Mono.just("Processed: " + input);
}
}
异步服务器
异步服务器(配置为spring.ai.mcp.server.type=ASYNC)使用异步提供者,这些提供者能够:
-
接受具有响应式返回类型的methods:
-
Mono<T>(对于单个结果) -
Flux<T>(用于流式结果) -
Publisher<T>(泛型响应式类型)
-
-
筛选出具有非响应式返回类型的方法:
-
原始类型
-
对象类型
-
集合
-
MCP结果类型
-
@Component
public class AsyncTools {
@McpTool(name = "async-tool", description = "Async tool")
public Mono<String> asyncTool(String input) {
// This method WILL be registered on async servers
return Mono.just("Processed: " + input);
}
@McpTool(name = "sync-tool", description = "Sync tool")
public String syncTool(String input) {
// This method will be FILTERED OUT on async servers
// A warning will be logged
return "Processed: " + input;
}
}
有状态与无状态过滤
有状态服务器
有状态服务器支持双向通信,并接受带有以下特点的方法:
-
双向上下文参数:
-
McpSyncRequestContext(对于同步操作) -
McpAsyncRequestContext(用于异步操作) -
McpSyncServerExchange(传统,用于同步操作) -
McpAsyncServerExchange(传统,用于异步操作)
-
-
支持双向操作:
-
roots()- 访问根目录 -
elicit()- 请求用户输入 -
sample()- 请求LLM采样
-
@Component
public class StatefulTools {
@McpTool(name = "interactive-tool", description = "Tool with bidirectional operations")
public String interactiveTool(
McpSyncRequestContext context,
@McpToolParam(description = "Input", required = true) String input) {
// This method WILL be registered on stateful servers
// Can use elicitation, sampling, roots
if (context.sampleEnabled()) {
var samplingResult = context.sample("Generate response");
// Process sampling result...
}
return "Processed with context";
}
}
无状态服务器
无状态服务器针对简单的请求-响应模式进行了优化,并:
-
筛选出带有双向上下文参数的方法:
-
带有
McpSyncRequestContext的方法将被跳过 -
带有
McpAsyncRequestContext的方法将被跳过 -
带有
McpSyncServerExchange的方法将被跳过 -
带有
McpAsyncServerExchange的方法将被跳过 -
对于每个被过滤的方法,都会记录一条警告信息。
-
-
接受方法支持以下方式:
-
McpTransportContext(轻量级无状态上下文) -
完全没有上下文参数
-
仅常规
@McpToolParam个参数
-
-
不支持双向操作:
-
roots()- 不可用 -
elicit()- 不可用 -
sample()- 不可用
-
@Component
public class StatelessTools {
@McpTool(name = "simple-tool", description = "Simple stateless tool")
public String simpleTool(@McpToolParam(description = "Input") String input) {
// This method WILL be registered on stateless servers
return "Processed: " + input;
}
@McpTool(name = "context-tool", description = "Tool with transport context")
public String contextTool(
McpTransportContext context,
@McpToolParam(description = "Input") String input) {
// This method WILL be registered on stateless servers
return "Processed: " + input;
}
@McpTool(name = "bidirectional-tool", description = "Tool with bidirectional context")
public String bidirectionalTool(
McpSyncRequestContext context,
@McpToolParam(description = "Input") String input) {
// This method will be FILTERED OUT on stateless servers
// A warning will be logged
return "Processed with sampling";
}
}
过滤摘要
| 服务池类型设置 | 接受的方法 | 过滤方法 |
|---|---|---|
同步有状态 |
非响应式返回 + 双向上下文 |
响应式返回(Mono/Flux) |
异步有状态 |
响应式返回(Mono/Flux)+ 双向上下文 |
非响应式返回 |
同步无状态 |
非响应式返回 + 无双向上下文 |
响应式返回或双向上下文参数 |
异步无状态 |
响应式返回(Mono/Flux)+ 无双向上下文 |
非响应式返回或双向上下文参数 |
| 方法过滤的最佳实践: |
-
保持方法与服务器类型一致 - 同步服务器使用同步方法,异步服务器使用异步方法
-
将有状态和无状态的实现分离到不同的类中以提高清晰度
-
启动时检查日志 以获取过滤方法的警告信息
-
使用正确的上下文 -
McpSyncRequestContext/McpAsyncRequestContext表示有状态,McpTransportContext表示无状态 -
测试两种模式 如果您支持有状态和无状态部署
异步支持
所有服务器端注解都支持使用Reactor进行异步实现。
@Component
public class AsyncTools {
@McpTool(name = "async-fetch", description = "Fetch data asynchronously")
public Mono<String> asyncFetch(
@McpToolParam(description = "URL", required = true) String url) {
return Mono.fromCallable(() -> {
// Simulate async operation
return fetchFromUrl(url);
}).subscribeOn(Schedulers.boundedElastic());
}
@McpResource(uri = "async-data://{id}", name = "Async Data")
public Mono<ReadResourceResult> asyncResource(String id) {
return Mono.fromCallable(() -> {
String data = loadData(id);
return new ReadResourceResult(List.of(
new TextResourceContents("async-data://" + id, "text/plain", data)
));
}).delayElements(Duration.ofMillis(100));
}
}
Spring Boot 集成
使用Spring Boot的自动配置,注解Bean会自动被检测并注册:
@SpringBootApplication
public class McpServerApplication {
public static void main(String[] args) {
SpringApplication.run(McpServerApplication.class, args);
}
}
@Component
public class MyMcpTools {
// Your @McpTool annotated methods
}
@Component
public class MyMcpResources {
// Your @McpResource annotated methods
}
自动生成配置。
-
扫描带有MCP注释的Bean
-
创建适当的规范
-
将它们与MCP服务器进行注册
-
根据配置处理同步和异步的实现方式