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, Jihoon Kim
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddRoot(McpSchema.Root root) Add a roots dynamically.callTool(McpSchema.CallToolRequest callToolRequest) Calls a tool provided by the server.voidclose()booleancompleteCompletion(McpSchema.CompleteRequest completeRequest) Send a completion/complete request.Get the client capabilities that define the supported features and functionality.Get the client implementation information.Get the current initialization result.getPrompt(McpSchema.GetPromptRequest getPromptRequest) Get the server capabilities that define the supported features and functionality.Get the server implementation information.Get the server instructions that provide guidance to the client on how to interact with this server.The initialization phase MUST be the first interaction between client and server.booleanCheck if the client-server connection is initialized.Retrieves the list of all prompts provided by the server.listPrompts(String cursor) Retrieves a paginated list of prompts provided by the server.Retrieves the list of all resources provided by the server.listResources(String cursor) Retrieves a paginated list of resources provided by the server.Retrieves the list of all resource templates provided by the server.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.
-
Method Details
-
getCurrentInitializationResult
Get the current initialization result.- Returns:
- the initialization result.
-
getServerCapabilities
Get the server capabilities that define the supported features and functionality.- Returns:
- The server capabilities
-
getServerInstructions
Get the server instructions that provide guidance to the client on how to interact with this server.- Returns:
- The instructions
-
getServerInfo
Get the server implementation information.- Returns:
- The server implementation details
-
isInitialized
public boolean isInitialized()Check if the client-server connection is initialized.- Returns:
- true if the client-server connection is initialized
-
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 all 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
Retrieves the list of all resources provided by the server.- Returns:
- The list of all resources result
-
listResources
Retrieves a paginated list of resources provided by the server.- Parameters:
cursor- Optional pagination cursor from a previous 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
Retrieves the list of all resource templates provided by the server.- Returns:
- The list of all resource templates result.
-
listResourceTemplates
Resource templates allow servers to expose parameterized resources using URI templates. Arguments may be auto-completed through the completion API. Retrieves a paginated list of resource templates provided by the server.- Parameters:
cursor- Optional pagination cursor from a previous list request- 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
Retrieves the list of all prompts provided by the server.- Returns:
- The list of all prompts result.
-
listPrompts
Retrieves a paginated list of prompts provided by the server.- Parameters:
cursor- Optional pagination cursor from a previous list request- Returns:
- The list of prompts result.
-
getPrompt
-
setLoggingLevel
Client can set the minimum logging level it wants to receive from the server.- Parameters:
loggingLevel- the min logging level
-
completeCompletion
Send a completion/complete request.- Parameters:
completeRequest- the completion request contains the prompt or resource reference and arguments for generating suggestions.- Returns:
- the completion result containing suggested values.
-