From 184130da2698b17be6f30725b6197ee70fa67d0c Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Sat, 28 Nov 2026 15:49:32 +0800 Subject: [PATCH] feat(v2): align extension methods with v1 and declare protocol hooks --- docs/experimental-v2.md | 2 +- src/acp/experimental/v2/_router.py | 4 +- src/acp/experimental/v2/agent.py | 4 +- src/acp/experimental/v2/client.py | 4 +- src/acp/experimental/v2/interfaces.py | 18 ++++++- tests/test_v2_runtime.py | 71 ++++++++++++++++++++++----- 6 files changed, 83 insertions(+), 20 deletions(-) diff --git a/docs/experimental-v2.md b/docs/experimental-v2.md index 318b0562..63932b0f 100644 --- a/docs/experimental-v2.md +++ b/docs/experimental-v2.md @@ -83,6 +83,6 @@ Each connection invokes the selected factory once and runs strictly under the ne ## Extensions, cancellation, and MCP-over-ACP -- Call `send_extension_request("_vendor/method", ...)` and `send_extension_notification("_vendor/event", ...)` for vendor extensions. +- 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. - `$/cancel_request` cancels running handler tasks and outgoing requests automatically; use `cancel_session(session_id=...)` for active session work. - [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 = {}`. diff --git a/src/acp/experimental/v2/_router.py b/src/acp/experimental/v2/_router.py index 64d332da..f436a1df 100644 --- a/src/acp/experimental/v2/_router.py +++ b/src/acp/experimental/v2/_router.py @@ -75,8 +75,8 @@ async def __call__(self, method: str, params: Any | None, is_notification: bool) return await self.handle_request(spec, params) async def _handle_extension(self, method: str, params: Any, is_notification: bool) -> Any: - handler_name = "handle_extension_notification" if is_notification else "handle_extension_request" - handler = getattr(self._target, handler_name, None) + handler_name = "ext_notification" if is_notification else "ext_method" + handler = self._handler(handler_name) if handler is None: if is_notification: return None diff --git a/src/acp/experimental/v2/agent.py b/src/acp/experimental/v2/agent.py index bd9ea9c4..1c4440f8 100644 --- a/src/acp/experimental/v2/agent.py +++ b/src/acp/experimental/v2/agent.py @@ -195,11 +195,11 @@ async def auth_status_update( build_request(schema.AuthStatusUpdateNotification, {"state": state, "accounts": accounts}, kwargs), ) - async def send_extension_request(self, method: str, params: Any = None) -> Any: + async def ext_method(self, method: str, params: Any = None) -> Any: await self._state.require(method) return await self._conn.send_request(_extension_method(method), params) - async def send_extension_notification(self, method: str, params: Any = None) -> None: + async def ext_notification(self, method: str, params: Any = None) -> None: await self._state.require(method) await self._conn.send_notification(_extension_method(method), params) diff --git a/src/acp/experimental/v2/client.py b/src/acp/experimental/v2/client.py index 7df456a0..4646beb0 100644 --- a/src/acp/experimental/v2/client.py +++ b/src/acp/experimental/v2/client.py @@ -347,11 +347,11 @@ async def cancel_terminal(self, session_id: str, terminal_id: str, **kwargs: Any build_request(schema.CancelTerminalRequest, {"session_id": session_id, "terminal_id": terminal_id}, kwargs), ) - async def send_extension_request(self, method: str, params: Any = None) -> Any: + async def ext_method(self, method: str, params: Any = None) -> Any: await self._state.require(method) return await self._conn.send_request(_extension_method(method), params) - async def send_extension_notification(self, method: str, params: Any = None) -> None: + async def ext_notification(self, method: str, params: Any = None) -> None: await self._state.require(method) await self._conn.send_notification(_extension_method(method), params) diff --git a/src/acp/experimental/v2/interfaces.py b/src/acp/experimental/v2/interfaces.py index f188c1ed..17c07780 100644 --- a/src/acp/experimental/v2/interfaces.py +++ b/src/acp/experimental/v2/interfaces.py @@ -1,7 +1,7 @@ from __future__ import annotations from functools import partial -from typing import Any, Literal, Protocol +from typing import TYPE_CHECKING, Any, Literal, Protocol from pydantic import AnyUrl, TypeAdapter @@ -11,6 +11,10 @@ from . import schema from .meta import AGENT_METHODS, CLIENT_METHODS +if TYPE_CHECKING: + from .agent import AgentSideConnection + from .client import ClientSideConnection + SetConfigOptionRequest = schema.SetSessionConfigOptionRequest CreateElicitationRequest = schema.CreateElicitationRequest CreateElicitationResponse = schema.CreateElicitationResponse @@ -173,6 +177,12 @@ async def cancel_terminal( self, session_id: str, terminal_id: str, **kwargs: Any ) -> schema.CancelTerminalResponse: ... + async def ext_method(self, method: str, params: Any) -> Any: ... + + async def ext_notification(self, method: str, params: Any) -> None: ... + + def on_connect(self, conn: AgentSideConnection) -> None: ... + class Client(Protocol): """Client handlers for ACP v2; only override the methods you support.""" @@ -241,3 +251,9 @@ async def auth_status_update( accounts: list[schema.AuthAccount] | None = None, **kwargs: Any, ) -> None: ... + + async def ext_method(self, method: str, params: Any) -> Any: ... + + async def ext_notification(self, method: str, params: Any) -> None: ... + + def on_connect(self, conn: ClientSideConnection) -> None: ... diff --git a/tests/test_v2_runtime.py b/tests/test_v2_runtime.py index e570ad86..c8210782 100644 --- a/tests/test_v2_runtime.py +++ b/tests/test_v2_runtime.py @@ -4,6 +4,7 @@ import json import socket from typing import Any +from unittest.mock import patch import pytest from pydantic import ValidationError @@ -126,15 +127,27 @@ async def echo_prompt(self, request: v2.schema.PromptRequest) -> None: ) -class ExtensionClient: - async def handle_extension_request(self, method: str, params: Any) -> Any: +class ExtensionClient(v2.Client): + def __init__(self) -> None: + self.notifications: asyncio.Queue[tuple[str, Any]] = asyncio.Queue() + + def on_connect(self, conn: v2.ClientSideConnection) -> None: + self.connection = conn + + async def ext_method(self, method: str, params: Any) -> Any: return {"method": method, "params": params} + async def ext_notification(self, method: str, params: Any) -> None: + await self.notifications.put((method, params)) + -class ExtensionAgent: +class ExtensionAgent(v2.Agent): def __init__(self) -> None: self.notifications: asyncio.Queue[tuple[str, Any]] = asyncio.Queue() + def on_connect(self, conn: v2.AgentSideConnection) -> None: + self.connection = conn + async def initialize( self, protocol_version: int, info: v2.schema.Implementation, **kwargs: Any ) -> v2.schema.InitializeResponse: @@ -143,9 +156,12 @@ async def initialize( info=v2.schema.Implementation(name="extension-agent", version="1.0.0"), ) - async def handle_extension_request(self, method: str, params: Any) -> Any: + async def ext_method(self, method: str, params: Any) -> Any: return {"method": method, "params": params} + async def ext_notification(self, method: str, params: Any) -> None: + await self.notifications.put((method, params)) + async def cancel_session(self, session_id: str, **kwargs: Any) -> None: notification = v2.schema.CancelSessionNotification(session_id=session_id, **kwargs) await self.notifications.put(("cancel", notification)) @@ -417,24 +433,32 @@ def test_v2_public_entry_point_is_explicit() -> None: async def test_extension_and_notification_names_are_explicit() -> None: client_transport, agent_transport = memory_transport_pair() agent = ExtensionAgent() + client = ExtensionClient() agent_connection = v2.AgentSideConnection(agent, agent_transport) - client_connection = v2.ClientSideConnection(ExtensionClient(), client_transport) + client_connection = v2.ClientSideConnection(client, client_transport) try: + assert agent.connection is agent_connection + assert client.connection is client_connection await client_connection.initialize( protocol_version=v2.PROTOCOL_VERSION, info=v2.schema.Implementation(name="test-client", version="1.0.0") ) - assert await client_connection.send_extension_request("_vendor/do", {"value": 1}) == { + assert await client_connection.ext_method("_vendor/do", {"value": 1}) == { "method": "_vendor/do", "params": {"value": 1}, } - assert await agent_connection.send_extension_request("_vendor/read", {"value": 2}) == { + assert await agent_connection.ext_method("_vendor/read", {"value": 2}) == { "method": "_vendor/read", "params": {"value": 2}, } with pytest.raises(ValueError, match="must start with '_'"): - await client_connection.send_extension_request("vendor/do") + await client_connection.ext_method("vendor/do") + + await client_connection.ext_notification("_vendor/event", {"value": 1}) + await agent_connection.ext_notification("_vendor/event", {"value": 2}) + assert await asyncio.wait_for(agent.notifications.get(), timeout=1) == ("_vendor/event", {"value": 1}) + assert await asyncio.wait_for(client.notifications.get(), timeout=1) == ("_vendor/event", {"value": 2}) await client_connection.cancel_session(session_id="session-1") 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: await agent_connection.close() +@pytest.mark.asyncio +async def test_inherited_extension_hooks_are_unimplemented() -> None: + class StubAgent(v2.Agent): + async def initialize( + self, protocol_version: int, info: v2.schema.Implementation, **kwargs: Any + ) -> v2.schema.InitializeResponse: + return v2.schema.InitializeResponse(protocol_version=protocol_version, info=info) + + client_transport, agent_transport = memory_transport_pair() + async with ( + v2.AgentSideConnection(StubAgent(), agent_transport), + v2.ClientSideConnection(Client(), client_transport) as client_connection, + ): + request = initialize_request() + await client_connection.initialize(protocol_version=request.protocol_version, info=request.info) + + with patch.object(v2.Agent, "ext_notification", autospec=True) as notification: + await client_connection.ext_notification("_vendor/event") + with pytest.raises(RequestError, check=lambda error: error.code == -32601): + await client_connection.ext_method("_vendor/ping") + notification.assert_not_called() + + @pytest.mark.asyncio async def test_unhandled_notifications_are_ignored(caplog: pytest.LogCaptureFixture) -> None: client_transport, agent_transport = memory_transport_pair() @@ -459,7 +506,7 @@ async def test_unhandled_notifications_are_ignored(caplog: pytest.LogCaptureFixt protocol_version=v2.PROTOCOL_VERSION, info=v2.schema.Implementation(name="test-client", version="1.0.0") ) await agent_connection.session_update(session_id="session-1", update=v2.schema.IdleSessionStateUpdate()) - await client_connection.send_extension_notification("_vendor/event") + await client_connection.ext_notification("_vendor/event") await asyncio.sleep(0) await asyncio.sleep(0) @@ -556,7 +603,7 @@ async def test_cancelling_a_request_cancels_the_remote_handler() -> None: cancelled = asyncio.Event() class SlowAgent(ExtensionAgent): - async def handle_extension_request(self, method: str, params: Any) -> Any: + async def ext_method(self, method: str, params: Any) -> Any: started.set() try: await asyncio.Event().wait() @@ -572,7 +619,7 @@ async def handle_extension_request(self, method: str, params: Any) -> Any: await client_connection.initialize( protocol_version=v2.PROTOCOL_VERSION, info=v2.schema.Implementation(name="test-client", version="1.0.0") ) - request = asyncio.create_task(client_connection.send_extension_request("_vendor/slow")) + request = asyncio.create_task(client_connection.ext_method("_vendor/slow")) await asyncio.wait_for(started.wait(), timeout=1) request.cancel() @@ -590,7 +637,7 @@ async def test_cancel_request_before_the_handler_starts_still_replies() -> None: handled: list[str] = [] class RecordingAgent(ExtensionAgent): - async def handle_extension_request(self, method: str, params: Any) -> Any: + async def ext_method(self, method: str, params: Any) -> Any: handled.append(method) return {}