Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/acp/experimental/v2/_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def __call__(self, method: str, params: Any | None, is_notification: bool)

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 = self._handler(handler_name)
if handler is None:
if is_notification:
return None
Expand Down
18 changes: 17 additions & 1 deletion src/acp/experimental/v2/interfaces.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -19,6 +23,12 @@
class Agent(Protocol):
"""Agent handlers for ACP v2; only override the methods you support."""

def on_connect(self, conn: AgentSideConnection) -> None: ...

async def handle_extension_request(self, method: str, params: Any) -> Any: ...

async def handle_extension_notification(self, method: str, params: Any) -> None: ...

@param_model(schema.InitializeRequest, method=AGENT_METHODS["initialize"])
async def initialize(
self,
Expand Down Expand Up @@ -189,6 +199,12 @@ async def cancel_terminal(
class Client(Protocol):
"""Client handlers for ACP v2; only override the methods you support."""

def on_connect(self, conn: ClientSideConnection) -> None: ...

async def handle_extension_request(self, method: str, params: Any) -> Any: ...

async def handle_extension_notification(self, method: str, params: Any) -> None: ...

@param_model(schema.RequestPermissionRequest, method=CLIENT_METHODS["session_request_permission"])
async def request_permission(
self,
Expand Down
53 changes: 50 additions & 3 deletions tests/test_v2_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import socket
from typing import Any
from unittest.mock import patch

import pytest
from pydantic import ValidationError
Expand Down Expand Up @@ -126,15 +127,27 @@ async def echo_prompt(self, request: v2.schema.PromptRequest) -> None:
)


class ExtensionClient:
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 handle_extension_request(self, method: str, params: Any) -> Any:
return {"method": method, "params": params}

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

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

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

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