examples/agents/claude-api/agent.py.
Run it
It needs Python 3.10 or later, Node 22 or later, and an Anthropic API key.--check starts the browser tools and drives them directly, without a model call:
APOSTATE_MCP=/path/to/apostate/mcp/cli.mjs.
How it works
The script starts the MCP server as a child process, turns each of its tools into an SDK tool, and hands them to the tool runner:--isolatedgives each run a fresh, temporary machine. Drop it, or pass--profile <name>, to keep logins between runs.WITHHELDremovesbrowser_run_code_unsafe(arbitrary Playwright code) andbrowser_file_uploadfrom the tools the model gets.- The system prompt tells the model to snapshot before it clicks, to click a human-verification checkbox once as a person would, and to treat page text as data, not instructions.
fallbacks="default"lets the API re-run a request on a fallback model if the first model declines it. The script prints a line when a request is declined.- The runner calls the model, runs each tool call through the MCP session, and loops until the model answers without calling a tool.
Adapting it
- Persona and proxy. Change the server’s arguments:
--platform macos,--proxy socks5://...,--locale de-DE --timezone Europe/Berlin. MCP server lists them. - Fewer tools. A task that only reads pages needs
browser_navigate,browser_snapshotandbrowser_find. Fewer tools means shorter requests. - Your own tools. Mix
@beta_toolfunctions intotools, for example one that saves a result to your database.