public class AcpAsyncClient
extends java.lang.Object
This client implements the ACP specification, enabling applications to interact with autonomous coding agents through a standardized interface. Key features include:
The client follows a lifecycle:
This implementation uses Project Reactor for non-blocking operations, making it suitable for high-throughput scenarios and reactive applications. All operations return Mono types that can be composed into reactive pipelines.
Example usage:
// Create transport
AgentParameters params = AgentParameters.builder("gemini")
.arg("--experimental-acp")
.build();
StdioAcpClientTransport transport = new StdioAcpClientTransport(params, McpJsonMapper.getDefault());
// Create client
AcpAsyncClient client = AcpClient.async(transport)
.requestTimeout(Duration.ofSeconds(30))
.build();
// Initialize
AcpSchema.InitializeResponse initResponse = client
.initialize(new AcpSchema.InitializeRequest(1, new AcpSchema.ClientCapabilities()))
.block();
// Create session and interact
String sessionId = client
.newSession(new AcpSchema.NewSessionRequest("/workspace", List.of()))
.map(AcpSchema.NewSessionResponse::sessionId)
.block();
AcpSchema.PromptResponse response = client
.prompt(new AcpSchema.PromptRequest(sessionId, List.of(new AcpSchema.TextContent("Fix the bug"))))
.block();
client.closeGracefully().block();
AcpSession,
AcpSchema| 限定符和类型 | 方法和说明 |
|---|---|
reactor.core.publisher.Mono<AcpSchema.AuthenticateResponse> |
authenticate(AcpSchema.AuthenticateRequest authenticateRequest)
Authenticates with the agent using the specified authentication method.
|
reactor.core.publisher.Mono<java.lang.Void> |
cancel(AcpSchema.CancelNotification cancelNotification)
Cancels ongoing operations for a session.
|
void |
close()
Closes the client connection immediately.
|
reactor.core.publisher.Mono<java.lang.Void> |
closeGracefully()
Gracefully closes the client connection, allowing pending operations to complete.
|
NegotiatedCapabilities |
getAgentCapabilities()
Returns the capabilities negotiated with the agent during initialization.
|
reactor.core.publisher.Mono<AcpSchema.InitializeResponse> |
initialize()
Initializes the ACP client with default settings.
|
reactor.core.publisher.Mono<AcpSchema.InitializeResponse> |
initialize(AcpSchema.InitializeRequest initializeRequest)
Initializes the connection with the agent.
|
reactor.core.publisher.Mono<AcpSchema.LoadSessionResponse> |
loadSession(AcpSchema.LoadSessionRequest loadSessionRequest)
Loads an existing agent session by ID.
|
reactor.core.publisher.Mono<AcpSchema.NewSessionResponse> |
newSession(AcpSchema.NewSessionRequest newSessionRequest)
Creates a new agent session with the specified working directory.
|
reactor.core.publisher.Mono<AcpSchema.PromptResponse> |
prompt(AcpSchema.PromptRequest promptRequest)
Sends a prompt to the agent within a session.
|
reactor.core.publisher.Mono<AcpSchema.SetSessionModeResponse> |
setSessionMode(AcpSchema.SetSessionModeRequest setModeRequest)
Sets the operational mode for a session (e.g., "code", "plan", "review").
|
reactor.core.publisher.Mono<AcpSchema.SetSessionModelResponse> |
setSessionModel(AcpSchema.SetSessionModelRequest setModelRequest)
Sets the AI model for the specified session.
|
public reactor.core.publisher.Mono<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.
After initialization, the agent's capabilities can be accessed via
getAgentCapabilities().
initializeRequest - the initialization request containing protocol version and
client capabilitiesAcpSchema.METHOD_INITIALIZE,
getAgentCapabilities()public reactor.core.publisher.Mono<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 reactor.core.publisher.Mono<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 reactor.core.publisher.Mono<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 reactor.core.publisher.Mono<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 reactor.core.publisher.Mono<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 reactor.core.publisher.Mono<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 reactor.core.publisher.Mono<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 reactor.core.publisher.Mono<java.lang.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_CANCELpublic void close()
This closes both the session and the underlying transport.
public reactor.core.publisher.Mono<java.lang.Void> closeGracefully()
This closes both the session and the underlying transport.