Something went wrong. Try again.
MCP based ACP agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324#!/usr/bin/env python3"""Minimal streaming react loop: MCP tools, OpenAI chat completions.
The whole agent in one file — no ACP, no database, no compaction, nocancellation (see ``streaming_async_react_with_cancellation.py`` for that; theonly difference is the cancel paths). Stream, accumulate, call the tool, append,loop. Same general idea as the original sandbox script; the wire details are whatcrow-cli does today (``crow_cli/agent2/llm.py``, ``agent2/tools.py``,``agent/session.py``).
uv run python sandbox/async-react/streaming_async_react.py ... --raw dump every request body"""
import asyncioimport jsonimport osimport sysfrom dataclasses import dataclass, fieldfrom pathlib import Path
import httpxfrom fastmcp import Clientfrom mcp.types import ImageContentfrom openai import AsyncOpenAI
API_KEY = os.getenv("ALIBABA_API_KEY")BASE_URL = os.getenv("ALIBABA_BASE_URL", "https://token-plan.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1")MODEL = os.getenv("REACT_MODEL", "glm-5.2")MAX_TOKENS = 8192MCP_CONFIG = { "mcpServers": { "crow-mcp": { "transport": "stdio", "command": str(Path.home() / ".agents/crow/src/crow-cli/.venv/bin/crow-cli"), "args": ["mcp2"], } }}
def configure_provider(raw: bool = False) -> AsyncOpenAI: async def log_request(request): print(f"\n{'=' * 20} RAW REQUEST {'=' * 20}\n{request.read().decode()}\n")
hooks = {"request": [log_request]} if raw else None return AsyncOpenAI( api_key=API_KEY, base_url=BASE_URL, http_client=httpx.AsyncClient(event_hooks=hooks) if hooks else None, )
async def get_tools(mcp_client: Client) -> list[dict]: """MCP schemas -> OpenAI tool definitions. ``or ""`` because a null description is rejected by some providers and many MCP tools omit it.""" return [ { "type": "function", "function": {"name": t.name, "description": t.description or "", "parameters": t.inputSchema}, } for t in await mcp_client.list_tools() ]
async def send_request(lm, messages, model, tools): """Open a streaming completion.
``include_usage`` is what makes the last chunk carry token counts — and that chunk has ``choices: []``, which is why the accumulator guards the index. ``parallel_tool_calls`` is not a sampling param and omitting it is NOT false, so it is always sent. ``reasoning_effort`` XOR ``temperature``: reasoning models reject temperature. """ return await lm.chat.completions.create( model=model, messages=messages, tools=tools, stream=True, max_tokens=MAX_TOKENS, stream_options={"include_usage": True}, temperature=0.6, parallel_tool_calls=False, )
@dataclassclass StreamState: """What the stream has produced so far."""
thinking: list[str] = field(default_factory=list) content: list[str] = field(default_factory=list) tool_calls: dict[int, dict] = field(default_factory=dict) # by delta index usage: dict | None = None
def reset(self) -> None: self.thinking.clear() self.content.clear() self.tool_calls.clear() self.usage = None
def accumulate_chunk(chunk, state: StreamState) -> list[tuple[str, object]]: """Fold one chunk into ``state``; return the new tokens by kind.
Reasoning, content and tool calls are three INDEPENDENT checks — one chunk can carry reasoning AND content (the transition boundary), and an if/else between them silently drops one. """ if getattr(chunk, "usage", None) is not None: state.usage = { "prompt_tokens": chunk.usage.prompt_tokens, "completion_tokens": chunk.usage.completion_tokens, "total_tokens": chunk.usage.total_tokens, } if not chunk.choices: return [] delta = chunk.choices[0].delta tokens: list[tuple[str, object]] = []
reasoning = getattr(delta, "reasoning_content", None) if reasoning: state.thinking.append(reasoning) tokens.append(("thinking", reasoning)) if delta.content: state.content.append(delta.content) tokens.append(("content", delta.content))
for call in delta.tool_calls or (): # ``index`` is the only reliable accumulation key: it is on every delta, # while id/type/name arrive exactly once, in the first one. Keying by id # breaks parallel calls, and breaks on alibaba compatible-mode, which # sends id:"" and name:"" on every continuation delta — `is not None` # passes, the real values are overwritten, the tool name is gone. Hence # truthiness below, not identity. slot = state.tool_calls.setdefault(call.index, {"id": "", "function_name": "", "arguments": []}) if call.id: slot["id"] = call.id if call.function and call.function.name: slot["function_name"] = call.function.name tokens.append(("tool_call", (call.function.name, call.function.arguments or ""))) if call.function and call.function.arguments: slot["arguments"].append(call.function.arguments) if not any(kind == "tool_call" for kind, _ in tokens): tokens.append(("tool_args", call.function.arguments)) return tokens
def build_tool_calls(state: StreamState) -> list[dict]: """Deltas -> OpenAI ``tool_calls``, by index, argument JSON repaired.
The id is the LLM's and stays the LLM's — the only link between the assistant message and its tool results. A provider that never sent one gets "" on both sides, never a generated UUID. Repair happens here, before the string enters history: a model that truncates its arguments would otherwise poison every later request, since the assistant message is re-sent each turn. """ calls = [] for _, slot in sorted(state.tool_calls.items()): arguments = "".join(slot["arguments"]) try: json.loads(arguments) except (json.JSONDecodeError, TypeError, ValueError): arguments += "}" * (arguments.count("{") - arguments.count("}")) arguments += "]" * (arguments.count("[") - arguments.count("]")) try: json.loads(arguments) except (json.JSONDecodeError, TypeError, ValueError): arguments = "{}" calls.append( dict(id=slot["id"], type="function", function=dict(name=slot["function_name"], arguments=arguments)) ) return calls
def deserialize_args(data): """Recursively decode JSON strings nested inside arguments — models double-encode constantly, and handing a tool the string '{"a": 1}' where it wanted an object is a spurious failure. Only strings starting { or [ are attempted, so a bare "1" stays a string. This is the top-level parse too: a well-formed call comes back a dict, anything else is malformed.""" if isinstance(data, str): if data.startswith(("{", "[")): try: return deserialize_args(json.loads(data)) except (json.JSONDecodeError, TypeError, ValueError): pass return data if isinstance(data, dict): return {k: deserialize_args(v) for k, v in data.items()} if isinstance(data, list): return [deserialize_args(item) for item in data] return data
def mcp_to_llm(mcp_content) -> object: """MCP content blocks -> OpenAI tool-message content: a list only when there is something a string cannot carry (an image), otherwise the plain text. The obvious ``result.content[0].text`` dies on an empty result and on any non-text block.""" if not mcp_content: return "" blocks = [] for item in mcp_content: if isinstance(item, ImageContent): blocks.append({"type": "image_url", "image_url": {"url": f"data:{item.mimeType};base64,{item.data}"}}) else: blocks.append({"type": "text", "text": str(getattr(item, "text", item))}) if len(blocks) == 1 and blocks[0]["type"] == "text": return blocks[0]["text"] return blocks
async def execute_tool_calls(mcp_client, tool_call_inputs) -> list[dict]: """Run a batch. A tool that raises is a tool result, not an exception: the model gets to see the error and recover, which it cannot do if the turn dies.""" tool_results = [] for tool_call in tool_call_inputs: name = tool_call["function"]["name"] raw_args = tool_call["function"]["arguments"] args = deserialize_args(raw_args) if not isinstance(args, dict): tool_call["function"]["arguments"] = "{}" # in place: history is re-sent content = (f"Error: malformed arguments for '{name}': {raw_args!r}. " "Retry with valid JSON matching the tool schema.") else: try: # call_tool_mcp, not call_tool: the raw protocol result, with # isError and every content block, and no raise on tool error. result = await mcp_client.call_tool_mcp(name, args) content = mcp_to_llm(result.content) if result.isError: content = f"Error: {content}" except Exception as exc: content = f"Error: {exc}" tool_results.append(dict(role="tool", tool_call_id=tool_call["id"], content=content)) return tool_results
def add_response_to_messages(messages, state, tool_call_inputs, tool_results): """ONE assistant message, then one tool message per call, in that order.
Not a content message plus a separate tool_calls message: the tool_calls ride on the assistant turn and the results must follow it immediately, each keyed by an id from it. ``content`` is always present, possibly "" — an assistant message with no content key is another thing providers reject. """ if not (state.content or state.thinking or tool_call_inputs): return messages # an empty completion persists nothing thinking = "".join(state.thinking) msg = {"role": "assistant", "content": "".join(state.content)} if thinking: msg["reasoning_content"] = thinking if tool_call_inputs: msg["tool_calls"] = tool_call_inputs messages.append(msg) messages.extend(tool_results) return messages
async def react_loop(messages, mcp_client, lm, model, tools, max_turns=50000): """Stream -> accumulate -> run tools -> append -> loop. Yields ``{"type", "token"}`` for whoever is rendering; ``messages`` is the caller's and is mutated in place.""" state = StreamState() for _ in range(max_turns): response = await send_request(lm, messages, model, tools) state.reset() async for chunk in response: for kind, token in accumulate_chunk(chunk, state): yield {"type": kind, "token": token}
tool_call_inputs = build_tool_calls(state) if not tool_call_inputs: add_response_to_messages(messages, state, [], []) yield {"type": "usage", "token": state.usage} return tool_results = await execute_tool_calls(mcp_client, tool_call_inputs) add_response_to_messages(messages, state, tool_call_inputs, tool_results)
def make_renderer(): last = None
def render(chunk): nonlocal last kind, token = chunk["type"], chunk["token"] if last == "tool_args" and kind != "tool_args": print(")") if kind in ("thinking", "content"): if last != kind: print(("\n" if last else "") + f"[{kind}] ", end="") print(token, end="", flush=True) elif kind == "tool_call": print(f"\n[tool] {token[0]}({token[1]}", end="", flush=True) elif kind == "tool_args": print(token, end="", flush=True) elif kind == "usage": print(f"\n[usage] {token}") last = kind
return render
async def run(raw: bool) -> list[dict]: mcp_client = Client(MCP_CONFIG) lm = configure_provider(raw) render = make_renderer() messages = [ dict(role="system", content="You are a helpful assistant named Crow."), dict(role="user", content="search for machine learning papers with your search tool"), ] async with mcp_client: tools = await get_tools(mcp_client) async for chunk in react_loop(messages, mcp_client, lm, MODEL, tools): render(chunk) return messages
if __name__ == "__main__": asyncio.run(run("--raw" in sys.argv[1:]))