| 1 | # /// script |
| 2 | # requires-python = ">=3.10,<3.15" |
| 3 | # dependencies = [ |
| 4 | # "agent-client-protocol", |
| 5 | # ] |
| 6 | # /// |
| 7 | import asyncio |
| 8 | from typing import Any |
| 9 | from uuid import uuid4 |
| 10 | |
| 11 | from acp import ( |
| 12 | Agent, |
| 13 | InitializeResponse, |
| 14 | NewSessionResponse, |
| 15 | PromptResponse, |
| 16 | run_agent, |
| 17 | text_block, |
| 18 | update_agent_message, |
| 19 | ) |
| 20 | from acp.interfaces import Client |
| 21 | from acp.schema import ( |
| 22 | AudioContentBlock, |
| 23 | ClientCapabilities, |
| 24 | EmbeddedResourceContentBlock, |
| 25 | HttpMcpServer, |
| 26 | ImageContentBlock, |
| 27 | Implementation, |
| 28 | McpServerStdio, |
| 29 | ResourceContentBlock, |
| 30 | SseMcpServer, |
| 31 | TextContentBlock, |
| 32 | ) |
| 33 | |
| 34 | |
| 35 | class EchoAgent(Agent): |
| 36 | _conn: Client |
| 37 | |
| 38 | def on_connect(self, conn: Client) -> None: |
| 39 | self._conn = conn |
| 40 | |
| 41 | async def initialize( |
| 42 | self, |
| 43 | protocol_version: int, |
| 44 | client_capabilities: ClientCapabilities | None = None, |
| 45 | client_info: Implementation | None = None, |
| 46 | **kwargs: Any, |
| 47 | ) -> InitializeResponse: |
| 48 | return InitializeResponse(protocol_version=protocol_version) |
| 49 | |
| 50 | async def new_session( |
| 51 | self, cwd: str, mcp_servers: list[HttpMcpServer | SseMcpServer | McpServerStdio], **kwargs: Any |
| 52 | ) -> NewSessionResponse: |
| 53 | return NewSessionResponse(session_id=uuid4().hex) |
| 54 | |
| 55 | async def prompt( |
| 56 | self, |
| 57 | prompt: list[ |
| 58 | TextContentBlock |
| 59 | | ImageContentBlock |
| 60 | | AudioContentBlock |
| 61 | | ResourceContentBlock |
| 62 | | EmbeddedResourceContentBlock |
| 63 | ], |
| 64 | session_id: str, |
| 65 | **kwargs: Any, |
| 66 | ) -> PromptResponse: |
| 67 | for block in prompt: |
| 68 | text = block.get("text", "") if isinstance(block, dict) else getattr(block, "text", "") |
| 69 | chunk = update_agent_message(text_block(text)) |
| 70 | chunk.field_meta = {"echo": True} |
| 71 | chunk.content.field_meta = {"echo": True} |
| 72 | |
| 73 | await self._conn.session_update(session_id=session_id, update=chunk, source="echo_agent") |
| 74 | return PromptResponse(stop_reason="end_turn") |
| 75 | |
| 76 | |
| 77 | async def main() -> None: |
| 78 | await run_agent(EchoAgent()) |
| 79 | |
| 80 | |
| 81 | if __name__ == "__main__": |
| 82 | asyncio.run(main()) |
| 83 | |