跳到主要内容

SDK 内容

Java MCP 概览 [Java MCP 客户端] Java MCP 服务端

客户端功能

MCP 客户端是模型上下文协议 (MCP) 架构中的关键组件,负责建立和管理与 MCP 服务端的连接。它实现了协议的客户端部分,处理:
  • 协议版本协商,以确保与服务端的兼容性
  • 能力协商,以确定可用功能
  • 消息传输和 JSON-RPC 通信
  • 工具发现与执行
  • 资源访问与管理
  • 提示词系统交互
  • 可选功能,如根目录管理和采样支持
核心 io.modelcontextprotocol.sdk:mcp 模块提供了 STDIO、Streamable-HTTP 和 SSE 客户端传输实现,无需外部 Web 框架。对于 Spring Framework 用户,Spring 特有的传输实现作为可选依赖项 io.modelcontextprotocol.sdk:mcp-spring-webflux 提供。
这个基于 Spring AI MCP 的快速入门演示将向您展示如何构建一个连接到 MCP 服务端的 AI 客户端。
客户端提供同步和异步 API,以便在不同的应用场景中灵活使用。
// Create a sync client with custom configuration
McpSyncClient client = McpClient.sync(transport)
    .requestTimeout(Duration.ofSeconds(10))
    .capabilities(ClientCapabilities.builder()
        .roots(true)      // Enable roots capability
        .sampling()       // Enable sampling capability
        .elicitation()    // Enable elicitation capability
        .build())
    .sampling(request -> CreateMessageResult.builder()...build())
    .elicitation(elicitRequest -> ElicitResult.builder()...build())
    .toolsChangeConsumer((List<McpSchema.Tool> tools) -> ...)
    .resourcesChangeConsumer((List<McpSchema.Resource> resources) -> ...)
    .promptsChangeConsumer((List<McpSchema.Prompt> prompts) -> ...)
    .loggingConsumer((LoggingMessageNotification logging) -> ...)
    .progressConsumer((ProgressNotification progress) -> ...)
    .build();

// Initialize connection
client.initialize();

// List available tools
ListToolsResult tools = client.listTools();

// Call a tool
CallToolResult result = client.callTool(
    new CallToolRequest("calculator",
        Map.of("operation", "add", "a", 2, "b", 3))
);

// List and read resources
ListResourcesResult resources = client.listResources();
ReadResourceResult resource = client.readResource(
    new ReadResourceRequest("resource://uri")
);

// List and use prompts
ListPromptsResult prompts = client.listPrompts();
GetPromptResult prompt = client.getPrompt(
    new GetPromptRequest("greeting", Map.of("name", "Spring"))
);

// Add/remove roots
client.addRoot(new Root("file:///path", "description"));
client.removeRoot("file:///path");

// Close client
client.closeGracefully();

客户端传输

传输层处理 MCP 客户端和服务端之间的通信,为各种用例提供不同的实现。客户端传输管理消息序列化、连接建立以及特定协议的通信模式。
为基于进程内的通信创建传输
ServerParameters params = ServerParameters.builder("npx")
    .args("-y", "@modelcontextprotocol/server-everything", "dir")
    .build();
McpTransport transport = new StdioClientTransport(params);

客户端能力

可以为客户端配置各种能力
var capabilities = ClientCapabilities.builder()
    .roots(true)      // Enable filesystem roots support with list changes notifications
    .sampling()       // Enable LLM sampling support
    .elicitation()    // Enable elicitation capability
    .build();

Roots (根目录) 支持

Roots (根目录) 定义了服务端在文件系统中可以操作的边界
// 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();
Roots 能力允许服务端:
  • 请求可访问的文件系统根目录列表
  • 在根目录列表发生变化时接收通知
  • 了解其拥有访问权限的目录和文件

Sampling (采样) 支持

Sampling (采样) 允许服务端通过客户端请求 LLM 交互(“补全”或“生成”)
// 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();
此能力允许:
  • 服务端无需 API 密钥即可利用 AI 能力
  • 客户端保持对模型访问和权限的控制
  • 支持基于文本和图像的交互
  • 可选地在提示词中包含 MCP 服务端上下文

Elicitation (启发/诱导) 支持

Elicitation (启发) 允许服务端向客户端请求特定信息或澄清
// Configure elicitation handler
Function<ElicitRequest, ElicitResult> elicitationHandler = request -> {
    // Elicitation implementation that interfaces with LLM
    return ElicitResult.builder()...build();
};

// Create client with elicitation support
var client = McpClient.sync(transport)
    .capabilities(ClientCapabilities.builder()
        .elicitation() // enable elicitation capability
        .build())
    .elicitation(elicitationHandler) // register elicitation handler
    .build();

Logging (日志) 支持

客户端可以注册一个日志消费者以接收来自服务端的日志消息,并设置最小日志级别来过滤消息
var mcpClient = McpClient.sync(transport)
        .loggingConsumer((LoggingMessageNotification notification) -> {
            System.out.println("Received log message: " + notification.data());
        })
        .build();

mcpClient.initialize();

mcpClient.setLoggingLevel(McpSchema.LoggingLevel.INFO);

// Call the tool that can send logging notifications
CallToolResult result = mcpClient.callTool(new McpSchema.CallToolRequest("logging-test", Map.of()));
客户端可以通过 mcpClient.setLoggingLevel(level) 请求控制其接收的最小日志级别。低于设定级别的消息将被过滤掉。支持的日志级别(按严重程度递增排序):DEBUG (0), INFO (1), NOTICE (2), WARNING (3), ERROR (4), CRITICAL (5), ALERT (6), EMERGENCY (7)

Progress (进度) 支持

客户端可以注册一个进度消费者以接收来自服务端的进度更新
var mcpClient = McpClient.sync(transport)
        .progressConsumer((ProgressNotification progress) -> {
            System.out.println("Received progress update: " + progress.data());
        })
        .build();

mcpClient.initialize();

// Call the tool that can send progress notifications
CallToolResult result = mcpClient.callTool(new McpSchema.CallToolRequest("progress-test", Map.of()));

变更通知

客户端可以注册一个变更消费者,以接收来自服务端关于工具、资源或提示词更新的变更通知

var spec = McpClient.sync(transport);

// Adds a consumer to be notified when the available tools change, such as tools
// being added or removed.
spec.toolsChangeConsumer((List<McpSchema.Tool> tools) -> {
    // Handle tools change
});

// Adds a consumer to be notified when the available resources change, such as resources
// being added or removed.
spec.resourcesChangeConsumer((List<McpSchema.Resource> resources) -> {
    // Handle resources change
});

// Adds a consumer to be notified when the available prompts change, such as prompts
// being added or removed.
spec.promptsChangeConsumer((List<McpSchema.Prompt> prompts) -> {
    // Handle prompts change

});

使用 MCP 客户端

工具执行

工具是客户端可以发现并执行的服务端函数。MCP 客户端提供了列出可用工具并使用特定参数执行它们的方法。每个工具都有唯一的名称并接受一个参数映射。
// List available tools and their names
var tools = client.listTools();
tools.forEach(tool -> System.out.println(tool.getName()));

// Execute a tool with parameters
var result = client.callTool("calculator", Map.of(
    "operation", "add",
    "a", 1,
    "b", 2
));

资源访问

资源代表客户端可以使用 URI 模板访问的服务端数据源。MCP 客户端提供了发现可用资源并通过标准化接口检索其内容的方法。
// List available resources and their names
var resources = client.listResources();
resources.forEach(resource -> System.out.println(resource.getName()));

// Retrieve resource content using a URI template
var content = client.getResource("file", Map.of(
    "path", "/path/to/file.txt"
));

提示词 (Prompt) 系统

提示词系统允许与服务端提示词模板进行交互。这些模板可以被发现并使用自定义参数执行,从而允许根据预定义模式动态生成文本。
// List available prompt templates
var prompts = client.listPrompts();
prompts.forEach(prompt -> System.out.println(prompt.getName()));

// Execute a prompt template with parameters
var response = client.executePrompt("echo", Map.of(
    "text", "Hello, World!"
));

使用补全 (Completion)

作为 补全能力 (Completion capabilities) 的一部分,MCP 为服务端提供了一种标准化方式来为提示词和资源 URI 提供参数自动补全建议。 请查看 服务端补全能力 以了解如何在服务端启用和配置补全。 在客户端,MCP 客户端提供了请求自动补全的方法:

CompleteRequest request = new CompleteRequest(
        new PromptReference("code_review"),
        new CompleteRequest.CompleteArgument("language", "py"));

CompleteResult result = syncMcpClient.completeCompletion(request);

添加上下文信息

通过 SSE 或 Streamable HTTP 传输发送的 HTTP 请求可以使用专用 API 进行自定义(参见 客户端传输)。这些自定义器可能需要必须在客户端级别注入的额外上下文特定信息。
McpSyncClient 用于阻塞环境,可能依赖线程本地变量 (thread-locals) 来共享信息。例如,某些框架将当前服务器请求或安全令牌存储在线程本地变量中。若要使此类信息对底层传输可用,请使用 SyncSpec#transportContextProvider
McpClient.sync(transport)
    .transportContextProvider(() -> {
        var data = obtainDataFromThreadLocals();
        return McpTransportContext.create(
                Map.of("some-data", data)
        );
    })
    .build();
McpTransportContext 将在基于 HttpClient 的 McpSyncHttpClientRequestCustomizer 和基于 WebClient 的 ExchangeFilterFunction 中可用。