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

36 lines
1.3 KiB
Python

import asyncio
from typing import Any, Dict
from tools.utils import ToolError
async def handle(args: Dict[str, Any], bridge: Any) -> list:
"""
Retrieves cookies for the specified tab or current domain.
"""
request_id = f"req_{asyncio.get_event_loop().time()}_{id(args)}"
params: Dict[str, Any] = {}
if args.get("tab_id") is not None:
params["tab_id"] = int(args["tab_id"])
if args.get("window_id") is not None:
params["window_id"] = int(args["window_id"])
result = await bridge.send_to_extension(request_id, "get_page_cookies", params)
cookies = result if isinstance(result, list) else []
lines = ["# Page Cookies\n"]
if not cookies:
lines.append("No cookies found for this domain.")
else:
for c in cookies:
name = c.get("name", "")
value = c.get("value", "")
domain = c.get("domain", "")
path = c.get("path", "")
secure = "Secure" if c.get("secure") else ""
http_only = "HttpOnly" if c.get("httpOnly") else ""
session = "Session" if c.get("session") else ""
flags = " | ".join([f for f in [secure, http_only, session] if f])
lines.append(f"- **{name}**: {value} (domain={domain}, path={path}) {flags}")
return [{"type": "text", "text": "\n".join(lines)}]