browserbase_session_close
Close the current cloud browser session and reset the active context to free up resources and prepare for new browsing tasks.
Instructions
Close the current Browserbase session and reset the active context.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/tools/session.ts:144-218 (handler)The handler function that performs the session close operation: retrieves current session, cleans up via SessionManager, handles errors, resets context to default, and returns appropriate text content with replay URL if available.async function handleCloseSession(context: Context): Promise<ToolResult> { const action = async (): Promise<ToolActionResult> => { // Store the current session ID before cleanup const previousSessionId = context.currentSessionId; let cleanupSuccessful = false; let cleanupErrorMessage = ""; // Step 1: Get session info before cleanup let browserbaseSessionId: string | undefined; const sessionManager = context.getSessionManager(); try { const session = await sessionManager.getSession( previousSessionId, context.config, false, ); if (session && session.stagehand) { // Store the actual Browserbase session ID for the replay URL browserbaseSessionId = session.sessionId; // cleanupSession handles both closing Stagehand and cleanup (idempotent) await sessionManager.cleanupSession(previousSessionId); cleanupSuccessful = true; } else { process.stderr.write( `[tool.closeSession] No session found for ID: ${previousSessionId || "default/unknown"}\n`, ); } } catch (error: unknown) { cleanupErrorMessage = error instanceof Error ? error.message : String(error); process.stderr.write( `[tool.closeSession] Error cleaning up session (ID was ${previousSessionId || "default/unknown"}): ${cleanupErrorMessage}\n`, ); } // Step 2: SessionManager automatically resets to default on cleanup // Context.currentSessionId getter will reflect the new active session const oldContextSessionId = previousSessionId; process.stderr.write( `[tool.closeSession] Session context reset to default. Previous context session ID was ${oldContextSessionId || "default/unknown"}.\n`, ); // Step 3: Determine the result message const defaultSessionId = sessionManager.getDefaultSessionId(); if (cleanupErrorMessage && !cleanupSuccessful) { throw new Error( `Failed to cleanup session (session ID was ${previousSessionId || "default/unknown"}). Error: ${cleanupErrorMessage}. Session context has been reset to default.`, ); } if (cleanupSuccessful) { let successMessage = `Browserbase session (${previousSessionId || "default"}) closed successfully. 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 session was found let infoMessage = "No active session found to close. Session context has been reset to default."; if (previousSessionId && previousSessionId !== defaultSessionId) { infoMessage = `No active session found for session ID '${previousSessionId}'. The context has been reset to default.`; } return { content: [{ type: "text", text: infoMessage }] }; }; return { action: action, waitForNetwork: false, }; }
- src/tools/session.ts:135-142 (schema)Zod schema for input (empty object) and tool schema definition including name, description, and inputSchema.const CloseSessionInputSchema = z.object({}); const closeSessionSchema: ToolSchema<typeof CloseSessionInputSchema> = { name: "browserbase_session_close", description: "Close the current Browserbase session and reset the active context.", inputSchema: CloseSessionInputSchema, };
- src/tools/session.ts:220-224 (registration)Defines the tool object combining schema and handler, and exports it as part of sessionTools array for further registration.const closeSessionTool: Tool<typeof CloseSessionInputSchema> = { capability: "core", schema: closeSessionSchema, handle: handleCloseSession, };
- src/tools/index.ts:6-27 (registration)Imports sessionTools (containing browserbase_session_close) and includes it in the TOOLS array exported for MCP server registration.import sessionTools from "./session.js"; import getUrlTool from "./url.js"; // Export individual tools export { default as navigateTool } from "./navigate.js"; export { default as actTool } from "./act.js"; export { default as extractTool } from "./extract.js"; export { default as observeTool } from "./observe.js"; export { default as screenshotTool } from "./screenshot.js"; export { default as sessionTools } from "./session.js"; export { default as getUrlTool } from "./url.js"; // Export all tools as array export const TOOLS = [ ...sessionTools, navigateTool, actTool, extractTool, observeTool, screenshotTool, getUrlTool, ];
- src/index.ts:186-216 (registration)Imports TOOLS, iterates over all tools including browserbase_session_close, and registers each to the MCP server via server.tool(), wrapping the call to context.run(tool, params) which invokes the tool's handle function.const tools: MCPToolsArray = [...TOOLS]; // Register each tool with the Smithery server tools.forEach((tool) => { if (tool.schema.inputSchema instanceof z.ZodObject) { server.tool( tool.schema.name, tool.schema.description, tool.schema.inputSchema.shape, async (params: z.infer<typeof tool.schema.inputSchema>) => { try { const result = await context.run(tool, params); return result; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); process.stderr.write( `[Smithery Error] ${new Date().toISOString()} Error running tool ${tool.schema.name}: ${errorMessage}\n`, ); throw new Error( `Failed to run tool '${tool.schema.name}': ${errorMessage}`, ); } }, ); } else { console.warn( `Tool "${tool.schema.name}" has an input schema that is not a ZodObject. Schema type: ${tool.schema.inputSchema.constructor.name}`, ); } });