Skip to main content
Glama

close-browser

Closes a specific browser instance by its ID to manage resources during Vite development sessions.

Instructions

Closes a specific browser instance

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contextIdYesID of the browser to close

Implementation Reference

  • The main handler function for the 'close-browser' MCP tool. It takes a contextId, calls contextManager.closeContext(contextId), and returns a formatted success or error response.
    async ({ contextId }) => {
      try {
        const result = await contextManager.closeContext(contextId);
    
        if (result.success) {
          return {
            content: [
              {
                type: 'text',
                text: `Browser closed successfully: ${contextId}`
              }
            ]
          };
        } else {
          return {
            content: [
              {
                type: 'text',
                text: `Failed to close browser: ${result.error}`
              }
            ],
            isError: true
          };
        }
      } catch (error) {
        const errorMessage = error instanceof Error ? error.message : String(error);
        Logger.error(`Failed to close browser ${contextId}:`, error);
        return {
          content: [
            {
              type: 'text',
              text: `Failed to close browser: ${errorMessage}`
            }
          ],
          isError: true
        };
      }
    }
  • Zod input schema for the close-browser tool, requiring a string contextId.
    {
      contextId: z.string().describe('ID of the browser to close')
    },
  • Registers the 'close-browser' tool using server.tool(), including description, schema, and inline handler.
    server.tool(
      'close-browser',
      'Closes a specific browser instance',
      {
        contextId: z.string().describe('ID of the browser to close')
      },
      async ({ contextId }) => {
        try {
          const result = await contextManager.closeContext(contextId);
    
          if (result.success) {
            return {
              content: [
                {
                  type: 'text',
                  text: `Browser closed successfully: ${contextId}`
                }
              ]
            };
          } else {
            return {
              content: [
                {
                  type: 'text',
                  text: `Failed to close browser: ${result.error}`
                }
              ],
              isError: true
            };
          }
        } catch (error) {
          const errorMessage = error instanceof Error ? error.message : String(error);
          Logger.error(`Failed to close browser ${contextId}:`, error);
          return {
            content: [
              {
                type: 'text',
                text: `Failed to close browser: ${errorMessage}`
              }
            ],
            isError: true
          };
        }
      }
    );
  • Core helper method in ContextManager that performs the actual browser context closure, map cleanup, shared browser management, and database deactivation.
    async closeContext(contextId: string): Promise<ContextOperationResult> {
      try {
        const contextInstance = this.contexts.get(contextId);
        if (!contextInstance) {
          return {
            success: false,
            error: `Context '${contextId}' not found`
          };
        }
    
        Logger.info(`Closing context: ${contextId}`);
    
        // Close context
        await contextInstance.context.close();
    
        // Remove from map
        this.contexts.delete(contextId);
        
        // Update shared browser context count
        if (this.sharedBrowser) {
          this.sharedBrowser.contextCount--;
          
          // Close shared browser if no contexts remain
          if (this.sharedBrowser.contextCount === 0) {
            Logger.info('Closing shared browser - no contexts remaining');
            await this.sharedBrowser.browser.close();
            this.sharedBrowser = null;
          }
        }
    
        // Update database to mark as inactive
        const db = getScreenshotDB();
        db.deactivateBrowser(contextId); // TODO: rename to deactivateContext
    
        Logger.info(`Context closed successfully: ${contextId}`);
    
        return {
          success: true,
          contextId
        };
    
      } catch (error) {
        Logger.error(`Failed to close context ${contextId}:`, error);
        return {
          success: false,
          error: error instanceof Error ? error.message : String(error)
        };
      }
    }
  • src/index.ts:82-82 (registration)
    Top-level call to registerBrowserManagerTools which sets up all browser manager tools including 'close-browser'.
    const contextManager = registerBrowserManagerTools(server);

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/ESnark/blowback'

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