Something went wrong. Try again.
MCP based ACP agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165"""Entry point: run :class:`~crow_cli.agent2.agent.CrowAgentV2` over stdio or HTTP.
Deliberately smaller than v1's. ``agent/main.py`` carried the agent class, theHTTP server, the argparse block and the ``use_unstable_protocol`` flag thatunlocked ``session/fork``; here the agent is its own module and v2 has nounstable-method gate to open — a method is available if the object has it, and``MethodRouter`` answers ``method_not_found`` for the rest.
One thing worth knowing about the stdio path: ``run_agent`` builds theconnection and then calls ``on_connect`` synchronously, so the agent has itsconnection before the first request arrives. Cleanup runs in a ``finally``because a stdio agent is killed by its client far more often than it exits onits own, and the MCP clients it spawned are child processes.
``--http`` uses the same per-connection factory shape as v1: the``AgentProtocolRouter`` is handed ``lambda conn: CrowAgentV2(...)``, and its``attach`` calls ``on_connect`` before the first request. There is noper-connection disconnect hook, so HTTP agents are not cleaned up — exactly thebehavior v1's ``--http`` has."""
from __future__ import annotations
import argparseimport asynciofrom pathlib import Pathfrom typing import Any, Optional
from acp.experimental.v2.agent import run_agent
from crow_cli.agent.hooks import CommandHookfrom crow_cli.agent.prompt import SystemPromptFactoryfrom crow_cli.config import Config, apply_config_overrides
from .agent import CrowAgentV2
async def serve_http( config: Config, model: Optional[str], host: str, port: int, *, hooks: Optional[list[CommandHook]] = None, system_prompt: Optional[SystemPromptFactory] = None, compactor: Any = None, compact_system_prompt: Any = None,) -> None: """Serve ACP v2 over Streamable HTTP + WebSocket.
One ``CrowAgentV2`` per connection; the router negotiates the protocol from the first ``initialize``, and with only a v2 factory configured any v1 client is refused. All instances share the sqlite session store, so any client can address the same sessions.""" import hypercorn.asyncio from acp.experimental.negotiation import AgentProtocolRouter from acp.http.asgi import create_asgi_app from hypercorn.config import Config as HypercornConfig
app = create_asgi_app( AgentProtocolRouter( v2=lambda conn: CrowAgentV2( config, hooks=hooks, model=model, system_prompt=system_prompt, compactor=compactor, compact_system_prompt=compact_system_prompt, ) ) ) hcfg = HypercornConfig() hcfg.bind = [f"{host}:{port}"] # Streamable HTTP requires HTTP/2; hypercorn negotiates h2c/h2. hcfg.alpn_protocols = ["h2", "http/1.1"] await hypercorn.asyncio.serve(app, hcfg)
async def agent_run( config_dir: Optional[Path] = None, config: Optional[Config] = None, config_file: Optional[Path] = None, debug: bool = False, model: Optional[str] = None, hooks: Optional[list[CommandHook]] = None, system_prompt: Optional[SystemPromptFactory] = None, compactor: Any = None, compact_system_prompt: Any = None, http: bool = False, host: str = "127.0.0.1", port: int = 2771,) -> None: """Build the agent and serve it until the client lets go.
stdio by default; ``http`` serves v2 over Streamable HTTP + WebSocket via an ``AgentProtocolRouter``.""" if config is None: config = Config.load(config_dir=config_dir) config = apply_config_overrides(config, config_file) if debug: config.chunk_log = True if http: await serve_http( config, model, host, port, hooks=hooks, system_prompt=system_prompt, compactor=compactor, compact_system_prompt=compact_system_prompt, ) return agent = CrowAgentV2( config, hooks=hooks, model=model, system_prompt=system_prompt, compactor=compactor, compact_system_prompt=compact_system_prompt, ) try: await run_agent(agent) finally: await agent.cleanup()
def main( config_dir: Optional[Path] = None, config: Optional[Config] = None, config_file: Optional[Path] = None, debug: bool = False, model: Optional[str] = None, http: bool = False, host: str = "127.0.0.1", port: int = 2771,) -> None: asyncio.run( agent_run( config_dir=config_dir, config=config, config_file=config_file, debug=debug, model=model, http=http, host=host, port=port, ) )
if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--config-dir", type=Path, default=None) parser.add_argument("--config-file", type=Path, default=None) parser.add_argument("--debug", action="store_true") parser.add_argument("--model", type=str, default=None) args = parser.parse_args() main( config_dir=args.config_dir, config_file=args.config_file, debug=args.debug, model=args.model, )