Skip to content
Merged
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 docs/experimental-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}`.
4 changes: 2 additions & 2 deletions src/acp/experimental/v2/_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/acp/experimental/v2/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions src/acp/experimental/v2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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 Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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: ...
71 changes: 59 additions & 12 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:
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:
Expand All @@ -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))
Expand Down Expand Up @@ -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")
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, "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()
Expand All @@ -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)

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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 {}

Expand Down
Loading