Files
Firefox-MCP-extension/bridge/tools/take_screenshot.py
T
2026-11-22 04:45:52 -08:00

29 lines
1.0 KiB
Python

import asyncio
from typing import Any, Dict
from tools.utils import ToolError
async def handle(args: Dict[str, Any], bridge: Any) -> list:
"""
Captures a screenshot of the current viewport.
Window ID is optional - captures the visible tab in that window.
"""
request_id = f"req_{asyncio.get_event_loop().time()}_{id(args)}"
params: Dict[str, Any] = {}
if args.get("window_id") is not None:
params["window_id"] = int(args["window_id"])
result = await bridge.send_to_extension(request_id, "take_screenshot", params)
screenshot_url = None
if isinstance(result, dict):
screenshot_url = result.get("screenshot")
if screenshot_url and screenshot_url.startswith("data:image/png;base64,"):
b64_data = screenshot_url.split(",", 1)[1]
return [
{"type": "text", "text": "Screenshot captured successfully."},
{"type": "image", "data": b64_data, "mimeType": "image/png"}
]
return [{"type": "text", "text": "Failed to capture screenshot."}]