Skip to content

Commit 184130d

Browse files
committed
feat(v2): align extension methods with v1 and declare protocol hooks
1 parent f0f546e commit 184130d

6 files changed

Lines changed: 83 additions & 20 deletions

File tree

‎docs/experimental-v2.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ Each connection invokes the selected factory once and runs strictly under the ne
8383

8484
## Extensions, cancellation, and MCP-over-ACP
8585

86-
- Call `send_extension_request("_vendor/method", ...)` and `send_extension_notification("_vendor/event", ...)` for vendor extensions.
86+
- Call `ext_method("_vendor/method", ...)` and `ext_notification("_vendor/event", ...)` on connections, and implement `ext_method`, `ext_notification`, or `on_connect` on `v2.Agent` / `v2.Client` for vendor extensions and connection setup.
8787
- `$/cancel_request` cancels running handler tasks and outgoing requests automatically; use `cancel_session(session_id=...)` for active session work.
8888
- [Stateless MCP-over-ACP](protocol-features.md#stateless-mcp-over-acp) uses `v2.schema.McpResultResponse`, `McpErrorResponse`, and `MessageMcpResponse` when the agent advertises `capabilities.session.mcp.acp = {}`.

‎src/acp/experimental/v2/_router.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ async def __call__(self, method: str, params: Any | None, is_notification: bool)
7575
return await self.handle_request(spec, params)
7676

7777
async def _handle_extension(self, method: str, params: Any, is_notification: bool) -> Any:
78-
handler_name = "handle_extension_notification" if is_notification else "handle_extension_request"
79-
handler = getattr(self._target, handler_name, None)
78+
handler_name = "ext_notification" if is_notification else "ext_method"
79+
handler = self._handler(handler_name)
8080
if handler is None:
8181
if is_notification:
8282
return None

‎src/acp/experimental/v2/agent.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ async def auth_status_update(
195195
build_request(schema.AuthStatusUpdateNotification, {"state": state, "accounts": accounts}, kwargs),
196196
)
197197

198-
async def send_extension_request(self, method: str, params: Any = None) -> Any:
198+
async def ext_method(self, method: str, params: Any = None) -> Any:
199199
await self._state.require(method)
200200
return await self._conn.send_request(_extension_method(method), params)
201201

202-
async def send_extension_notification(self, method: str, params: Any = None) -> None:
202+
async def ext_notification(self, method: str, params: Any = None) -> None:
203203
await self._state.require(method)
204204
await self._conn.send_notification(_extension_method(method), params)
205205

‎src/acp/experimental/v2/client.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,11 @@ async def cancel_terminal(self, session_id: str, terminal_id: str, **kwargs: Any
347347
build_request(schema.CancelTerminalRequest, {"session_id": session_id, "terminal_id": terminal_id}, kwargs),
348348
)
349349

350-
async def send_extension_request(self, method: str, params: Any = None) -> Any:
350+
async def ext_method(self, method: str, params: Any = None) -> Any:
351351
await self._state.require(method)
352352
return await self._conn.send_request(_extension_method(method), params)
353353

354-
async def send_extension_notification(self, method: str, params: Any = None) -> None:
354+
async def ext_notification(self, method: str, params: Any = None) -> None:
355355
await self._state.require(method)
356356
await self._conn.send_notification(_extension_method(method), params)
357357

‎src/acp/experimental/v2/interfaces.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from functools import partial
4-
from typing import Any, Literal, Protocol
4+
from typing import TYPE_CHECKING, Any, Literal, Protocol
55

66
from pydantic import AnyUrl, TypeAdapter
77

@@ -11,6 +11,10 @@
1111
from . import schema
1212
from .meta import AGENT_METHODS, CLIENT_METHODS
1313

14+
if TYPE_CHECKING:
15+
from .agent import AgentSideConnection
16+
from .client import ClientSideConnection
17+
1418
SetConfigOptionRequest = schema.SetSessionConfigOptionRequest
1519
CreateElicitationRequest = schema.CreateElicitationRequest
1620
CreateElicitationResponse = schema.CreateElicitationResponse
@@ -173,6 +177,12 @@ async def cancel_terminal(
173177
self, session_id: str, terminal_id: str, **kwargs: Any
174178
) -> schema.CancelTerminalResponse: ...
175179

180+
async def ext_method(self, method: str, params: Any) -> Any: ...
181+
182+
async def ext_notification(self, method: str, params: Any) -> None: ...
183+
184+
def on_connect(self, conn: AgentSideConnection) -> None: ...
185+
176186

177187
class Client(Protocol):
178188
"""Client handlers for ACP v2; only override the methods you support."""
@@ -241,3 +251,9 @@ async def auth_status_update(
241251
accounts: list[schema.AuthAccount] | None = None,
242252
**kwargs: Any,
243253
) -> None: ...
254+
255+
async def ext_method(self, method: str, params: Any) -> Any: ...
256+
257+
async def ext_notification(self, method: str, params: Any) -> None: ...
258+
259+
def on_connect(self, conn: ClientSideConnection) -> None: ...

‎tests/test_v2_runtime.py‎

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import socket
66
from typing import Any
7+
from unittest.mock import patch
78

89
import pytest
910
from pydantic import ValidationError
@@ -126,15 +127,27 @@ async def echo_prompt(self, request: v2.schema.PromptRequest) -> None:
126127
)
127128

128129

129-
class ExtensionClient:
130-
async def handle_extension_request(self, method: str, params: Any) -> Any:
130+
class ExtensionClient(v2.Client):
131+
def __init__(self) -> None:
132+
self.notifications: asyncio.Queue[tuple[str, Any]] = asyncio.Queue()
133+
134+
def on_connect(self, conn: v2.ClientSideConnection) -> None:
135+
self.connection = conn
136+
137+
async def ext_method(self, method: str, params: Any) -> Any:
131138
return {"method": method, "params": params}
132139

140+
async def ext_notification(self, method: str, params: Any) -> None:
141+
await self.notifications.put((method, params))
142+
133143

134-
class ExtensionAgent:
144+
class ExtensionAgent(v2.Agent):
135145
def __init__(self) -> None:
136146
self.notifications: asyncio.Queue[tuple[str, Any]] = asyncio.Queue()
137147

148+
def on_connect(self, conn: v2.AgentSideConnection) -> None:
149+
self.connection = conn
150+
138151
async def initialize(
139152
self, protocol_version: int, info: v2.schema.Implementation, **kwargs: Any
140153
) -> v2.schema.InitializeResponse:
@@ -143,9 +156,12 @@ async def initialize(
143156
info=v2.schema.Implementation(name="extension-agent", version="1.0.0"),
144157
)
145158

146-
async def handle_extension_request(self, method: str, params: Any) -> Any:
159+
async def ext_method(self, method: str, params: Any) -> Any:
147160
return {"method": method, "params": params}
148161

162+
async def ext_notification(self, method: str, params: Any) -> None:
163+
await self.notifications.put((method, params))
164+
149165
async def cancel_session(self, session_id: str, **kwargs: Any) -> None:
150166
notification = v2.schema.CancelSessionNotification(session_id=session_id, **kwargs)
151167
await self.notifications.put(("cancel", notification))
@@ -417,24 +433,32 @@ def test_v2_public_entry_point_is_explicit() -> None:
417433
async def test_extension_and_notification_names_are_explicit() -> None:
418434
client_transport, agent_transport = memory_transport_pair()
419435
agent = ExtensionAgent()
436+
client = ExtensionClient()
420437
agent_connection = v2.AgentSideConnection(agent, agent_transport)
421-
client_connection = v2.ClientSideConnection(ExtensionClient(), client_transport)
438+
client_connection = v2.ClientSideConnection(client, client_transport)
422439

423440
try:
441+
assert agent.connection is agent_connection
442+
assert client.connection is client_connection
424443
await client_connection.initialize(
425444
protocol_version=v2.PROTOCOL_VERSION, info=v2.schema.Implementation(name="test-client", version="1.0.0")
426445
)
427446

428-
assert await client_connection.send_extension_request("_vendor/do", {"value": 1}) == {
447+
assert await client_connection.ext_method("_vendor/do", {"value": 1}) == {
429448
"method": "_vendor/do",
430449
"params": {"value": 1},
431450
}
432-
assert await agent_connection.send_extension_request("_vendor/read", {"value": 2}) == {
451+
assert await agent_connection.ext_method("_vendor/read", {"value": 2}) == {
433452
"method": "_vendor/read",
434453
"params": {"value": 2},
435454
}
436455
with pytest.raises(ValueError, match="must start with '_'"):
437-
await client_connection.send_extension_request("vendor/do")
456+
await client_connection.ext_method("vendor/do")
457+
458+
await client_connection.ext_notification("_vendor/event", {"value": 1})
459+
await agent_connection.ext_notification("_vendor/event", {"value": 2})
460+
assert await asyncio.wait_for(agent.notifications.get(), timeout=1) == ("_vendor/event", {"value": 1})
461+
assert await asyncio.wait_for(client.notifications.get(), timeout=1) == ("_vendor/event", {"value": 2})
438462

439463
await client_connection.cancel_session(session_id="session-1")
440464
await client_connection.notify_mcp(server_id="mcp-1", request_id="r", method="notifications/progress")
@@ -448,6 +472,29 @@ async def test_extension_and_notification_names_are_explicit() -> None:
448472
await agent_connection.close()
449473

450474

475+
@pytest.mark.asyncio
476+
async def test_inherited_extension_hooks_are_unimplemented() -> None:
477+
class StubAgent(v2.Agent):
478+
async def initialize(
479+
self, protocol_version: int, info: v2.schema.Implementation, **kwargs: Any
480+
) -> v2.schema.InitializeResponse:
481+
return v2.schema.InitializeResponse(protocol_version=protocol_version, info=info)
482+
483+
client_transport, agent_transport = memory_transport_pair()
484+
async with (
485+
v2.AgentSideConnection(StubAgent(), agent_transport),
486+
v2.ClientSideConnection(Client(), client_transport) as client_connection,
487+
):
488+
request = initialize_request()
489+
await client_connection.initialize(protocol_version=request.protocol_version, info=request.info)
490+
491+
with patch.object(v2.Agent, "ext_notification", autospec=True) as notification:
492+
await client_connection.ext_notification("_vendor/event")
493+
with pytest.raises(RequestError, check=lambda error: error.code == -32601):
494+
await client_connection.ext_method("_vendor/ping")
495+
notification.assert_not_called()
496+
497+
451498
@pytest.mark.asyncio
452499
async def test_unhandled_notifications_are_ignored(caplog: pytest.LogCaptureFixture) -> None:
453500
client_transport, agent_transport = memory_transport_pair()
@@ -459,7 +506,7 @@ async def test_unhandled_notifications_are_ignored(caplog: pytest.LogCaptureFixt
459506
protocol_version=v2.PROTOCOL_VERSION, info=v2.schema.Implementation(name="test-client", version="1.0.0")
460507
)
461508
await agent_connection.session_update(session_id="session-1", update=v2.schema.IdleSessionStateUpdate())
462-
await client_connection.send_extension_notification("_vendor/event")
509+
await client_connection.ext_notification("_vendor/event")
463510
await asyncio.sleep(0)
464511
await asyncio.sleep(0)
465512

@@ -556,7 +603,7 @@ async def test_cancelling_a_request_cancels_the_remote_handler() -> None:
556603
cancelled = asyncio.Event()
557604

558605
class SlowAgent(ExtensionAgent):
559-
async def handle_extension_request(self, method: str, params: Any) -> Any:
606+
async def ext_method(self, method: str, params: Any) -> Any:
560607
started.set()
561608
try:
562609
await asyncio.Event().wait()
@@ -572,7 +619,7 @@ async def handle_extension_request(self, method: str, params: Any) -> Any:
572619
await client_connection.initialize(
573620
protocol_version=v2.PROTOCOL_VERSION, info=v2.schema.Implementation(name="test-client", version="1.0.0")
574621
)
575-
request = asyncio.create_task(client_connection.send_extension_request("_vendor/slow"))
622+
request = asyncio.create_task(client_connection.ext_method("_vendor/slow"))
576623
await asyncio.wait_for(started.wait(), timeout=1)
577624

578625
request.cancel()
@@ -590,7 +637,7 @@ async def test_cancel_request_before_the_handler_starts_still_replies() -> None:
590637
handled: list[str] = []
591638

592639
class RecordingAgent(ExtensionAgent):
593-
async def handle_extension_request(self, method: str, params: Any) -> Any:
640+
async def ext_method(self, method: str, params: Any) -> Any:
594641
handled.append(method)
595642
return {}
596643

0 commit comments

Comments
 (0)