java.lang.Object
io.modelcontextprotocol.client.McpSyncClient
- All Implemented Interfaces:
AutoCloseable
A synchronous client implementation for the Model Context Protocol (MCP) that wraps an
McpAsyncClient to provide blocking operations.
This client implements the MCP specification by delegating to an asynchronous client and blocking on the results. Key features include:
- Synchronous, blocking API for simpler integration in non-reactive applications
- Tool discovery and invocation for server-provided functionality
- Resource access and management with URI-based addressing
- Prompt template handling for standardized AI interactions
- Real-time notifications for tools, resources, and prompts changes
- Structured logging with configurable severity levels
The client follows the same lifecycle as its async counterpart:
- Initialization - Establishes connection and negotiates capabilities
- Normal Operation - Handles requests and notifications
- Graceful Shutdown - Ensures clean connection termination
This implementation implements AutoCloseable for resource cleanup and provides
both immediate and graceful shutdown options. All operations block until completion or
timeout, making it suitable for traditional synchronous programming models.
- Author:
- Dariusz Jędrzejczyk, Christian Tzolov
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionMcpSyncClient(McpAsyncClient delegate) Deprecated.This method will be removed in 0.9.0. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddRoot(McpSchema.Root root) Add a roots dynamically.callTool(McpSchema.CallToolRequest callToolRequest) Calls a tool provided by the server.voidclose()booleanGet the client capabilities that define the supported features and functionality.Get the client implementation information.getPrompt(McpSchema.GetPromptRequest getPromptRequest) Get the server capabilities that define the supported features and functionality.Get the server implementation information.The initialization phase MUST be the first interaction between client and server.listPrompts(String cursor) Send a resources/list request.listResources(String cursor) Send a resources/list request.Request a list of resource templates the server has.listResourceTemplates(String cursor) Resource templates allow servers to expose parameterized resources using URI templates.Retrieves the list of all tools provided by the server.Retrieves a paginated list of tools provided by the server.ping()Send a synchronous ping request.readResource(McpSchema.ReadResourceRequest readResourceRequest) Send a resources/read request.readResource(McpSchema.Resource resource) Send a resources/read request.voidremoveRoot(String rootUri) Remove a root dynamically.voidSend a roots/list_changed notification.voidsetLoggingLevel(McpSchema.LoggingLevel loggingLevel) Client can set the minimum logging level it wants to receive from the server.voidsubscribeResource(McpSchema.SubscribeRequest subscribeRequest) Subscriptions.voidunsubscribeResource(McpSchema.UnsubscribeRequest unsubscribeRequest) Send a resources/unsubscribe request.
-
Constructor Details
-
McpSyncClient
Deprecated.This method will be removed in 0.9.0. UseMcpClient.sync(McpClientTransport)to obtain an instance.Create a new McpSyncClient with the given delegate.- Parameters:
delegate- the asynchronous kernel on top of which this synchronous client provides a blocking API.
-
-
Method Details
-
getServerCapabilities
Get the server capabilities that define the supported features and functionality.- Returns:
- The server capabilities
-
getServerInfo
Get the server implementation information.- Returns:
- The server implementation details
-
getClientCapabilities
Get the client capabilities that define the supported features and functionality.- Returns:
- The client capabilities
-
getClientInfo
Get the client implementation information.- Returns:
- The client implementation details
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
closeGracefully
public boolean closeGracefully() -
initialize
The initialization phase MUST be the first interaction between client and server. During this phase, the client and server:- Establish protocol version compatibility
- Exchange and negotiate capabilities
- Share implementation details
The client MUST initiate this phase by sending an initialize request containing:- The protocol version the client supports
- The client's capabilities
- Client implementation information
McpSchema.ServerCapabilities.
After successful initialization, the client MUST send an initialized notification to indicate it is ready to begin normal operations.
Initialization Spec- Returns:
- the initialize result.
-
rootsListChangedNotification
public void rootsListChangedNotification()Send a roots/list_changed notification. -
addRoot
Add a roots dynamically. -
removeRoot
Remove a root dynamically. -
ping
Send a synchronous ping request.- Returns:
-
callTool
Calls a tool provided by the server. Tools enable servers to expose executable functionality that can interact with external systems, perform computations, and take actions in the real world.- Parameters:
callToolRequest- The request containing: - name: The name of the tool to call (must match a tool name from tools/list) - arguments: Arguments that conform to the tool's input schema- Returns:
- The tool execution result containing: - content: List of content items (text, images, or embedded resources) representing the tool's output - isError: Boolean indicating if the execution failed (true) or succeeded (false/absent)
-
listTools
Retrieves the list of all tools provided by the server.- Returns:
- The list of tools result containing: - tools: List of available tools, each with a name, description, and input schema - nextCursor: Optional cursor for pagination if more tools are available
-
listTools
Retrieves a paginated list of tools provided by the server.- Parameters:
cursor- Optional pagination cursor from a previous list request- Returns:
- The list of tools result containing: - tools: List of available tools, each with a name, description, and input schema - nextCursor: Optional cursor for pagination if more tools are available
-
listResources
Send a resources/list request.- Parameters:
cursor- the cursor- Returns:
- the list of resources result.
-
listResources
Send a resources/list request.- Returns:
- the list of resources result.
-
readResource
Send a resources/read request.- Parameters:
resource- the resource to read- Returns:
- the resource content.
-
readResource
Send a resources/read request.- Parameters:
readResourceRequest- the read resource request.- Returns:
- the resource content.
-
listResourceTemplates
Resource templates allow servers to expose parameterized resources using URI templates. Arguments may be auto-completed through the completion API. Request a list of resource templates the server has.- Parameters:
cursor- the cursor- Returns:
- the list of resource templates result.
-
listResourceTemplates
Request a list of resource templates the server has.- Returns:
- the list of resource templates result.
-
subscribeResource
Subscriptions. The protocol supports optional subscriptions to resource changes. Clients can subscribe to specific resources and receive notifications when they change. Send a resources/subscribe request.- Parameters:
subscribeRequest- the subscribe request contains the uri of the resource to subscribe to.
-
unsubscribeResource
Send a resources/unsubscribe request.- Parameters:
unsubscribeRequest- the unsubscribe request contains the uri of the resource to unsubscribe from.
-
listPrompts
-
listPrompts
-
getPrompt
-
setLoggingLevel
Client can set the minimum logging level it wants to receive from the server.- Parameters:
loggingLevel- the min logging level
-