Skip to content

Commit 2040be1

Browse files
committed
feat(v2): declare on_connect and extension hooks on v2 protocols
1 parent f0f546e commit 2040be1

3 files changed

Lines changed: 68 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async def __call__(self, method: str, params: Any | None, is_notification: bool)
7676

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

‎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 handle_extension_request(self, method: str, params: Any) -> Any: ...
181+
182+
async def handle_extension_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 handle_extension_request(self, method: str, params: Any) -> Any: ...
256+
257+
async def handle_extension_notification(self, method: str, params: Any) -> None: ...
258+
259+
def on_connect(self, conn: ClientSideConnection) -> None: ...

‎tests/test_v2_runtime.py‎

Lines changed: 50 additions & 3 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+
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+
130137
async def handle_extension_request(self, method: str, params: Any) -> Any:
131138
return {"method": method, "params": params}
132139

140+
async def handle_extension_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:
@@ -146,6 +159,9 @@ async def initialize(
146159
async def handle_extension_request(self, method: str, params: Any) -> Any:
147160
return {"method": method, "params": params}
148161

162+
async def handle_extension_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,10 +433,13 @@ 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
)
@@ -436,6 +455,11 @@ async def test_extension_and_notification_names_are_explicit() -> None:
436455
with pytest.raises(ValueError, match="must start with '_'"):
437456
await client_connection.send_extension_request("vendor/do")
438457

458+
await client_connection.send_extension_notification("_vendor/event", {"value": 1})
459+
await agent_connection.send_extension_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})
462+
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")
441465

@@ -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, "handle_extension_notification", autospec=True) as notification:
492+
await client_connection.send_extension_notification("_vendor/event")
493+
with pytest.raises(RequestError, check=lambda error: error.code == -32601):
494+
await client_connection.send_extension_request("_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()

0 commit comments

Comments
 (0)