DEV Community

Phonebox
Phonebox

Posted on Originally published at phonebox.dev

Give a Pydantic AI agent a cloud Android phone

Short answer: To give a Pydantic AI agent an Android phone, create an MCPToolset pointing at https://phonebox.dev/mcp, pass your Phonebox API key as auth, and add it to the agent's toolsets. Your agent instantly gets all 34 Phonebox tools: create a cloud phone, read the screen as numbered elements, tap, type and swipe with optional human-like gestures, install APKs, move files, set location, language and timezone, record the screen, reset it and park it. No emulator, no Android SDK, nothing to run on your machine. About ten lines of Python.

Why give a Pydantic AI agent a phone?

Pydantic AI makes agents type-safe and production-ready. But 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 real cloud Android phone with its own screen, apps and sign-ins.

  • Test the app you just built. Install the APK, walk the changed screens, and return a typed bug report.
  • Automate apps that have no API. Read a balance, change a setting, check a listing, and hand back validated data.
  • Keep sessions across runs. Park the phone and it keeps its apps, files and logins for the next run.

Phonebox's MCP server is hosted, so Pydantic AI connects to it over HTTPS like any remote MCP server.

The code

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 supports
Enter fullscreen mode Exit fullscreen mode
import 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())
Enter fullscreen mode Exit fullscreen mode

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.

Get typed results back from the phone

This is where Pydantic AI shines. Give the agent an output_type and the phone session ends in validated data, not 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],
)
Enter fullscreen mode Exit fullscreen mode

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])
Enter fullscreen mode Exit fullscreen mode

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 live_view_url 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 it 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. Phonebox also parks a phone after its idle timeout and ends a session at its maximum length, so a crashed script never leaves a phone running.
  • Phonebox's tool schemas also list context and llm_model, used only for analytics. The model fills them in from the schema; calls without them still work.
  • 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 and fastmcp-slim 4.2.5: MCPToolset connected to https://phonebox.dev/mcp with a project API key and listed all 34 Phonebox tools. Every code sample on this page type-checks against those versions. See Pydantic AI's MCP docs for the toolset's other options.

FAQ

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 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; 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.

Full guide: https://phonebox.dev/guides/pydantic-ai-android-phone

Top comments (0)