Something went wrong. Try again.
MCP based ACP agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199"""One custom agent, four ways to serve it.
A single pair of callables — a system prompt and a compaction strategy — iseverything crow needs to run your agent. This file plugs the same pair intoboth runtimes (v1 ``AcpAgent``, v2 ``CrowAgentV2``) and both transports(stdio, Streamable HTTP + WebSocket), so the runtime/transport choice is theonly thing that changes between the four subcommands.
The two callable contracts are spelled out in depth in``examples/custom_compactor.py``; here they are kept short so the matrix iswhat stands out. They are the SAME contracts on both runtimes — v2 reuses``crow_cli.agent.prompt.SystemPromptFactory`` and``crow_cli.agent.compact.Compactor`` verbatim.
Run:
uv run python examples/custom_agent.py v1-stdio --config-dir ~/.agents/crow uv run python examples/custom_agent.py v1-http --config-dir ~/.agents/crow uv run python examples/custom_agent.py v2-stdio --config-dir ~/.agents/crow uv run python examples/custom_agent.py v2-http --config-dir ~/.agents/crow
``*-http`` serve Streamable HTTP at ``http://127.0.0.1:<port>/acp``. StreamableHTTP requires HTTP/2, so they run hypercorn (already a crow-cli dependency),not a plain WSGI server."""
import asynciofrom pathlib import Path
import typerfrom acp import run_agent as run_agent_v1from acp.experimental.v2.agent import run_agent as run_agent_v2
from crow_cli.agent.compact import CompactCtx, CompactResponse, default_compactorfrom crow_cli.agent.hooks import uv_project_hookfrom crow_cli.agent.main import AcpAgentfrom crow_cli.agent.prompt import SystemPromptResponse, default_system_promptfrom crow_cli.agent2.agent import CrowAgentV2from crow_cli.config import Config
app = typer.Typer()
# ---------------------------------------------------------------------------# Contract 1: the system prompt. ``session/new`` renders this template once# and stores it; everything that varies goes in ``template_args``.# ---------------------------------------------------------------------------
TEMPLATE = """You are the operator for {{ workspace }}, generation {{ generation }}.
{{ display_tree }}"""
def init_sys(config: Config, cwd: str, session_id: str | None = None) -> SystemPromptResponse: base = default_system_prompt(config, cwd, session_id) return SystemPromptResponse( template=TEMPLATE, template_args={**base.template_args, "generation": 1}, )
def compact_sys(ctx: CompactCtx) -> SystemPromptResponse: fresh = default_system_prompt(ctx.config, ctx.session.cwd, ctx.session.session_id) return SystemPromptResponse( template=TEMPLATE, template_args={**fresh.template_args, "generation": ctx.session.agent_idx + 1}, )
# ---------------------------------------------------------------------------# Contract 2: compaction. Take the default summary, then tack on one line of# the successor's handoff. ``ctx.system_prompt`` is ``compact_sys`` above.# ---------------------------------------------------------------------------
async def custom_compactor(ctx: CompactCtx) -> CompactResponse: summary = await default_compactor(ctx) system_prompt = ctx.system_prompt(ctx) return CompactResponse( system_template=system_prompt.template, system_args=system_prompt.template_args, prompt=summary.prompt + "\n\nCarry on as the operator.", )
# ---------------------------------------------------------------------------# The four servings. Everything above is transport- and runtime-agnostic; only# these tails differ.# ---------------------------------------------------------------------------
def _v1_agent(config: Config) -> AcpAgent: return AcpAgent( config, hooks=[uv_project_hook], system_prompt=init_sys, compactor=custom_compactor, compact_system_prompt=compact_sys, )
def _v2_agent(config: Config) -> CrowAgentV2: return CrowAgentV2( config, hooks=[uv_project_hook], system_prompt=init_sys, compactor=custom_compactor, compact_system_prompt=compact_sys, )
@app.command("v1-stdio")def v1_stdio( config_dir: Path = typer.Option(..., "--config-dir", "-d"), debug: bool = typer.Option(False, "--debug"),) -> None: """One v1 agent per process, on stdin/stdout.""" config = Config.load(config_dir=config_dir) if debug: config.chunk_log = True asyncio.run(run_agent_v1(_v1_agent(config), use_unstable_protocol=True))
@app.command("v1-http")def v1_http( config_dir: Path = typer.Option(..., "--config-dir", "-d"), host: str = typer.Option("127.0.0.1", "--host"), port: int = typer.Option(2769, "--port"), debug: bool = typer.Option(False, "--debug"),) -> None: """One v1 agent per connection, over Streamable HTTP + WebSocket.""" config = Config.load(config_dir=config_dir) if debug: config.chunk_log = True asyncio.run(_v1_http(config, host, port))
async def _v1_http(config: Config, host: str, port: int) -> None: import hypercorn.asyncio from acp.http.asgi import create_asgi_app from hypercorn.config import Config as HypercornConfig
# A factory, not an instance: HTTP mints one agent per connection. app = create_asgi_app(lambda conn: _v1_agent(config)) hcfg = HypercornConfig() hcfg.bind = [f"{host}:{port}"] hcfg.alpn_protocols = ["h2", "http/1.1"] # Streamable HTTP requires HTTP/2 await hypercorn.asyncio.serve(app, hcfg)
@app.command("v2-stdio")def v2_stdio( config_dir: Path = typer.Option(..., "--config-dir", "-d"), debug: bool = typer.Option(False, "--debug"),) -> None: """One v2 agent per process, on stdin/stdout.""" config = Config.load(config_dir=config_dir) if debug: config.chunk_log = True asyncio.run(_v2_stdio(config))
async def _v2_stdio(config: Config) -> None: agent = _v2_agent(config) try: await run_agent_v2(agent) finally: await agent.cleanup()
@app.command("v2-http")def v2_http( config_dir: Path = typer.Option(..., "--config-dir", "-d"), host: str = typer.Option("127.0.0.1", "--host"), port: int = typer.Option(2771, "--port"), debug: bool = typer.Option(False, "--debug"),) -> None: """One v2 agent per connection, over Streamable HTTP + WebSocket.""" from crow_cli.agent2.main import serve_http
config = Config.load(config_dir=config_dir) if debug: config.chunk_log = True asyncio.run( serve_http( config, None, host, port, hooks=[uv_project_hook], system_prompt=init_sys, compactor=custom_compactor, compact_system_prompt=compact_sys, ) )
if __name__ == "__main__": app()