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

237 lines
10 KiB
Python

from typing import Any, Callable, Dict
class Tool:
def __init__(self, name: str, description: str, schema: Dict[str, Any], handler: Callable):
self.name = name
self.description = description
self.schema = schema
self.handler = handler
def to_mcp_dict(self):
return {
"name": self.name,
"description": self.description,
"inputSchema": self.schema
}
from .page_summary import handle as get_page_summary_handler
from .element_details import handle as get_element_details_handler
from .click_element import handle as click_element_handler
from .type_text import handle as type_text_handler
from .press_key import handle as press_key_handler
from .scroll_page import handle as scroll_page_handler
from .execute_script import handle as execute_script_handler
from .take_screenshot import handle as take_screenshot_handler
from .read_page_text import handle as read_page_text_handler
from .list_tabs import handle as list_tabs_handler
from .switch_tab import handle as switch_tab_handler
from .navigate_to import handle as navigate_to_handler
from .open_new_tab import handle as open_new_tab_handler
from .get_current_url import handle as get_current_url_handler
from .get_page_cookies import handle as get_page_cookies_handler
TOOL_REGISTRY = [
Tool(
name="get_page_summary",
description="Returns a condensed 'Accessibility Tree' of the current page. Scans for interactive elements (buttons, links, inputs, etc.), assigns a temporary unique ID (data-mcp-id), and returns a simplified list with role and visible text/label.",
schema={
"type": "object",
"properties": {
"tab_id": {"type": "integer", "description": "The Firefox Tab ID to scan"},
"window_id": {"type": "integer", "description": "The Firefox Window ID containing the tab to scan"},
},
"required": [],
},
handler=get_page_summary_handler
),
Tool(
name="get_element_details",
description="Returns detailed attributes, value, bounding box, computed style, and surrounding HTML context for a specific element mcp-id.",
schema={
"type": "object",
"properties": {
"id": {"type": "integer", "description": "The mcp-id of the element to inspect"},
"tab_id": {"type": "integer", "description": "The Firefox Tab ID containing the element"},
"window_id": {"type": "integer", "description": "The Firefox Window ID containing the element"},
},
"required": ["id"],
},
handler=get_element_details_handler
),
Tool(
name="click_element",
description="Performs a click action on the element with the specified mcp-id.",
schema={
"type": "object",
"properties": {
"id": {"type": "integer", "description": "The mcp-id of the element to click"},
"tab_id": {"type": "integer", "description": "The Firefox Tab ID containing the element"},
"window_id": {"type": "integer", "description": "The Firefox Window ID containing the element"},
},
"required": ["id"],
},
handler=click_element_handler
),
Tool(
name="type_text",
description="Focuses the element with the specified mcp-id and types the provided text into it.",
schema={
"type": "object",
"properties": {
"id": {"type": "integer", "description": "The mcp-id of the element to type into"},
"text": {"type": "string", "description": "The text to type"},
"tab_id": {"type": "integer", "description": "The Firefox Tab ID containing the element"},
"window_id": {"type": "integer", "description": "The Firefox Window ID containing the element"},
},
"required": ["id", "text"],
},
handler=type_text_handler
),
Tool(
name="press_key",
description="Simulates a keyboard press on the currently focused element (e.g., 'Enter', 'Escape', 'Tab', 'ArrowDown').",
schema={
"type": "object",
"properties": {
"key": {"type": "string", "description": "The key to press (e.g., 'Enter', 'Escape', 'Tab')"},
"tab_id": {"type": "integer", "description": "The Firefox Tab ID containing the element"},
"window_id": {"type": "integer", "description": "The Firefox Window ID containing the element"},
},
"required": ["key"],
},
handler=press_key_handler
),
Tool(
name="scroll_page",
description="Scrolls the page by a specified direction and amount. Amount can be pixels or a percentage (1-100) of the viewport.",
schema={
"type": "object",
"properties": {
"direction": {"type": "string", "description": "Direction to scroll: 'up', 'down', 'left', 'right'", "enum": ["up", "down", "left", "right"]},
"amount": {"type": "integer", "description": "Pixels to scroll, or a percentage (1-100) of viewport size", "default": 500},
"tab_id": {"type": "integer", "description": "The Firefox Tab ID to scroll"},
"window_id": {"type": "integer", "description": "The Firefox Window ID to scroll"},
},
"required": ["direction"],
},
handler=scroll_page_handler
),
Tool(
name="execute_script",
description="Executes arbitrary JavaScript in the context of the current page. (God Mode - use with caution).",
schema={
"type": "object",
"properties": {
"js_code": {"type": "string", "description": "JavaScript code to execute"},
"tab_id": {"type": "integer", "description": "The Firefox Tab ID to execute in"},
"window_id": {"type": "integer", "description": "The Firefox Window ID to execute in"},
},
"required": ["js_code"],
},
handler=execute_script_handler
),
Tool(
name="take_screenshot",
description="Captures a screenshot of the current viewport in a window and returns it as a data URL (PNG). Can only capture the visible tab in a window.",
schema={
"type": "object",
"properties": {
"window_id": {"type": "integer", "description": "The Firefox Window ID to capture (uses the visible tab in that window)"},
},
"required": [],
},
handler=take_screenshot_handler
),
Tool(
name="read_page_text",
description="Returns a 'Reader Mode' version of the page, stripping scripts, styles, nav, headers, footers, and returning only the primary textual content.",
schema={
"type": "object",
"properties": {
"tab_id": {"type": "integer", "description": "The Firefox Tab ID to read"},
"window_id": {"type": "integer", "description": "The Firefox Window ID to read"},
},
"required": [],
},
handler=read_page_text_handler
),
Tool(
name="list_tabs",
description="Lists all open Firefox tabs, including their titles, URLs, internal Tab IDs, and active status.",
schema={
"type": "object",
"properties": {},
"required": [],
},
handler=list_tabs_handler
),
Tool(
name="switch_tab",
description="Switches focus to the specified Tab ID or Window ID.",
schema={
"type": "object",
"properties": {
"tab_id": {"type": "integer", "description": "The Firefox Tab ID to switch to"},
"window_id": {"type": "integer", "description": "The Firefox Window ID to switch to (switches to active tab in that window)"},
},
"required": [],
},
handler=switch_tab_handler
),
Tool(
name="navigate_to",
description="Navigates the specified tab to the specified URL.",
schema={
"type": "object",
"properties": {
"url": {"type": "string", "description": "The URL to navigate to"},
"tab_id": {"type": "integer", "description": "The Firefox Tab ID to navigate"},
"window_id": {"type": "integer", "description": "The Firefox Window ID containing the tab to navigate"},
},
"required": ["url"],
},
handler=navigate_to_handler
),
Tool(
name="open_new_tab",
description="Opens a new tab with the specified URL.",
schema={
"type": "object",
"properties": {
"url": {"type": "string", "description": "The URL to open in the new tab"},
"window_id": {"type": "integer", "description": "The Firefox Window ID to open the new tab in"},
},
"required": ["url"],
},
handler=open_new_tab_handler
),
Tool(
name="get_current_url",
description="Returns the current URL of the specified tab or the active tab.",
schema={
"type": "object",
"properties": {
"tab_id": {"type": "integer", "description": "The Firefox Tab ID to get the URL from"},
"window_id": {"type": "integer", "description": "The Firefox Window ID containing the tab to get the URL from"},
},
"required": [],
},
handler=get_current_url_handler
),
Tool(
name="get_page_cookies",
description="Retrieves cookies for the specified tab or current domain.",
schema={
"type": "object",
"properties": {
"tab_id": {"type": "integer", "description": "The Firefox Tab ID to get cookies from"},
"window_id": {"type": "integer", "description": "The Firefox Window ID containing the tab to get cookies from"},
},
"required": [],
},
handler=get_page_cookies_handler
),
]