Requiring an explicit tab_id forced every navigate or close through a preceding tabs_list call, which costs an MCP client a full round trip just to learn the ID the browser already considers current. navigate and tabs_close now resolve the active tab when tab_id is omitted, matching the screenshot tool. Resolution is explicit rather than forwarding None into the SDK, so the acting tool knows which tab it touched; tabs_close reports it, since closing the wrong tab is not recoverable. This stays in the MCP layer: the SDK and CLI signatures are unchanged.
17 lines
656 B
Python
17 lines
656 B
Python
"""Tab targeting for the MCP surface.
|
|
|
|
MCP callers pay a full round trip for every extra tool call, so tools that act
|
|
on a tab accept an optional ``tab_id`` and fall back to the browser's current
|
|
active tab. Resolution happens here rather than by forwarding ``None`` into the
|
|
SDK, so the acting tool always knows which tab it touched and can report it.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from browser_cli import BrowserCLI
|
|
|
|
def resolve_tab_id(client: BrowserCLI, tab_id: int | None) -> int:
|
|
"""Return *tab_id*, or the ID of the currently active tab when it is ``None``."""
|
|
if tab_id is not None:
|
|
return tab_id
|
|
return client.tabs.active().id
|