Give a Pydantic AI agent a cloud Android phone with MCPToolset.
Why give a Pydantic AI agent a phone?
Pydantic AI agents return typed, validated data. Most of the apps your users live in are mobile apps with no API. Phonebox closes that gap: your agent uses the app the way a person does, on a cloud Android phone with its own screen, apps and sign-ins. That gives you:
- Testing the app you just built. Install the APK, walk the changed screens and return a typed bug report.
- Automating apps that have no API. Read a balance, change a setting, check a listing and hand back validated data.
- Sessions that persist across runs. Park the phone and it keeps its apps, files and logins for the next run.
Phonebox's MCP server is hosted and stateless, so Pydantic AI connects to it over HTTPS like any other remote MCP server.
The code
Install Pydantic AI, which includes MCP support, and set your keys:
pip install pydantic-ai # or: pip install "pydantic-ai-slim[mcp,openai]"
export PHONEBOX_API_KEY=pbx_...
export OPENAI_API_KEY=... # or any model provider Pydantic AI supportsimport asyncio
import os
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPToolset
phonebox = MCPToolset(
"https://phonebox.dev/mcp",
auth=os.environ["PHONEBOX_API_KEY"], # sent as a bearer token
)
agent = Agent(
"openai:gpt-5",
instructions=(
"You operate a cloud Android phone through the Phonebox tools. "
"Start or create one phone, call observe before every action, "
"batch steps in act, and park the phone when you are done."
),
toolsets=[phonebox],
)
async def main() -> None:
async with agent: # opens the MCP session once for the whole run
result = await agent.run("Open Settings and tell me the Android version.")
print(result.output)
if __name__ == "__main__":
asyncio.run(main())
async with agent keeps one MCP session open for everything inside it. Without it, Pydantic AI opens the session on demand for each run. The tools keep their Phonebox names, such as observe and act. Swap "openai:gpt-5" for any model string Pydantic AI supports; the Phonebox side doesn't change.
How do I get typed results back from the phone?
Give the agent an output_type and the phone session ends in validated data instead of free text:
import os
from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPToolset
class ScreenCheck(BaseModel):
passed: bool
screen_title: str
problems: list[str]
phonebox = MCPToolset("https://phonebox.dev/mcp", auth=os.environ["PHONEBOX_API_KEY"])
qa_agent = Agent(
"openai:gpt-5",
output_type=ScreenCheck,
instructions="Install the APK, open the app, check the login screen, then park the phone.",
toolsets=[phonebox],
)
result.output is a ScreenCheck, ready to fail a CI job or open a ticket.
Keep the tool list short
A phone task rarely needs all 34 tools. .filtered() limits what the model sees, which saves tokens and keeps the agent on task:
import os
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPToolset
PHONE_TOOLS = {"create_phone", "start_phone", "observe", "act",
"install_app", "live_view_url", "park_phone"}
phonebox = MCPToolset(
"https://phonebox.dev/mcp",
auth=os.environ["PHONEBOX_API_KEY"],
).filtered(lambda ctx, tool: tool.name in PHONE_TOOLS)
agent = Agent("openai:gpt-5", toolsets=[phonebox])
How the agent drives the phone
The loop is simple: create_phone (or start_phone for a parked one), then observe to read the screen as a numbered list of elements plus a screenshot, then act to tap, type or swipe. act runs up to 20 actions in one call and stops at the first failure, so the agent needs fewer model calls per task. Add humanize: true to a tap, double tap, swipe or scroll for human-like input: jitter, a curved path and human timing. Open the live_view_url tool's link in your browser to watch the phone live, or take over for a sign-in.
Keys, costs and cleanup
Create an Agent key in API keys and export it as PHONEBOX_API_KEY. An Agent key can create, start, use and park phones but can't delete them, which is the right default for an agent. Give the key an expiry, and set a monthly spending limit in Settings: it stops new sessions when it's reached.
Tell the agent to park every phone it starts. A parked phone costs nothing and keeps its apps, files and sign-ins for the next run. Phonebox also parks a phone after its idle timeout and ends a session at its maximum length, so a crashed script doesn't leave a phone running.
Phonebox's tool schemas also list context (a short reason for the call) and llm_model, which are used only for analytics. Models fill them in from the schema, so an agent needs nothing extra. The server doesn't reject a call without them, so code that calls a tool directly can leave them out.
Pricing: Starter is $20/month and includes $20 of phone time; phone time is $0.06 a minute, billed per second. See pricing.
What was checked
Checked on December 4, 2026 with pydantic-ai-slim 2.78.0, fastmcp-slim 4.2.5 and mcp 2.3.0: MCPToolset connected to https://phonebox.dev/mcp with a project API key and listed all 34 Phonebox tools. The code on this page type-checks against those versions with pyright. See Pydantic AI's MCP client docs for the toolset's other options.
Pydantic AI: common questions
Can Pydantic AI use a hosted MCP server?
Yes. MCPToolset takes a URL and connects over Streamable HTTP. For Phonebox, use https://phonebox.dev/mcp and pass your project API key as auth, which Pydantic AI sends as a bearer token.
Which package do I need?
pydantic-ai includes MCP support. With the slim package, install the mcp extra plus your model provider's extra, for example pydantic-ai-slim[mcp,openai].
Should I use MCPToolset or the MCP capability?
Either works with Phonebox, with the same URL and key. Pydantic AI's docs recommend the MCP capability for most uses. This page uses MCPToolset because it gives you toolset methods such as .filtered() and control of the connection with async with.
Does it work with Claude, Gemini or local models?
Yes. The Phonebox connection doesn't depend on the model. Change the model string and keep the toolset.
Do I need an Android emulator or the Android SDK?
No. The phone runs in Phonebox's cloud and the MCP server is hosted, so nothing Android-related runs on your machine.