public class McpSyncClient
extends java.lang.Object
implements java.lang.AutoCloseable
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:
The client follows the same lifecycle as its async counterpart:
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.
McpClient
,
McpAsyncClient
,
McpSchema
限定符和类型 | 方法和说明 |
---|---|
void |
addRoot(McpSchema.Root root)
Add a roots dynamically.
|
McpSchema.CallToolResult |
callTool(McpSchema.CallToolRequest callToolRequest)
Calls a tool provided by the server.
|
void |
close() |
boolean |
closeGracefully() |
McpSchema.CompleteResult |
completeCompletion(McpSchema.CompleteRequest completeRequest)
Send a completion/complete request.
|
McpSchema.ClientCapabilities |
getClientCapabilities()
Get the client capabilities that define the supported features and functionality.
|
McpSchema.Implementation |
getClientInfo()
Get the client implementation information.
|
McpSchema.InitializeResult |
getCurrentInitializationResult()
Get the current initialization result.
|
McpSchema.GetPromptResult |
getPrompt(McpSchema.GetPromptRequest getPromptRequest) |
McpSchema.ServerCapabilities |
getServerCapabilities()
Get the server capabilities that define the supported features and functionality.
|
McpSchema.Implementation |
getServerInfo()
Get the server implementation information.
|
java.lang.String |
getServerInstructions()
Get the server instructions that provide guidance to the client on how to interact
with this server.
|
McpSchema.InitializeResult |
initialize()
The initialization phase MUST be the first interaction between client and server.
|
boolean |
isInitialized()
Check if the client-server connection is initialized.
|
McpSchema.ListPromptsResult |
listPrompts()
Retrieves the list of all prompts provided by the server.
|
McpSchema.ListPromptsResult |
listPrompts(java.lang.String cursor)
Retrieves a paginated list of prompts provided by the server.
|
McpSchema.ListResourcesResult |
listResources()
Retrieves the list of all resources provided by the server.
|
McpSchema.ListResourcesResult |
listResources(java.lang.String cursor)
Retrieves a paginated list of resources provided by the server.
|
McpSchema.ListResourceTemplatesResult |
listResourceTemplates()
Retrieves the list of all resource templates provided by the server.
|
McpSchema.ListResourceTemplatesResult |
listResourceTemplates(java.lang.String cursor)
Resource templates allow servers to expose parameterized resources using URI
templates.
|
McpSchema.ListToolsResult |
listTools()
Retrieves the list of all tools provided by the server.
|
McpSchema.ListToolsResult |
listTools(java.lang.String cursor)
Retrieves a paginated list of tools provided by the server.
|
java.lang.Object |
ping()
Send a synchronous ping request.
|
McpSchema.ReadResourceResult |
readResource(McpSchema.ReadResourceRequest readResourceRequest)
Send a resources/read request.
|
McpSchema.ReadResourceResult |
readResource(McpSchema.Resource resource)
Send a resources/read request.
|
void |
removeRoot(java.lang.String rootUri)
Remove a root dynamically.
|
void |
rootsListChangedNotification()
Send a roots/list_changed notification.
|
void |
setLoggingLevel(McpSchema.LoggingLevel loggingLevel)
Client can set the minimum logging level it wants to receive from the server.
|
void |
subscribeResource(McpSchema.SubscribeRequest subscribeRequest)
Subscriptions.
|
void |
unsubscribeResource(McpSchema.UnsubscribeRequest unsubscribeRequest)
Send a resources/unsubscribe request.
|
public McpSchema.InitializeResult getCurrentInitializationResult()
public McpSchema.ServerCapabilities getServerCapabilities()
public java.lang.String getServerInstructions()
public McpSchema.Implementation getServerInfo()
public boolean isInitialized()
public McpSchema.ClientCapabilities getClientCapabilities()
public McpSchema.Implementation getClientInfo()
public void close()
close
在接口中 java.lang.AutoCloseable
public boolean closeGracefully()
public McpSchema.InitializeResult initialize()
McpSchema.ServerCapabilities
. public void rootsListChangedNotification()
public void addRoot(McpSchema.Root root)
public void removeRoot(java.lang.String rootUri)
public java.lang.Object ping()
public McpSchema.CallToolResult callTool(McpSchema.CallToolRequest callToolRequest)
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 schemapublic McpSchema.ListToolsResult listTools()
public McpSchema.ListToolsResult listTools(java.lang.String cursor)
cursor
- Optional pagination cursor from a previous list requestpublic McpSchema.ListResourcesResult listResources()
public McpSchema.ListResourcesResult listResources(java.lang.String cursor)
cursor
- Optional pagination cursor from a previous list requestpublic McpSchema.ReadResourceResult readResource(McpSchema.Resource resource)
resource
- the resource to readpublic McpSchema.ReadResourceResult readResource(McpSchema.ReadResourceRequest readResourceRequest)
readResourceRequest
- the read resource request.public McpSchema.ListResourceTemplatesResult listResourceTemplates()
public McpSchema.ListResourceTemplatesResult listResourceTemplates(java.lang.String cursor)
cursor
- Optional pagination cursor from a previous list requestpublic void subscribeResource(McpSchema.SubscribeRequest subscribeRequest)
subscribeRequest
- the subscribe request contains the uri of the resource to
subscribe to.public void unsubscribeResource(McpSchema.UnsubscribeRequest unsubscribeRequest)
unsubscribeRequest
- the unsubscribe request contains the uri of the resource
to unsubscribe from.public McpSchema.ListPromptsResult listPrompts()
public McpSchema.ListPromptsResult listPrompts(java.lang.String cursor)
cursor
- Optional pagination cursor from a previous list requestpublic McpSchema.GetPromptResult getPrompt(McpSchema.GetPromptRequest getPromptRequest)
public void setLoggingLevel(McpSchema.LoggingLevel loggingLevel)
loggingLevel
- the min logging levelpublic McpSchema.CompleteResult completeCompletion(McpSchema.CompleteRequest completeRequest)
completeRequest
- the completion request contains the prompt or resource
reference and arguments for generating suggestions.