@@ -229,6 +229,9 @@ class AsyncSpec {
229229 /** Receives the session updates only while no handler is added. */
230230 private @ Nullable Function <AcpSchema .SessionNotification , Mono <Void >> defaultSessionUpdateHandler ;
231231
232+ /** Request handlers that apply when none is registered for their method. */
233+ private final Map <String , AcpClientSession .RequestHandler <?>> defaultRequestHandlers = new HashMap <>();
234+
232235 private @ Nullable Function <AcpSchema .CreateElicitationRequest , Mono <AcpSchema .CreateElicitationResponse >> createElicitationHandler ;
233236
234237 private AsyncSpec (AcpClientTransport transport ) {
@@ -546,6 +549,27 @@ public AsyncSpec defaultSessionUpdateHandler(Function<AcpSchema.SessionNotificat
546549 return this ;
547550 }
548551
552+ /**
553+ * Sets the handler that answers {@code session/request_permission} when no
554+ * {@link #requestPermissionHandler} is registered: a default that the application's own
555+ * handler replaces, whichever is registered first. It is meant for frameworks, which set a
556+ * default (refusing every request, for instance) before handing the builder to the
557+ * application. Without either, the client answers the request {@code -32601} (method not
558+ * found). A later call replaces the default.
559+ * @param handler the default handler; it emits the answer to the agent
560+ * @return this builder
561+ * @throws IllegalArgumentException if {@code handler} is null
562+ */
563+ public AsyncSpec defaultRequestPermissionHandler (
564+ Function <AcpSchema .RequestPermissionRequest , Mono <AcpSchema .RequestPermissionResponse >> handler ) {
565+ Assert .notNull (handler , "Default request permission handler must not be null" );
566+ this .defaultRequestHandlers .put (AcpSchema .METHOD_SESSION_REQUEST_PERMISSION ,
567+ (AcpClientSession .RequestHandler <AcpSchema .RequestPermissionResponse >) params -> handler
568+ .apply (transport .unmarshalParams (params , new TypeRef <AcpSchema .RequestPermissionRequest >() {
569+ })));
570+ return this ;
571+ }
572+
549573 /**
550574 * Registers a handler for an agent-to-client request method the SDK does not model, with
551575 * the params as the transport read them: the escape hatch for a method newer than this
@@ -728,8 +752,9 @@ else if (absent.size() < TERMINAL_METHODS.size()) {
728752 /**
729753 * Fails when the advertised capabilities claim a capability without the handlers that
730754 * serve it, and warns once about handlers for capabilities not advertised.
755+ * @return the capabilities, checked
731756 */
732- private void checkCapabilitiesHaveHandlers (AcpSchema .ClientCapabilities capabilities ) {
757+ private AcpSchema . ClientCapabilities checkCapabilitiesHaveHandlers (AcpSchema .ClientCapabilities capabilities ) {
733758 NegotiatedCapabilities advertised = NegotiatedCapabilities .fromClient (capabilities );
734759 List <String > missing = new ArrayList <>();
735760 List <String > unadvertised = new ArrayList <>();
@@ -751,6 +776,7 @@ private void checkCapabilitiesHaveHandlers(AcpSchema.ClientCapabilities capabili
751776 + "call them: {}. Advertise the capabilities in clientCapabilities(..)" ,
752777 String .join ("; " , unadvertised ));
753778 }
779+ return capabilities ;
754780 }
755781
756782 private void checkCapability (String capability , boolean advertised , List <String > methods ,
@@ -800,14 +826,8 @@ private static IllegalStateException alreadyRegistered(String method, String set
800826 public AcpAsyncClient build () {
801827 // Explicit capabilities win; without them the handlers decide what is advertised
802828 AcpSchema .@ Nullable ClientCapabilities explicit = this .clientCapabilities ;
803- AcpSchema .ClientCapabilities capabilities ;
804- if (explicit != null ) {
805- checkCapabilitiesHaveHandlers (explicit );
806- capabilities = explicit ;
807- }
808- else {
809- capabilities = deriveCapabilities ();
810- }
829+ AcpSchema .ClientCapabilities capabilities = (explicit != null ) ? checkCapabilitiesHaveHandlers (explicit )
830+ : deriveCapabilities ();
811831
812832 // Set up session update notification handler: the application's handlers, or else the
813833 // default one
@@ -833,6 +853,7 @@ public AcpAsyncClient build() {
833853 // The capabilities the client advertises when it initializes
834854 AtomicReference <AcpSchema .@ Nullable ClientCapabilities > advertised = new AtomicReference <>();
835855 Map <String , AcpClientSession .RequestHandler <?>> handlers = new HashMap <>(requestHandlers );
856+ this .defaultRequestHandlers .forEach (handlers ::putIfAbsent );
836857 Function <AcpSchema .CreateElicitationRequest , Mono <AcpSchema .CreateElicitationResponse >> elicitation = this .createElicitationHandler ;
837858 if (elicitation != null ) {
838859 AcpClientSession .RequestHandler <AcpSchema .CreateElicitationResponse > rawHandler = params -> {
0 commit comments