Skip to main content
You do not need an agent product to give a model a browser. The Anthropic Python SDK can take the tools of any MCP server and run the tool loop for you. This example connects it to the Apostate MCP server, so Claude drives an Apostate browser with Playwright’s browser tools. The code is 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:
To use a clone of this repository instead of the npm package, set 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:
  • --isolated gives each run a fresh, temporary machine. Drop it, or pass --profile <name>, to keep logins between runs.
  • WITHHELD removes browser_run_code_unsafe (arbitrary Playwright code) and browser_file_upload from 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_snapshot and browser_find. Fewer tools means shorter requests.
  • Your own tools. Mix @beta_tool functions into tools, for example one that saves a result to your database.