1414
1515import java .util .concurrent .Callable ;
1616import 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 ;
1821import com .agentclientprotocol .sdk .spec .AcpClientSession ;
1922import com .agentclientprotocol .sdk .spec .AcpClientTransport ;
2023import 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