Skip to main content
Glama
kazuph

MCP Browser Tabs Server

by kazuph

close_tab_by_id

Close a specific Chrome tab by its unique ID, unaffected by tab reordering, window changes, or index shifting. Use the tab ID from the get_tabs output for precise control.

Instructions

🔥 PREFERRED METHOD: Close a specific tab in Google Chrome using its unique tab ID. IMMUNE to tab reordering, window changes, and index shifting. Extract the Tab ID from [Tab ID: 1234567890] in get_tabs output. Example: if tab shows '[Tab ID: 1594670961]', use tabId: 1594670961

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tabIdYesThe exact Tab ID number from [Tab ID: xxxxx] in get_tabs output - NOT the display number

Implementation Reference

  • Core handler function that executes the logic to close a Chrome tab by its unique ID using AppleScript via osascript.
    async function closeChromeTabById(tabId: number): Promise<void> {
      const script = `
        tell application "Google Chrome"
          set targetTabID to "${tabId}"
          set tabFound to false
          
          repeat with w in (every window)
            repeat with t in (every tab of w)
              if (id of t) as string = targetTabID then
                close t
                set tabFound to true
                exit repeat
              end if
            end repeat
            if tabFound then exit repeat
          end repeat
          
          if not tabFound then
            error "Tab with ID " & targetTabID & " not found"
          end if
        end tell
      `;
    
      try {
        await execAsync(`osascript -e '${script}'`);
      } catch (error) {
        throw new Error(
          `Failed to close Chrome tab with ID ${tabId}: ${error instanceof Error ? error.message : String(error)}`
        );
      }
    }
  • src/index.ts:241-248 (registration)
    Registration of the 'close_tab_by_id' tool in the tools list, including name, description, and input schema.
      name: "close_tab_by_id",
      description: "🔥 PREFERRED METHOD: Close a specific tab in Google Chrome using its unique tab ID. IMMUNE to tab reordering, window changes, and index shifting. Extract the Tab ID from [Tab ID: 1234567890] in get_tabs output. Example: if tab shows '[Tab ID: 1594670961]', use tabId: 1594670961",
      inputSchema: zodToJsonSchema(
        z.object({
          tabId: z.number().int().positive().describe("The exact Tab ID number from [Tab ID: xxxxx] in get_tabs output - NOT the display number"),
        })
      ),
    },
  • Dispatch handler in the tool call request handler that invokes closeChromeTabById with the provided tabId argument.
    if (name === "close_tab_by_id") {
      const { tabId } = request.params.arguments as { tabId: number };
      await closeChromeTabById(tabId);
      return {
        content: [
          {
            type: "text",
            text: `✅ Successfully closed tab with ID ${tabId}`,
          },
        ],
      };
    }
  • Zod schema definition for the input parameters (tabId) of the close_tab_by_id tool.
    inputSchema: zodToJsonSchema(
      z.object({
        tabId: z.number().int().positive().describe("The exact Tab ID number from [Tab ID: xxxxx] in get_tabs output - NOT the display number"),
      })
    ),
Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/kazuph/mcp-browser-tabs'

If you have feedback or need assistance with the MCP directory API, please join our Discord server