Interface McpClient
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
DefaultMcpClient
-
Method Summary
Modifier and TypeMethodDescriptionvoidPerforms a health check that returns normally if the MCP server is reachable and properly responding to ping requests.executeTool(ToolExecutionRequest executionRequest) Executes a tool on the MCP server and returns the result.executeTool(ToolExecutionRequest executionRequest, InvocationContext invocationContext) Executes a tool on the MCP server and returns the result.default CompletableFuture<ToolExecutionResult> executeToolAsync(ToolExecutionRequest executionRequest, InvocationContext invocationContext) Non-blocking counterpart ofexecuteTool(ToolExecutionRequest, InvocationContext): executes a tool on the MCP server without holding a thread while the result is in flight.Render the contents of a prompt.default @Nullable StringReturns the server instructions from the initialize result, or null if the server did not provide any.key()Returns the unique key of this client.Obtain a list of prompts available on the MCP server.Obtains the current list of resources available on the MCP server.listResources(InvocationContext invocationContext) Obtains the current list of resources available on the MCP server.Obtains the current list of resource templates (dynamic resources) available on the MCP server.listResourceTemplates(InvocationContext invocationContext) Obtains the current list of resource templates (dynamic resources) available on the MCP server.Obtains a list of tools from the MCP server.listTools(InvocationContext invocationContext) Obtains a list of tools from the MCP server.readResource(String uri) Retrieves the contents of the resource with the specified URI.readResource(String uri, InvocationContext invocationContext) Retrieves the contents of the resource with the specified URI.voidDeprecated.Roots are deprecated in MCP protocol version 2026-07-28.voidDeprecated.UsesubscribeToResources(List)for MCP 2026-07-28 and later.default longsubscribeToResources(List<String> uris) Subscribes to resource content update notifications for the given URIs.voidDeprecated.UseunsubscribeFromResources(long)for MCP 2026-07-28 and later.default voidunsubscribeFromResources(long subscriptionId) Unsubscribes from resource content update notifications for the given subscription ID.Methods inherited from interface AutoCloseable
close
-
Method Details
-
key
String key()Returns the unique key of this client. -
instructions
Returns the server instructions from the initialize result, or null if the server did not provide any. -
listTools
List<ToolSpecification> listTools()Obtains a list of tools from the MCP server. -
listTools
Obtains a list of tools from the MCP server. -
executeTool
Executes a tool on the MCP server and returns the result. Currently, this expects a tool execution to only contain text-based results or JSON structured content. -
executeTool
ToolExecutionResult executeTool(ToolExecutionRequest executionRequest, InvocationContext invocationContext) Executes a tool on the MCP server and returns the result. Currently, this expects a tool execution to only contain text-based results or JSON structured content. -
executeToolAsync
@Experimental default CompletableFuture<ToolExecutionResult> executeToolAsync(ToolExecutionRequest executionRequest, InvocationContext invocationContext) Non-blocking counterpart ofexecuteTool(ToolExecutionRequest, InvocationContext): executes a tool on the MCP server without holding a thread while the result is in flight.The default implementation returns a failed future carrying
AsyncNotSupportedException.- Since:
- 1.20.0
-
listResources
List<McpResource> listResources()Obtains the current list of resources available on the MCP server. -
listResources
Obtains the current list of resources available on the MCP server. -
listResourceTemplates
List<McpResourceTemplate> listResourceTemplates()Obtains the current list of resource templates (dynamic resources) available on the MCP server. -
listResourceTemplates
Obtains the current list of resource templates (dynamic resources) available on the MCP server. -
readResource
Retrieves the contents of the resource with the specified URI. This also works for dynamic resources (templates). -
readResource
Retrieves the contents of the resource with the specified URI. This also works for dynamic resources (templates). -
subscribeToResource
Deprecated.UsesubscribeToResources(List)for MCP 2026-07-28 and later. ThrowsUnsupportedOperationExceptionwhen using the modern protocol.Subscribes to updates for the resource with the specified URI. When the resource changes, the server will send anotifications/resources/updatednotification. The client will invoke theonResourceUpdatedcallback (if configured) with the URI of the updated resource. -
unsubscribeFromResource
Deprecated.UseunsubscribeFromResources(long)for MCP 2026-07-28 and later. ThrowsUnsupportedOperationExceptionwhen using the modern protocol.Unsubscribes from updates for the resource with the specified URI. -
subscribeToResources
Subscribes to resource content update notifications for the given URIs. Once subscribed, the server notifies the client whenever one of those resources changes; the notifications are delivered to the callback registered viaDefaultMcpClient.Builder#onResourceUpdated.The MCP specification requires the server to confirm a subscription before it sends anything on it, so this method blocks until that confirmation arrives and fails if it does not. When it returns normally, the subscription is established and the returned subscription ID can be passed to
unsubscribeFromResources(long)to stop receiving the notifications. How long the client waits for the confirmation is governed byDefaultMcpClient.Builder#resourcesTimeout(Duration).Because this method blocks, do not call it from inside a notification callback such as
onResourceUpdated: those callbacks run on the thread that reads messages from the server, which is the same thread that would have to deliver the confirmation.Only available with MCP protocol version 2026-07-28 and later.
- Parameters:
uris- the list of resource URIs to subscribe to- Returns:
- a subscription ID
- Throws:
UnsupportedOperationException- when using legacy protocol (2025-11-25)McpException- if the server rejects the subscriptionIllegalStateException- if the server confirms the subscription but declines to honour the requested resource URIs, if it ends the subscription without ever confirming it, or if the client is closed while the call is still waitingCancellationException- if the subscription is cancelled before the server confirms it, for example by a concurrentunsubscribeFromResources(long)RuntimeException- if the underlying transport fails, or the server does not confirm the subscription in time
-
unsubscribeFromResources
default void unsubscribeFromResources(long subscriptionId) Unsubscribes from resource content update notifications for the given subscription ID. Only available with MCP protocol version 2026-07-28 and later.- Parameters:
subscriptionId- the subscription ID returned bysubscribeToResources(List)- Throws:
UnsupportedOperationException- when using legacy protocol (2025-11-25)
-
listPrompts
-
getPrompt
Render the contents of a prompt. -
checkHealth
void checkHealth()Performs a health check that returns normally if the MCP server is reachable and properly responding to ping requests. If this method throws an exception, the health of this MCP client is considered degraded. -
setRoots
Deprecated.Roots are deprecated in MCP protocol version 2026-07-28. This method throwsUnsupportedOperationExceptionwhen the modern protocol is in use.Sets the roots that are made available to the server upon its request. After calling this method, the client also sends anotifications/roots/list_changedmessage to the server.
-