Skip to content

Commit c2c0522

Browse files
author
Mark Pollack
committed
Elicitation is stable: typed elicitation/complete handler and mode checks
The ACP schema promoted elicitation to stable (protocol v1.7.0, agent-client-protocol PR 1779), so its records lose @UnstableAcpApi. - Clients get completeElicitationHandler on the async and sync builders, so elicitation/complete reaches a typed handler. - The capability's form and url modes are typed records, as in the schema; the no-argument constructor, which advertised form mode although {} advertises none, gives way to formOnly(), urlOnly() and formAndUrl(). - The agent checks the requested mode against the client's capability, and a client answers a mode it did not advertise with -32602. - _meta on the schema, property, item and option records, description on EnumOption, accept() without content, and createElicitation and completeElicitation on AcpSyncAgent. - ElicitationAction becomes an open value, and form properties and multi-select items of an unknown kind read as Unknown* records, following the forward-compatibility rule for the schema's unions.
1 parent 1e0a7d6 commit c2c0522

13 files changed

Lines changed: 981 additions & 117 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
108108
before `_meta`), which instead gains `ToolCallUpdate(toolCallId, title, kind, status)` for the
109109
common permission-request case (migration: use it, or add `null` for `_meta`). A test walks a copy of the v1.9.1 schema
110110
(`acp-core/src/test/resources/schema/v1/schema.json`) and fails on any object with `_meta`
111-
whose record lacks it. Not covered yet: the elicitation records (with the elicitation work), the
111+
whose record lacks it; the elicitation records are covered too (below). Not covered yet: the
112112
presence markers typed `Object` (they keep `_meta` as a map entry). `CancelRequestNotification`
113113
carries `_meta` (with `$/cancel_request`, below).
114114

115+
- **`elicitation/complete` reaches a typed client handler:** `AcpClient.async(...)
116+
.completeElicitationHandler(Function<CompleteElicitationNotification, Mono<Void>>)` and
117+
`AcpClient.sync(...).completeElicitationHandler(Consumer<CompleteElicitationNotification>)`. Before,
118+
the agent could send the notification but a client could only read it as raw params through
119+
`notificationHandler`. The spec requires clients to ignore unknown or already-completed
120+
elicitation IDs; the handler sees every notification and does that check.
121+
- `AcpSyncAgent.createElicitation` and `AcpSyncAgent.completeElicitation`, matching the async agent.
122+
- `CreateElicitationResponse.accept()` without content (for an accepted URL elicitation), and the
123+
mode constants `CreateElicitationRequest.MODE_FORM` and `MODE_URL`.
124+
- The elicitation records carry every field of the stable schema: `_meta` on `ElicitationSchema`,
125+
the five property schemas, both multi-select item types and `EnumOption`, and `description` on
126+
`EnumOption`. The constructors without them remain, except for the two that would take seven or
127+
more arguments.
128+
115129
### Changed
116130

117131
- **Breaking: error codes follow the ACP v1 schema (`$defs.ErrorCode`).** A prompt sent while the
@@ -130,6 +144,43 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
130144
(`-32005`) are removed: the SDK made them up inside the range ACP reserves for itself.
131145
`AcpCapabilityException.toProtocolException()` now answers `-32600` with the capability name as
132146
data.
147+
- **Elicitation is stable (the ACP schema promoted it on 2026-07-24, in protocol v1.7.0), and its
148+
API is no longer `@UnstableAcpApi`:** `CreateElicitationRequest`, `CreateElicitationResponse`,
149+
`ElicitationAction`, `CompleteElicitationNotification`, `ElicitationSchema`, the property schemas,
150+
the multi-select items, `EnumOption`, `ElicitationCapabilities` and
151+
`ClientCapabilities.elicitation`.
152+
- **Breaking: the elicitation capability's modes are typed, and the no-argument
153+
`ElicitationCapabilities()` constructor is removed.** `form` and `url` were `Object`; they are now
154+
`ElicitationFormCapabilities` and `ElicitationUrlCapabilities` (each with `_meta`), as in the
155+
schema. The removed constructor advertised form mode, although the spec says an empty capability
156+
(`{}`) advertises no mode. Migration: `new ElicitationCapabilities()` becomes
157+
`ElicitationCapabilities.formOnly()`; `urlOnly()` and `formAndUrl()` advertise the other modes, and
158+
code that read `form()` or `url()` as `Object` gets the typed records.
159+
- **Breaking: `ElicitationAction` is an open value, not an enum,** by the rule for the schema's
160+
closed string unions (see the `AcpSchema` Javadoc): a record over the wire string with the
161+
constants `ACCEPT`, `DECLINE` and `CANCEL`, `of(String)`, `known()` and `isKnown()`. An action
162+
this SDK does not know (a newer peer) is kept and written back instead of failing the response.
163+
Migration: compare with `equals`, not `==`, and replace a `switch` over the enum with `if`/`else`
164+
or a switch over `action().value()`.
165+
- **Elicitation form properties of an unknown type and multi-select items of an unknown shape no
166+
longer fail the request:** they read as `UnknownElicitationPropertySchema` (keeping `type` and
167+
every other field) and `UnknownMultiSelectItems`, and are written back unchanged, as for the
168+
other discriminated unions. The five property schemas now write their own `type` discriminator:
169+
their canonical constructors accept `null` for it (it becomes the record's type) and reject a
170+
different type, which they used to accept and ignore.
171+
- **Breaking: `StringPropertySchema` and `MultiSelectPropertySchema` have only their canonical
172+
constructors,** which end with `_meta`. Migration: pass `null` as the last argument, or use
173+
`StringPropertySchema.text(...)` and `singleSelect(...)`.
174+
- **Behaviour change: the agent checks the requested elicitation mode, not only elicitation.**
175+
`createElicitation` fails with `AcpCapabilityException` (`elicitation.form` or `elicitation.url`)
176+
when the client did not advertise the request's mode; before, any `elicitation` object, even `{}`,
177+
let both modes through. The spec says agents must not request a mode the client did not advertise.
178+
A mode the SDK does not know still needs only `elicitation`.
179+
- **Behaviour change: a client with a typed `createElicitationHandler` answers a request for a mode
180+
it did not advertise at initialization with `-32602` (invalid params)**, as the spec says, without
181+
calling the handler. Before initialization, and for a mode the SDK does not know, the handler is
182+
called. Migration: advertise the modes the handler supports in `clientCapabilities.elicitation`.
183+
A raw `requestHandler("elicitation/create", ...)` is not checked.
133184

134185
- **Unknown union variants no longer fail the message** (ACP forward compatibility). A newer agent's
135186
`sessionUpdate` type used to fail the whole `session/update` notification, which the client then

‎acp-core/src/main/java/com/agentclientprotocol/sdk/agent/AcpSyncAgent.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,25 @@ public AcpSchema.KillTerminalCommandResponse killTerminal(AcpSchema.KillTerminal
171171
return SyncBlocking.awaitResponse(asyncAgent.killTerminal(request), blockTimeout);
172172
}
173173

174+
/**
175+
* Requests structured user input from the client via a form or URL. The client must
176+
* have advertised the request's mode.
177+
* @param request The elicitation request
178+
* @return The user's response
179+
*/
180+
public AcpSchema.CreateElicitationResponse createElicitation(AcpSchema.CreateElicitationRequest request) {
181+
return SyncBlocking.awaitResponse(asyncAgent.createElicitation(request), blockTimeout);
182+
}
183+
184+
/**
185+
* Notifies the client that the external interaction of a URL-mode elicitation has
186+
* completed.
187+
* @param notification The completion notification
188+
*/
189+
public void completeElicitation(AcpSchema.CompleteElicitationNotification notification) {
190+
asyncAgent.completeElicitation(notification).block(blockTimeout);
191+
}
192+
174193
/**
175194
* Returns the capabilities negotiated with the client during initialization.
176195
*

‎acp-core/src/main/java/com/agentclientprotocol/sdk/agent/DefaultAcpAsyncAgent.java‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,21 @@ public Mono<AcpSchema.KillTerminalCommandResponse> killTerminal(AcpSchema.KillTe
184184
@Override
185185
public Mono<AcpSchema.CreateElicitationResponse> createElicitation(
186186
AcpSchema.CreateElicitationRequest request) {
187-
return sendRequest(AcpSchema.METHOD_ELICITATION_CREATE, request,
188-
new TypeRef<AcpSchema.CreateElicitationResponse>() {
189-
}, NegotiatedCapabilities::supportsElicitation, "elicitation");
187+
return switch (request.mode()) {
188+
case AcpSchema.CreateElicitationRequest.MODE_FORM -> sendRequest(AcpSchema.METHOD_ELICITATION_CREATE,
189+
request, ELICITATION_RESPONSE, NegotiatedCapabilities::supportsElicitationForm, "elicitation.form");
190+
case AcpSchema.CreateElicitationRequest.MODE_URL -> sendRequest(AcpSchema.METHOD_ELICITATION_CREATE,
191+
request, ELICITATION_RESPONSE, NegotiatedCapabilities::supportsElicitationUrl, "elicitation.url");
192+
// A mode this SDK does not know: the client can only have advertised it in a
193+
// capability this SDK does not model, so only elicitation itself is checked.
194+
default -> sendRequest(AcpSchema.METHOD_ELICITATION_CREATE, request, ELICITATION_RESPONSE,
195+
NegotiatedCapabilities::supportsElicitation, "elicitation");
196+
};
190197
}
191198

199+
private static final TypeRef<AcpSchema.CreateElicitationResponse> ELICITATION_RESPONSE = new TypeRef<>() {
200+
};
201+
192202
@Override
193203
public Mono<Void> completeElicitation(AcpSchema.CompleteElicitationNotification notification) {
194204
return sendNotification(AcpSchema.METHOD_ELICITATION_COMPLETE, notification);

‎acp-core/src/main/java/com/agentclientprotocol/sdk/client/AcpAsyncClient.java‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ public class AcpAsyncClient {
156156
*/
157157
private final AtomicReference<@Nullable NegotiatedCapabilities> agentCapabilities = new AtomicReference<>();
158158

159+
/**
160+
* The capabilities the client advertised in its last initialize request.
161+
*/
162+
private final AtomicReference<AcpSchema.@Nullable ClientCapabilities> advertisedCapabilities;
163+
159164
/**
160165
* Creates a new AcpAsyncClient with the given session and transport. Uses default
161166
* client capabilities.
@@ -176,11 +181,28 @@ public class AcpAsyncClient {
176181
*/
177182
AcpAsyncClient(AcpSession session, AcpClientTransport transport,
178183
AcpSchema.@Nullable ClientCapabilities clientCapabilities) {
184+
this(session, transport, clientCapabilities, new AtomicReference<>());
185+
}
186+
187+
/**
188+
* Creates a new AcpAsyncClient that records the capabilities it advertises when it
189+
* initializes, so its handlers can honour them.
190+
* @param session the ACP session for communication
191+
* @param transport the transport layer for this client
192+
* @param clientCapabilities the client capabilities to use during initialization (may
193+
* be null for defaults)
194+
* @param advertisedCapabilities set to the capabilities of each initialize request
195+
* the client sends
196+
*/
197+
AcpAsyncClient(AcpSession session, AcpClientTransport transport,
198+
AcpSchema.@Nullable ClientCapabilities clientCapabilities,
199+
AtomicReference<AcpSchema.@Nullable ClientCapabilities> advertisedCapabilities) {
179200
Assert.notNull(session, "Session must not be null");
180201
Assert.notNull(transport, "Transport must not be null");
181202
this.session = session;
182203
this.transport = transport;
183204
this.clientCapabilities = clientCapabilities != null ? clientCapabilities : new AcpSchema.ClientCapabilities();
205+
this.advertisedCapabilities = advertisedCapabilities;
184206
}
185207

186208
// --------------------------
@@ -209,7 +231,12 @@ public class AcpAsyncClient {
209231
public Mono<AcpSchema.InitializeResponse> initialize(AcpSchema.InitializeRequest initializeRequest) {
210232
Assert.notNull(initializeRequest, "Initialize request must not be null");
211233
logger.debug("Initializing ACP client with protocol version: {}", initializeRequest.protocolVersion());
212-
return session.sendRequest(AcpSchema.METHOD_INITIALIZE, initializeRequest, INITIALIZE_RESPONSE_TYPE_REF)
234+
AcpSchema.ClientCapabilities advertised = initializeRequest.clientCapabilities();
235+
return Mono
236+
.fromRunnable(() -> advertisedCapabilities
237+
.set(advertised != null ? advertised : new AcpSchema.ClientCapabilities(null, null)))
238+
.then(Mono.defer(
239+
() -> session.sendRequest(AcpSchema.METHOD_INITIALIZE, initializeRequest, INITIALIZE_RESPONSE_TYPE_REF)))
213240
.doOnNext(response -> {
214241
// Store the negotiated agent capabilities
215242
NegotiatedCapabilities caps = NegotiatedCapabilities.fromAgent(response.agentCapabilities());

‎acp-core/src/main/java/com/agentclientprotocol/sdk/client/AcpClient.java‎

Lines changed: 102 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
import java.util.concurrent.Callable;
1616
import java.util.concurrent.Executors;
17+
import java.util.concurrent.atomic.AtomicReference;
1718

19+
import com.agentclientprotocol.sdk.error.AcpErrorCodes;
20+
import com.agentclientprotocol.sdk.error.AcpProtocolException;
1821
import com.agentclientprotocol.sdk.spec.AcpClientSession;
1922
import com.agentclientprotocol.sdk.spec.AcpClientTransport;
2023
import com.agentclientprotocol.sdk.spec.AcpSchema;
@@ -199,6 +202,8 @@ class AsyncSpec {
199202

200203
private final List<Function<AcpSchema.SessionNotification, Mono<Void>>> sessionUpdateConsumers = new ArrayList<>();
201204

205+
private @Nullable Function<AcpSchema.CreateElicitationRequest, Mono<AcpSchema.CreateElicitationResponse>> createElicitationHandler;
206+
202207
private AsyncSpec(AcpClientTransport transport) {
203208
Assert.notNull(transport, "Transport must not be null");
204209
this.transport = transport;
@@ -415,8 +420,16 @@ public AsyncSpec killTerminalHandler(
415420
}
416421

417422
/**
418-
* Adds a typed handler for elicitation requests from the agent.
419-
* The agent uses elicitation to request structured user input via forms.
423+
* Adds a typed handler for {@code elicitation/create} requests from the agent,
424+
* which ask the user for structured input through a form or a URL. The handler
425+
* answers {@code accept}, {@code decline} or {@code cancel}.
426+
*
427+
* <p>
428+
* The client answers a request whose mode ({@code form} or {@code url}) it did
429+
* not advertise in {@code clientCapabilities.elicitation} at initialization with
430+
* a JSON-RPC {@code -32602} (invalid params) error, without calling the handler.
431+
* Advertise the modes the handler supports, for example with
432+
* {@link AcpSchema.ElicitationCapabilities#formOnly()}.
420433
*
421434
* @param handler The typed handler function that processes elicitation requests
422435
* @return This builder instance for method chaining
@@ -425,8 +438,30 @@ public AsyncSpec killTerminalHandler(
425438
public AsyncSpec createElicitationHandler(
426439
Function<AcpSchema.CreateElicitationRequest, Mono<AcpSchema.CreateElicitationResponse>> handler) {
427440
Assert.notNull(handler, "Create elicitation handler must not be null");
428-
return request(AcpSchema.METHOD_ELICITATION_CREATE, new TypeRef<AcpSchema.CreateElicitationRequest>() {
429-
}, handler);
441+
this.requestHandlers.remove(AcpSchema.METHOD_ELICITATION_CREATE);
442+
this.createElicitationHandler = handler;
443+
return this;
444+
}
445+
446+
/**
447+
* Adds a typed handler for {@code elicitation/complete} notifications: the agent
448+
* reports that the external interaction of a URL-mode elicitation has finished.
449+
* The spec requires clients to ignore unknown or already-completed elicitation
450+
* IDs, so the handler should check the ID against the URL elicitations it
451+
* accepted.
452+
*
453+
* @param handler The typed handler function that processes completion
454+
* notifications
455+
* @return This builder instance for method chaining
456+
* @throws IllegalArgumentException if handler is null
457+
*/
458+
public AsyncSpec completeElicitationHandler(
459+
Function<AcpSchema.CompleteElicitationNotification, Mono<Void>> handler) {
460+
Assert.notNull(handler, "Complete elicitation handler must not be null");
461+
this.notificationHandlers.put(AcpSchema.METHOD_ELICITATION_COMPLETE, params -> handler
462+
.apply(transport.unmarshalFrom(params, new TypeRef<AcpSchema.CompleteElicitationNotification>() {
463+
})));
464+
return this;
430465
}
431466

432467
/**
@@ -457,6 +492,9 @@ public AsyncSpec sessionUpdateConsumer(
457492
public AsyncSpec requestHandler(String method, AcpClientSession.RequestHandler<?> handler) {
458493
Assert.notNull(method, "Method must not be null");
459494
Assert.notNull(handler, "Handler must not be null");
495+
if (AcpSchema.METHOD_ELICITATION_CREATE.equals(method)) {
496+
this.createElicitationHandler = null;
497+
}
460498
this.requestHandlers.put(method, handler);
461499
return this;
462500
}
@@ -504,11 +542,46 @@ public AcpAsyncClient build() {
504542
});
505543
}
506544

545+
// The capabilities the client advertises when it initializes
546+
AtomicReference<AcpSchema.@Nullable ClientCapabilities> advertised = new AtomicReference<>();
547+
Map<String, AcpClientSession.RequestHandler<?>> handlers = new HashMap<>(requestHandlers);
548+
Function<AcpSchema.CreateElicitationRequest, Mono<AcpSchema.CreateElicitationResponse>> elicitation = this.createElicitationHandler;
549+
if (elicitation != null) {
550+
AcpClientSession.RequestHandler<AcpSchema.CreateElicitationResponse> rawHandler = params -> {
551+
AcpSchema.CreateElicitationRequest request = transport.unmarshalFrom(params,
552+
new TypeRef<AcpSchema.CreateElicitationRequest>() {
553+
});
554+
if (!advertisesMode(advertised.get(), request.mode())) {
555+
return Mono.error(new AcpProtocolException(AcpErrorCodes.INVALID_PARAMS,
556+
"Elicitation mode '" + request.mode() + "' was not advertised by the client"));
557+
}
558+
return elicitation.apply(request);
559+
};
560+
handlers.put(AcpSchema.METHOD_ELICITATION_CREATE, rawHandler);
561+
}
562+
507563
// Create session with request and notification handlers
508-
AcpSession session = new AcpClientSession(requestTimeout, transport, requestHandlers, notificationHandlers,
509-
Function.identity());
564+
AcpSession session = new AcpClientSession(requestTimeout, transport, handlers,
565+
new HashMap<>(notificationHandlers), Function.identity());
510566

511-
return new AcpAsyncClient(session, transport, clientCapabilities);
567+
return new AcpAsyncClient(session, transport, clientCapabilities, advertised);
568+
}
569+
570+
/**
571+
* Whether the advertised capabilities cover an elicitation mode. Before the
572+
* client initializes nothing is known and every mode passes; a mode this SDK does
573+
* not know passes too, since only the handler can judge it.
574+
*/
575+
private static boolean advertisesMode(AcpSchema.@Nullable ClientCapabilities advertised, String mode) {
576+
if (advertised == null) {
577+
return true;
578+
}
579+
AcpSchema.ElicitationCapabilities elicitation = advertised.elicitation();
580+
return switch (mode) {
581+
case AcpSchema.CreateElicitationRequest.MODE_FORM -> elicitation != null && elicitation.form() != null;
582+
case AcpSchema.CreateElicitationRequest.MODE_URL -> elicitation != null && elicitation.url() != null;
583+
default -> true;
584+
};
512585
}
513586

514587
}
@@ -762,8 +835,10 @@ public SyncSpec killTerminalHandler(
762835
}
763836

764837
/**
765-
* Adds a typed handler for elicitation requests from the agent.
766-
* The agent uses elicitation to request structured user input via forms.
838+
* Adds a typed handler for {@code elicitation/create} requests from the agent.
839+
* See {@link AsyncSpec#createElicitationHandler(Function)}: a request for a mode
840+
* the client did not advertise is answered with {@code -32602} without calling
841+
* the handler.
767842
*
768843
* @param handler The typed handler function that processes elicitation requests
769844
* @return This builder instance for method chaining
@@ -776,6 +851,24 @@ public SyncSpec createElicitationHandler(
776851
return this;
777852
}
778853

854+
/**
855+
* Adds a synchronous handler for {@code elicitation/complete} notifications: the
856+
* agent reports that the external interaction of a URL-mode elicitation has
857+
* finished. Clients must ignore unknown or already-completed elicitation IDs.
858+
*
859+
* @param handler The handler that processes completion notifications
860+
* @return This builder instance for method chaining
861+
* @throws IllegalArgumentException if handler is null
862+
*/
863+
public SyncSpec completeElicitationHandler(Consumer<AcpSchema.CompleteElicitationNotification> handler) {
864+
Assert.notNull(handler, "Complete elicitation handler must not be null");
865+
asyncSpec.completeElicitationHandler(notification -> Mono
866+
.fromRunnable(() -> handler.accept(notification))
867+
.subscribeOn(SYNC_HANDLER_SCHEDULER)
868+
.then());
869+
return this;
870+
}
871+
779872
/**
780873
* Adds a synchronous consumer to be notified when session update notifications
781874
* are received from the agent. This is the preferred method for sync clients.

0 commit comments

Comments
 (0)