sinhsac.github.io / android-camera-mcp

Android Camera MCP

Turn any Android 10+ phone into a private, LAN-first remote camera for AI agents via the Model Context Protocol (MCP). Pairs a Kotlin / CameraX foreground service agent (with on-device ML Kit OCR, barcode scanning, and ROI crop) with a decoupled Python TCP gateway daemon and 15 MCP tools for Antigravity, Claude Desktop / Claude Code, Cursor, and Kiro.

Kotlin / CameraX On-Device ML Kit Python 3.10+ · asyncio MCP Python SDK 1.x Android 10+ (API 29) v0.5.0 MIT License

Why Android Camera MCP?

  • LAN-First & Private: Control messages, JPEG frames, and on-device ML Kit analysis stay strictly inside your local Wi-Fi/LAN. No cloud relays, accounts, or subscriptions.
  • Decoupled Gateway Daemon & Multi-Phone Registry: Run python main.py gateway once (:8765 for phones, 127.0.0.1:8766 for local MCP clients). Restarting an AI client or running several clients at once never drops the phone connection, and multiple phones can connect concurrently.
  • On-Device Scene Analysis (camera_analyze): Runs offline ML Kit OCR, QR/barcode scanning, and exposure/sharpness stats directly on the phone, returning compact JSON with normalized x,y,w,h bounding boxes — no large image transfer needed just to read a label or meter.
  • Phone-Side ROI Crop & Capture Presets: Pass normalized crop_x/y/w/h to crop from the full sensor before upload (paired with zoom_ratio or camera_preview/camera_analyze), plus named preset profiles (general, document, macro, low_light).
  • Background & Screen-Off Operation: CameraX is bound to an Android LifecycleService foreground service (FOREGROUND_SERVICE_TYPE_CAMERA) with wake and Wi-Fi locks, automatic reconnect on network change, and an in-app Allow background running shortcut.
  • Observability & LAN Discovery: Built-in mDNS/NSD discovery (_android-camera-mcp._tcp + Discover button), per-command timing breakdown, phone-side SQLite log viewer, and automatic log sync to ~/.android-camera-mcp/device-logs/.

Architecture

┌───────────────────────────────────────────────┐
│ Android Camera Agent (v0.5.0)                 │  Kotlin · CameraX · LifecycleService
│ Capture · ROI crop · Live frames · ML Kit OCR │  SQLite TraceLog · NSD Discover
└──────────────────────┬────────────────────────┘
                       │ Persistent TCP :8765 (4B length + 1B kind + payload)
                       │ 0x01 JSON control/logs + 0x02 binary JPEG
                       ▼
┌───────────────────────────────────────────────┐
│ Camera Gateway Daemon                         │  python main.py gateway
│ Multi-device registry · FIFO queue · mDNS     │  Stream buffer · Device log store
└──────────────────────┬────────────────────────┘
                       │ Local Control API (127.0.0.1:8766)
                       ▼
┌───────────────────────────────────────────────┐
│ MCP Server (stdio)                            │  python main.py mcp (one per AI client)
│ 15 typed FastMCP camera tools                 │
└──────────────────────┬────────────────────────┘
                       │ MCP over stdio
                       ▼
┌───────────────────────────────────────────────┐
│ Antigravity · Claude · Cursor · Kiro          │
└───────────────────────────────────────────────┘

Quick Start

1

Install the Android APK

Scan the QR code below or pick a versioned APK from GitHub Releases (Android 10+).

2

Run the Gateway Daemon

Start python main.py gateway on your LAN machine (:8765 for phones, 127.0.0.1:8766 for MCP clients, optional zeroconf for mDNS).

3

Connect Phone & AI Client

Register python main.py mcp in Antigravity / Claude / Cursor / Kiro, tap Discover (or enter LAN IP) and Connect on the phone.

1. Scan QR Code to Download APK

Every build on main publishes an immutable versioned release (v0.5.0-b<run>-<sha>) with SHA-256 checksums, plus updates the rolling latest-debug QR code below:

Scan QR code to download Android Camera Agent APK

2. Set Up & Start the Python Gateway

Requires Python 3.10+ (requirements.txt pins mcp<2 for the MCP Python SDK 1.x FastMCP API).

git clone https://github.com/sinhsac/android-camera-mcp.git
cd android-camera-mcp/server

python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install -r requirements.txt

# Optional: enable mDNS LAN discovery (Discover button on Android)
pip install zeroconf

# Keep the gateway running (phone port 8765, local API 127.0.0.1:8766)
python main.py gateway

# Optional: verbose logs + rotating log file
python main.py gateway --log-file ~/.android-camera-mcp/gateway.log -v

3. Configure Your MCP Client

Antigravity CLI:

agy mcp add android-camera \
  /absolute/path/to/android-camera-mcp/server/.venv/bin/python \
  /absolute/path/to/android-camera-mcp/server/main.py mcp

Claude Desktop, Cursor, or Kiro (mcpServers JSON):

{
  "mcpServers": {
    "android-camera": {
      "command": "/absolute/path/to/android-camera-mcp/server/.venv/bin/python",
      "args": ["/absolute/path/to/android-camera-mcp/server/main.py", "mcp"]
    }
  }
}

On Windows, set "command" to C:\\path\\to\\android-camera-mcp\\server\\.venv\\Scripts\\python.exe. If the gateway daemon is not running, tools cleanly report GATEWAY_UNAVAILABLE.

4. Connect the Phone

  • Open Android Camera Agent and grant Camera permission (plus Notifications on Android 13+).
  • Tap Discover to find the gateway automatically via mDNS (when zeroconf is installed), or enter your computer's LAN IP and port 8765 manually, then tap Connect.
  • Optional: tap Allow background running to exempt the app from battery optimization, and All logs to inspect the on-device SQLite trace database.
  • Tap Test capture to verify locally, or ask your AI: "List my connected Android cameras, analyze the scene for text or QR codes, and capture a tight crop."

Recommended AI Framing Workflow

  • Read text / labels / QR codes: call camera_analyze first (runs offline on the phone, returns OCR text, barcodes, exposure/focus hints, and normalized x,y,w,h boxes without sending a large image).
  • Inspect a fine detail: call camera_preview (or camera_analyze) → pick crop_x/y/w/h → call camera_capture(crop_*=..., max_dimension=1600) (optionally with zoom_ratio or preset="document").
  • Monitor a scene briefly: call camera_stream_start → poll camera_stream_frame(after_sequence=...) → call camera_stream_stop (auto-stops after duration or idle timeout).

Available MCP Tools (15)

Category Tool Description
Discovery camera_list List connected Android agents, online/stale status, active camera, lenses, battery, and thermal health.
Discovery camera_info Inspect per-camera capabilities (zoom/EV ranges, flash, manual focus, max JPEG size, presets), stream state, and FIFO queue depth.
Discovery camera_status Fetch live phone state, active camera, battery %, charging, temperature, thermal level, uptime, and timing breakdown.
Analysis camera_analyze On-device offline ML Kit OCR + barcode/QR scanning + exposure/focus stats (dark|ok|bright, blurry|soft|sharp) and normalized x,y,w,h boxes; supports optional crop_*, still=true, and include_image=true.
Capture camera_capture Capture an upright JPEG still (1600px long edge default; max_dimension=0 for full sensor) with optional preset (general|document|macro|low_light), phone-side ROI crop_x/y/w/h, and per-shot camera controls.
Capture camera_preview Fast low-resolution frame (640px default) from the live feed with optional phone-side crop_* to check framing before a full capture.
Streaming camera_stream_start Start a temporary low-FPS live stream with automatic duration and idle timeout limits.
Streaming camera_stream_frame Return the newest buffered stream frame; pass after_sequence to wait for a newer frame.
Streaming camera_stream_stop Stop an active stream on the device (idempotent).
Controls camera_set_zoom Set zoom ratio on the active camera (clamped to supported range, including ultra-wide <1.0x).
Controls camera_set_torch Toggle continuous flashlight/torch on or off.
Controls camera_set_flash Set flash mode (off, auto, on) for subsequent captures.
Controls camera_set_focus Configure continuous AF, one-shot auto AF, or manual focus distance (0.0–1.0).
Controls camera_set_exposure Set EV compensation steps or omit value to restore auto exposure (0).
Controls camera_select_lens Switch active camera/lens by camera_id and refresh advertised capabilities.

CLI Options & Server Tests

# Gateway daemon options
python main.py gateway \
  --host 0.0.0.0 --port 8765 \
  --api-host 127.0.0.1 --api-port 8766 \
  --min-capture-interval 1.5 --max-queue 4 \
  --log-file ~/.android-camera-mcp/gateway.log -v
# Add --no-mdns to disable _android-camera-mcp._tcp advertisement

# MCP stdio server (default subcommand)
python main.py mcp --api-host 127.0.0.1 --api-port 8766

# Run automated server test suite (real TCP sockets)
cd server
pip install -r requirements.txt pytest
python -m pytest -q

Security, Power & Troubleshooting

Trusted Home LAN Only (ADR-008)

The local control API binds strictly to 127.0.0.1:8766, while the device TCP listener on :8765 intentionally uses no pairing token for personal trusted-LAN simplicity. Allow inbound TCP 8765 only from your local subnet and do not expose it to the public internet.

  • Battery & OEM Background Killing: The foreground service holds a partial wake lock and Wi-Fi lock while connected. Plug the phone into power for extended sessions and tap Allow background running in the app to exempt it from aggressive battery optimization on Samsung, Xiaomi, or Oppo devices.
  • Per-Device FIFO Queue & Pacing: Commands for the same phone run in FIFO order (--max-queue 4 by default) and early captures automatically wait for the --min-capture-interval cooldown (~1.5s) instead of failing. RATE_LIMITED is only returned when the queue is full.
  • Device Logs & Crash Reports: The phone records trace logs in SQLite (viewable via All logs) and automatically uploads unsynced entries to ~/.android-camera-mcp/device-logs/<device_id>/logs.jsonl on the gateway host.
  • Building Android From Source: Requires JDK 17 and Android SDK (compileSdk 35, minSdk 29). Run ./gradlew assembleDebug from the repository root.