Skip to main content
Glama

browserbase_session_close

Close the current browser session to properly terminate browser processes and stop session recording, ensuring clean resource cleanup.

Instructions

Closes the current Browserbase session by properly shutting down the Stagehand instance, which handles browser cleanup and terminates the session recording.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • Implements the core logic for the 'browserbase_session_close' tool. Retrieves the current session, closes the Stagehand instance if available, performs cleanup, resets the session context to default, and returns success or informational messages with replay URLs.
    async function handleCloseSession(context: Context): Promise<ToolResult> { const action = async (): Promise<ToolActionResult> => { // Store the current session ID before it's potentially changed. const previousSessionId = context.currentSessionId; let stagehandClosedSuccessfully = false; let stagehandCloseErrorMessage = ""; // Step 1: Attempt to get the session and close Stagehand let browserbaseSessionId: string | undefined; try { const session = await getSession( previousSessionId, context.config, false, ); if (session && session.stagehand) { // Store the actual Browserbase session ID for the replay URL browserbaseSessionId = session.sessionId; process.stderr.write( `[tool.closeSession] Attempting to close Stagehand for session: ${previousSessionId || "default"} (Browserbase ID: ${browserbaseSessionId})`, ); // Use Stagehand's close method which handles browser cleanup properly await session.stagehand.close(); stagehandClosedSuccessfully = true; process.stderr.write( `[tool.closeSession] Stagehand and browser connection for session (${previousSessionId}) closed successfully.`, ); // Clean up the session from tracking await cleanupSession(previousSessionId); if (browserbaseSessionId) { process.stderr.write( `[tool.closeSession] View session replay at https://www.browserbase.com/sessions/${browserbaseSessionId}`, ); } } else { process.stderr.write( `[tool.closeSession] No Stagehand instance found for session: ${previousSessionId || "default/unknown"}`, ); } } catch (error: unknown) { stagehandCloseErrorMessage = error instanceof Error ? error.message : String(error); process.stderr.write( `[tool.closeSession] Error retrieving or closing Stagehand (session ID was ${previousSessionId || "default/unknown"}): ${stagehandCloseErrorMessage}`, ); } // Step 2: Always reset the context's current session ID to default const oldContextSessionId = context.currentSessionId; context.currentSessionId = defaultSessionId; process.stderr.write( `[tool.closeSession] Session context reset to default. Previous context session ID was ${oldContextSessionId || "default/unknown"}.`, ); // Step 3: Determine the result message if (stagehandCloseErrorMessage && !stagehandClosedSuccessfully) { throw new Error( `Failed to close the Stagehand session (session ID in context was ${previousSessionId || "default/unknown"}). Error: ${stagehandCloseErrorMessage}. Session context has been reset to default.`, ); } if (stagehandClosedSuccessfully) { let successMessage = `Browserbase session (${previousSessionId || "default"}) closed successfully via Stagehand. Context reset to default.`; if (browserbaseSessionId && previousSessionId !== defaultSessionId) { successMessage += ` View replay at https://www.browserbase.com/sessions/${browserbaseSessionId}`; } return { content: [{ type: "text", text: successMessage }] }; } // No Stagehand instance was found let infoMessage = "No active Stagehand session found to close. Session context has been reset to default."; if (previousSessionId && previousSessionId !== defaultSessionId) { infoMessage = `No active Stagehand session found for session ID '${previousSessionId}'. The context has been reset to default.`; } return { content: [{ type: "text", text: infoMessage }] }; }; return { action: action, waitForNetwork: false, }; }
  • Defines the input schema (empty object) and tool schema for 'browserbase_session_close', including name, description, and input schema reference.
    const CloseSessionInputSchema = z.object({}); const closeSessionSchema: ToolSchema<typeof CloseSessionInputSchema> = { name: "browserbase_session_close", description: "Closes the current Browserbase session by properly shutting down the Stagehand instance, which handles browser cleanup and terminates the session recording.", inputSchema: CloseSessionInputSchema, };
  • Registers the tool by creating the Tool object with schema and handler, which is then exported as part of sessionTools for inclusion in the main TOOLS array.
    const closeSessionTool: Tool<typeof CloseSessionInputSchema> = { capability: "core", schema: closeSessionSchema, handle: handleCloseSession, };
  • Aggregates all tools including sessionTools (which contains browserbase_session_close) into the main TOOLS export used for MCP server registration.
    export const TOOLS = [ ...multiSessionTools, ...sessionTools, navigateTool, actTool, extractTool, observeTool, screenshotTool, getUrlTool, ];

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/vaibhavAtlys/mcp-server-browserbase'

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