From 0b0d5086ce740aa698bcb8990b86229ebc295129 Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Wed, 1 Jan 2025 14:00:37 +0100 Subject: [PATCH] feat: add server/client metadata getters to MCP clients Add getter methods to expose server and client capabilities and implementation info in McpAsyncClient and McpSyncClient. Store server metadata received during initialization for later access. --- .../ai/mcp/client/McpAsyncClient.java | 55 +++++++++++++++++-- .../ai/mcp/client/McpSyncClient.java | 33 +++++++++++ 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/mcp/src/main/java/org/springframework/ai/mcp/client/McpAsyncClient.java b/mcp/src/main/java/org/springframework/ai/mcp/client/McpAsyncClient.java index 3931c77..bf1f17c 100644 --- a/mcp/src/main/java/org/springframework/ai/mcp/client/McpAsyncClient.java +++ b/mcp/src/main/java/org/springframework/ai/mcp/client/McpAsyncClient.java @@ -77,6 +77,21 @@ public class McpAsyncClient { */ private final McpSchema.ClientCapabilities clientCapabilities; + /** + * Client implementation information. + */ + private final McpSchema.Implementation clientInfo; + + /** + * Server capabilities. + */ + private McpSchema.ServerCapabilities serverCapabilities; + + /** + * Server implementation information. + */ + private McpSchema.Implementation serverInfo; + /** * Roots define the boundaries of where servers can operate within the filesystem, * allowing them to understand which directories and files they have access to. @@ -100,11 +115,6 @@ public class McpAsyncClient { */ private final McpTransport transport; - /** - * Client implementation information. - */ - private Implementation clientInfo; - /** * Create a new McpAsyncClient with the given transport and session request-response * timeout. @@ -241,6 +251,9 @@ public class McpAsyncClient { return result.flatMap(initializeResult -> { + this.serverCapabilities = initializeResult.capabilities(); + this.serverInfo = initializeResult.serverInfo(); + logger.info("Server response with Protocol: {}, Capabilities: {}, Info: {} and Instructions {}", initializeResult.protocolVersion(), initializeResult.capabilities(), initializeResult.serverInfo(), initializeResult.instructions()); @@ -256,6 +269,38 @@ public class McpAsyncClient { }); } + /** + * Get the server capabilities that define the supported features and functionality. + * @return The server capabilities + */ + public McpSchema.ServerCapabilities getServerCapabilities() { + return this.serverCapabilities; + } + + /** + * Get the server implementation information. + * @return The server implementation details + */ + public McpSchema.Implementation getServerInfo() { + return this.serverInfo; + } + + /** + * Get the client capabilities that define the supported features and functionality. + * @return The client capabilities + */ + public ClientCapabilities getClientCapabilities() { + return this.clientCapabilities; + } + + /** + * Get the client implementation information. + * @return The client implementation details + */ + public McpSchema.Implementation getClientInfo() { + return this.clientInfo; + } + /** * Closes the client connection immediately. */ diff --git a/mcp/src/main/java/org/springframework/ai/mcp/client/McpSyncClient.java b/mcp/src/main/java/org/springframework/ai/mcp/client/McpSyncClient.java index 866e4d2..8cc018a 100644 --- a/mcp/src/main/java/org/springframework/ai/mcp/client/McpSyncClient.java +++ b/mcp/src/main/java/org/springframework/ai/mcp/client/McpSyncClient.java @@ -22,6 +22,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.ai.mcp.spec.McpSchema; +import org.springframework.ai.mcp.spec.McpSchema.ClientCapabilities; import org.springframework.ai.mcp.spec.McpSchema.GetPromptRequest; import org.springframework.ai.mcp.spec.McpSchema.GetPromptResult; import org.springframework.ai.mcp.spec.McpSchema.ListPromptsResult; @@ -49,6 +50,38 @@ public class McpSyncClient implements AutoCloseable { this.delegate = delegate; } + /** + * Get the server capabilities that define the supported features and functionality. + * @return The server capabilities + */ + public McpSchema.ServerCapabilities getServerCapabilities() { + return this.delegate.getServerCapabilities(); + } + + /** + * Get the server implementation information. + * @return The server implementation details + */ + public McpSchema.Implementation getServerInfo() { + return this.delegate.getServerInfo(); + } + + /** + * Get the client capabilities that define the supported features and functionality. + * @return The client capabilities + */ + public ClientCapabilities getClientCapabilities() { + return this.delegate.getClientCapabilities(); + } + + /** + * Get the client implementation information. + * @return The client implementation details + */ + public McpSchema.Implementation getClientInfo() { + return this.delegate.getClientInfo(); + } + @Override public void close() { this.delegate.close();