Class DefaultMcpClient
- All Implemented Interfaces:
McpClient, AutoCloseable
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic DefaultMcpClient.Builderbuilder()voidPerforms a health check that returns normally if the MCP server is reachable and properly responding to ping requests.voidclose()voidEvicts the tool list cache, forcing the next call tolistTools()to retrieve a fresh list of tools from the MCP server.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.Render the contents of a prompt.@Nullable StringReturns the server instructions from the initialize result, or null if the server did not provide any.booleankey()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.voidSets the roots that are made available to the server upon its request.voidSubscribes to updates for the resource with the specified URI.longsubscribeToResources(List<String> uris) Subscribes to resource content update notifications for the given URIs.voidUnsubscribes from updates for the resource with the specified URI.voidunsubscribeFromResources(long subscriptionId) Unsubscribes from resource content update notifications for the given subscription ID.
-
Constructor Details
-
DefaultMcpClient
-
-
Method Details
-
isModernProtocol
public boolean isModernProtocol() -
key
-
instructions
Description copied from interface:McpClientReturns the server instructions from the initialize result, or null if the server did not provide any.- Specified by:
instructionsin interfaceMcpClient
-
listTools
Description copied from interface:McpClientObtains a list of tools from the MCP server. -
listTools
Description copied from interface:McpClientObtains a list of tools from the MCP server. -
evictToolListCache
public void evictToolListCache()Evicts the tool list cache, forcing the next call tolistTools()to retrieve a fresh list of tools from the MCP server. -
executeTool
Description copied from interface:McpClientExecutes 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.- Specified by:
executeToolin interfaceMcpClient
-
executeTool
public ToolExecutionResult executeTool(ToolExecutionRequest executionRequest, InvocationContext invocationContext) Description copied from interface:McpClientExecutes 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.- Specified by:
executeToolin interfaceMcpClient
-
listResources
Description copied from interface:McpClientObtains the current list of resources available on the MCP server.- Specified by:
listResourcesin interfaceMcpClient
-
listResources
Description copied from interface:McpClientObtains the current list of resources available on the MCP server.- Specified by:
listResourcesin interfaceMcpClient
-
readResource
Description copied from interface:McpClientRetrieves the contents of the resource with the specified URI. This also works for dynamic resources (templates).- Specified by:
readResourcein interfaceMcpClient
-
readResource
Description copied from interface:McpClientRetrieves the contents of the resource with the specified URI. This also works for dynamic resources (templates).- Specified by:
readResourcein interfaceMcpClient
-
listPrompts
Description copied from interface:McpClientObtain a list of prompts available on the MCP server.- Specified by:
listPromptsin interfaceMcpClient
-
getPrompt
-
checkHealth
public void checkHealth()Description copied from interface:McpClientPerforms 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.- Specified by:
checkHealthin interfaceMcpClient
-
setRoots
Description copied from interface:McpClientSets 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. -
subscribeToResource
Description copied from interface:McpClientSubscribes 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.- Specified by:
subscribeToResourcein interfaceMcpClient
-
unsubscribeFromResource
Description copied from interface:McpClientUnsubscribes from updates for the resource with the specified URI.- Specified by:
unsubscribeFromResourcein interfaceMcpClient
-
subscribeToResources
Description copied from interface:McpClientSubscribes 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
McpClient.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.
- Specified by:
subscribeToResourcesin interfaceMcpClient- Parameters:
uris- the list of resource URIs to subscribe to- Returns:
- a subscription ID
-
unsubscribeFromResources
public void unsubscribeFromResources(long subscriptionId) Description copied from interface:McpClientUnsubscribes from resource content update notifications for the given subscription ID. Only available with MCP protocol version 2026-07-28 and later.- Specified by:
unsubscribeFromResourcesin interfaceMcpClient- Parameters:
subscriptionId- the subscription ID returned byMcpClient.subscribeToResources(List)
-
listResourceTemplates
Description copied from interface:McpClientObtains the current list of resource templates (dynamic resources) available on the MCP server.- Specified by:
listResourceTemplatesin interfaceMcpClient
-
listResourceTemplates
Description copied from interface:McpClientObtains the current list of resource templates (dynamic resources) available on the MCP server.- Specified by:
listResourceTemplatesin interfaceMcpClient
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
builder
-