import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { listTabsSchema, newTabSchema, closeTabSchema, switchTabSchema } from '../schemas.js';
import { createTab, closeTab, listTabs, switchTab, getActiveTabId } from '../tabs.js';
import { handleResult, ok } from '../errors.js';
/**
* Register tab management tools
*/
export function registerTabTools(server: McpServer): void {
// List all tabs
server.tool(
'list_tabs',
'List all open browser tabs with their IDs, URLs, and titles',
listTabsSchema.shape,
async () => {
const tabs = await listTabs();
return handleResult(ok({ tabs }));
}
);
// Create new tab
server.tool(
'new_tab',
'Open a new browser tab, optionally navigating to a URL. The new tab becomes active.',
newTabSchema.shape,
async ({ url }) => {
const result = await createTab(url);
return handleResult(result);
}
);
// Close tab
server.tool(
'close_tab',
'Close a browser tab. If no tabId provided, closes the active tab.',
closeTabSchema.shape,
async ({ tabId }) => {
const targetId = tabId ?? getActiveTabId();
const result = await closeTab(tabId);
if (result.success) {
return handleResult(ok({
closedTabId: targetId,
newActiveTabId: getActiveTabId(),
}));
}
return handleResult(result);
}
);
// Switch tab
server.tool(
'switch_tab',
'Switch to a different tab, making it the active tab for subsequent operations.',
switchTabSchema.shape,
async ({ tabId }) => {
const result = await switchTab(tabId);
return handleResult(result);
}
);
}