public class AcpSyncClient
extends java.lang.Object
implements java.lang.AutoCloseable
AcpAsyncClient to provide blocking operations.
This client implements the ACP 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.
Example usage:
try (AcpSyncClient client = AcpClient.sync(transport).build()) {
// Initialize
AcpSchema.InitializeResponse initResponse = client.initialize(
new AcpSchema.InitializeRequest(1, new AcpSchema.ClientCapabilities()));
// Create session
AcpSchema.NewSessionResponse sessionResponse = client.newSession(
new AcpSchema.NewSessionRequest("/workspace", List.of()));
// Send prompt
AcpSchema.PromptResponse response = client.prompt(
new AcpSchema.PromptRequest(sessionResponse.sessionId(),
List.of(new AcpSchema.TextContent("Fix the bug"))));
System.out.println("Stop reason: " + response.stopReason());
}
AcpClient,
AcpAsyncClient,
AcpSchema| 限定符和类型 | 方法和说明 |
|---|---|
AcpSchema.AuthenticateResponse |
authenticate(AcpSchema.AuthenticateRequest authenticateRequest)
Authenticates with the agent using the specified authentication method.
|
void |
cancel(AcpSchema.CancelNotification cancelNotification)
Cancels ongoing operations for a session.
|
void |
close()
Closes the client connection and waits for shutdown to complete.
|
boolean |
closeGracefully()
Gracefully closes the client connection with a default timeout.
|
NegotiatedCapabilities |
getAgentCapabilities()
Returns the capabilities negotiated with the agent during initialization.
|
AcpSchema.InitializeResponse |
initialize()
Initializes the ACP client with default settings.
|
AcpSchema.InitializeResponse |
initialize(AcpSchema.InitializeRequest initializeRequest)
Initializes the connection with the agent.
|
AcpSchema.LoadSessionResponse |
loadSession(AcpSchema.LoadSessionRequest loadSessionRequest)
Loads an existing agent session by ID.
|
AcpSchema.NewSessionResponse |
newSession(AcpSchema.NewSessionRequest newSessionRequest)
Creates a new agent session with the specified working directory.
|
AcpSchema.PromptResponse |
prompt(AcpSchema.PromptRequest promptRequest)
Sends a prompt to the agent within a session.
|
AcpSchema.SetSessionModeResponse |
setSessionMode(AcpSchema.SetSessionModeRequest setModeRequest)
Sets the operational mode for a session (e.g., "code", "plan", "review").
|
AcpSchema.SetSessionModelResponse |
setSessionModel(AcpSchema.SetSessionModelRequest setModelRequest)
Sets the AI model for the specified session.
|
public void close()
This method blocks until the connection is closed or the timeout is reached. For synchronous clients, this ensures resources are fully released when try-with-resources completes.
close 在接口中 java.lang.AutoCloseablepublic boolean closeGracefully()
public AcpSchema.InitializeResponse initialize(AcpSchema.InitializeRequest initializeRequest)
The client sends its protocol version and capabilities, and the agent responds with its supported protocol version, authentication methods, and capabilities.
initializeRequest - the initialization request containing protocol version and
client capabilitiesAcpSchema.METHOD_INITIALIZEpublic AcpSchema.InitializeResponse initialize()
Uses protocol version 1 and default client capabilities. This is a convenience method for the common case where no special capabilities need to be advertised.
initialize(AcpSchema.InitializeRequest)public NegotiatedCapabilities getAgentCapabilities()
This method returns null if initialize(com.agentclientprotocol.sdk.spec.AcpSchema.InitializeRequest) has not been called yet.
public AcpSchema.AuthenticateResponse authenticate(AcpSchema.AuthenticateRequest authenticateRequest)
Authentication is optional and depends on the agent's configuration. The authentication methods available are returned in the initialize response.
authenticateRequest - the authentication request specifying the auth method
and credentialsAcpSchema.METHOD_AUTHENTICATEpublic AcpSchema.NewSessionResponse newSession(AcpSchema.NewSessionRequest newSessionRequest)
A session represents a conversation context with the agent. All prompts within a session share the same working directory and conversation history.
newSessionRequest - the session creation request with working directory and
initial contextAcpSchema.METHOD_SESSION_NEWpublic AcpSchema.LoadSessionResponse loadSession(AcpSchema.LoadSessionRequest loadSessionRequest)
This allows resuming a previous conversation with the agent, maintaining the full history and context.
loadSessionRequest - the session load request with session IDAcpSchema.METHOD_SESSION_LOADpublic AcpSchema.SetSessionModeResponse setSessionMode(AcpSchema.SetSessionModeRequest setModeRequest)
Different modes may change how the agent processes prompts and what capabilities it exposes.
setModeRequest - the set mode request with session ID and desired modeAcpSchema.METHOD_SESSION_SET_MODEpublic AcpSchema.SetSessionModelResponse setSessionModel(AcpSchema.SetSessionModelRequest setModelRequest)
This allows changing which AI model is used for subsequent prompts in the session.
setModelRequest - the set model request with session ID and desired modelAcpSchema.METHOD_SESSION_SET_MODELpublic AcpSchema.PromptResponse prompt(AcpSchema.PromptRequest promptRequest)
The prompt can contain text, images, or other content types. The agent processes the prompt and may send streaming updates via session/update notifications before returning the final response.
promptRequest - the prompt request with session ID and contentAcpSchema.METHOD_SESSION_PROMPTpublic void cancel(AcpSchema.CancelNotification cancelNotification)
This sends a notification to the agent to stop any in-progress work for the specified session. Note that this is a notification (fire-and-forget), not a request.
cancelNotification - the cancel notification with session ID and optional
reasonAcpSchema.METHOD_SESSION_CANCEL