"""browser-use as a service on this VM: an MCP server and a small HTTP API. /mcp MCP (streamable HTTP), one tool: browser_task(task, model, profile) POST /run {"task": "...", "model": "gpt-6-luna", "profile": "default"}: runs it, answers JSON GET /runs the last 20 runs GET / a form to start a task from your browser Each task runs agent.py's run_task: Chrome on the desktop, models through exe.dev's LLM integration, one run per profile at a time. The server has no login of its own. It listens on port 8080, which exe.dev's HTTPS proxy serves only to people signed in to exe.dev with access to the VM, and to clients with a token for the VM; X-ExeDev-Email (who asked) is saved with each run. https://exe.dev/deploy/browser-use """ import asyncio import html import json import os import sys from pathlib import Path import uvicorn from mcp.server import MCPServer from mcp.server.transport_security import TransportSecuritySettings from starlette.requests import Request from starlette.responses import HTMLResponse, JSONResponse sys.path.insert(0, str(Path(__file__).parent)) from agent import BASE, DEFAULT_MODEL, run_task # noqa: E402 PORT = int(os.environ.get('PORT', '8080')) FORM = """
Signed in as {email}. Watch the agent on the desktop.
""" mcp = MCPServer('browser-use', instructions=( 'Runs a browser-use agent in a real Chrome on an exe.dev VM. Give it a whole task in plain words; ' 'it browses, then answers with the result. Logins in the chosen profile persist.')) async def run(task: str, model: str | None, profile: str | None, who: str | None) -> dict: # In a thread of its own: the profile lock and the agent's own event loop stay off the server's. return await asyncio.to_thread(lambda: asyncio.run(run_task( task, model=model or DEFAULT_MODEL, profile=profile or 'default', requested_by=who))) @mcp.tool() async def browser_task(task: str, model: str = DEFAULT_MODEL, profile: str = 'default') -> dict: """Do a task in a real web browser (search, read, fill in forms, click through sites) and return the result. Takes from seconds to a few minutes. model: an exe.dev LLM integration model; profile: the browser profile, whose logins persist between tasks.""" return await run(task, model, profile, 'mcp') @mcp.custom_route('/', methods=['GET']) async def form(request: Request): return HTMLResponse(FORM.format(email=html.escape(request.headers.get('x-exedev-email', 'nobody')), host=html.escape(request.url.hostname or ''), model=DEFAULT_MODEL)) @mcp.custom_route('/runs', methods=['GET']) async def runs(request: Request): logs = sorted((BASE / 'runs').glob('*.json'), reverse=True)[:20] return JSONResponse([json.loads(p.read_text()) for p in logs]) @mcp.custom_route('/run', methods=['POST']) async def run_route(request: Request): if request.headers.get('content-type', '').startswith('application/x-www-form-urlencoded'): req = dict(await request.form()) else: try: req = await request.json() except ValueError: return JSONResponse({'error': 'send JSON: {"task": "..."}'}, status_code=400) task = str(req.get('task') or '').strip() if not task: return JSONResponse({'error': 'task is required'}, status_code=400) try: record = await run(task, req.get('model'), req.get('profile'), request.headers.get('x-exedev-email')) except ValueError as e: # a bad profile name return JSONResponse({'error': str(e)}, status_code=400) return JSONResponse(record) if __name__ == '__main__': # Host checks are the proxy's job: requests arrive as