Skip to content

Commit 6c2faee

Browse files
author
Mark Pollack
committed
acp-core: a framework default for permission requests
AcpClient.AsyncSpec.defaultRequestPermissionHandler sets the handler that answers session/request_permission when the application registers none, the way defaultSessionUpdateHandler does for session updates: the application's own handler replaces it, whichever is registered first. The framework integrations use it to refuse permission requests nobody handles. The capabilities branch of build() folds into one expression, keeping the interface under the NCSS gate.
1 parent 0cd8df3 commit 6c2faee

4 files changed

Lines changed: 135 additions & 129 deletions

File tree

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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 -> {
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2026 the original author or authors.
3+
*/
4+
5+
package com.agentclientprotocol.sdk.client;
6+
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import com.agentclientprotocol.sdk.MockAcpClientTransport;
11+
import com.agentclientprotocol.sdk.spec.AcpSchema;
12+
import org.junit.jupiter.api.Test;
13+
import reactor.core.publisher.Mono;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
17+
18+
/**
19+
* A framework sets a default permission handler, for example one that refuses; the
20+
* application's own handler replaces it, whichever is registered first.
21+
*/
22+
class DefaultRequestPermissionHandlerTest {
23+
24+
private static final AcpSchema.JSONRPCRequest REQUEST = new AcpSchema.JSONRPCRequest(AcpSchema.JSONRPC_VERSION,
25+
"p-1", AcpSchema.METHOD_SESSION_REQUEST_PERMISSION,
26+
Map.of("sessionId", "s-1", "toolCall", Map.of("toolCallId", "t-1"), "options",
27+
List.of(Map.of("optionId", "yes", "name", "Allow", "kind", "allow_once"))));
28+
29+
@Test
30+
void theDefaultAnswersWhenTheApplicationRegistersNone() {
31+
MockAcpClientTransport transport = new MockAcpClientTransport();
32+
AcpAsyncClient client = AcpClient.async(transport).defaultRequestPermissionHandler(answer("default")).build();
33+
34+
transport.simulateIncomingMessage(REQUEST);
35+
36+
assertThat(answeredWith(transport)).isEqualTo("default");
37+
client.close();
38+
}
39+
40+
@Test
41+
void theApplicationsHandlerReplacesTheDefaultInEitherOrder() {
42+
MockAcpClientTransport before = new MockAcpClientTransport();
43+
AcpAsyncClient first = AcpClient.async(before)
44+
.defaultRequestPermissionHandler(answer("default"))
45+
.requestPermissionHandler(answer("app"))
46+
.build();
47+
before.simulateIncomingMessage(REQUEST);
48+
assertThat(answeredWith(before)).isEqualTo("app");
49+
first.close();
50+
51+
MockAcpClientTransport after = new MockAcpClientTransport();
52+
AcpAsyncClient second = AcpClient.async(after)
53+
.requestPermissionHandler(answer("app"))
54+
.defaultRequestPermissionHandler(answer("default"))
55+
.build();
56+
after.simulateIncomingMessage(REQUEST);
57+
assertThat(answeredWith(after)).isEqualTo("app");
58+
second.close();
59+
}
60+
61+
@Test
62+
void aLaterDefaultReplacesAnEarlierOne() {
63+
MockAcpClientTransport transport = new MockAcpClientTransport();
64+
AcpAsyncClient client = AcpClient.async(transport)
65+
.defaultRequestPermissionHandler(answer("first"))
66+
.defaultRequestPermissionHandler(answer("second"))
67+
.build();
68+
69+
transport.simulateIncomingMessage(REQUEST);
70+
71+
assertThat(answeredWith(transport)).isEqualTo("second");
72+
assertThatThrownBy(() -> AcpClient.async(new MockAcpClientTransport()).defaultRequestPermissionHandler(null))
73+
.isInstanceOf(IllegalArgumentException.class);
74+
client.close();
75+
}
76+
77+
private static java.util.function.Function<AcpSchema.RequestPermissionRequest, Mono<AcpSchema.RequestPermissionResponse>> answer(
78+
String optionId) {
79+
return request -> Mono
80+
.just(new AcpSchema.RequestPermissionResponse(new AcpSchema.PermissionSelected(optionId)));
81+
}
82+
83+
/** The option id of the answer to {@code p-1}, waiting up to five seconds for it. */
84+
private static String answeredWith(MockAcpClientTransport transport) {
85+
long deadline = System.nanoTime() + 5_000_000_000L;
86+
while (System.nanoTime() < deadline) {
87+
for (AcpSchema.JSONRPCMessage message : transport.getSentMessages()) {
88+
if (message instanceof AcpSchema.JSONRPCResponse response && "p-1".equals(response.id())
89+
&& response.result() instanceof AcpSchema.RequestPermissionResponse answer
90+
&& answer.outcome() instanceof AcpSchema.PermissionSelected selected) {
91+
return selected.optionId();
92+
}
93+
}
94+
try {
95+
Thread.sleep(20);
96+
}
97+
catch (InterruptedException e) {
98+
Thread.currentThread().interrupt();
99+
break;
100+
}
101+
}
102+
throw new AssertionError("no answer to p-1 in " + transport.getSentMessages());
103+
}
104+
105+
}

‎acp-test/src/main/java/com/agentclientprotocol/sdk/test/InMemoryTransportPair.java‎

Lines changed: 0 additions & 63 deletions
This file was deleted.

‎acp-test/src/test/java/com/agentclientprotocol/sdk/test/InMemoryTransportPairTest.java‎

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)