Skip to main content
Glama
MesuterPikin

Browserbase MCP Server

by MesuterPikin

browserbase_session_create

Create or reuse a browser session for web automation, enabling AI agents to navigate websites, extract data, and perform interactions through the Browserbase MCP Server.

Instructions

Create or reuse a Browserbase browser session and set it as active.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sessionIdNoOptional session ID to use/reuse. If not provided or invalid, a new session is created.

Implementation Reference

  • Main handler function that orchestrates session creation or reuse via SessionManager, initializes Browserbase SDK for debug URLs, and returns session view and debugger links.
    async function handleCreateSession(
      context: Context,
      params: CreateSessionInput,
    ): Promise<ToolResult> {
      const action = async (): Promise<ToolActionResult> => {
        try {
          const sessionManager = context.getSessionManager();
          const config = context.config; // Get config from context
          let targetSessionId: string;
    
          // Session ID Strategy: Use raw sessionId for both internal tracking and Browserbase operations
          // Default session uses generated ID with timestamp/UUID, user sessions use provided ID as-is
          if (params.sessionId) {
            targetSessionId = params.sessionId;
            process.stderr.write(
              `[tool.createSession] Attempting to create/assign session with specified ID: ${targetSessionId}\n`,
            );
          } else {
            targetSessionId = sessionManager.getDefaultSessionId();
          }
    
          let session: BrowserSession;
          const defaultSessionId = sessionManager.getDefaultSessionId();
          if (targetSessionId === defaultSessionId) {
            session = await sessionManager.ensureDefaultSessionInternal(config);
          } else {
            // When user provides a sessionId, we want to resume that Browserbase session
            // Note: targetSessionId is used for internal tracking in SessionManager
            // while params.sessionId is the Browserbase session ID to resume
            session = await sessionManager.createNewBrowserSession(
              targetSessionId, // Internal session ID for tracking
              config,
              params.sessionId, // Browserbase session ID to resume
            );
          }
    
          if (
            !session ||
            !session.page ||
            !session.sessionId ||
            !session.stagehand
          ) {
            throw new Error(
              `SessionManager failed to return a valid session object with actualSessionId for ID: ${targetSessionId}`,
            );
          }
    
          // Note: No need to set context.currentSessionId - SessionManager handles this
          // and context.currentSessionId is a getter that delegates to SessionManager
          const bb = new Browserbase({
            apiKey: config.browserbaseApiKey,
          });
    
          const browserbaseSessionId = session.stagehand.browserbaseSessionId;
          if (!browserbaseSessionId) {
            throw new Error(
              "Browserbase session ID not found in Stagehand instance",
            );
          }
          const debugUrl = (await bb.sessions.debug(browserbaseSessionId))
            .debuggerFullscreenUrl;
    
          return {
            content: [
              {
                type: "text",
                text: `Browserbase Live Session View URL: https://www.browserbase.com/sessions/${browserbaseSessionId}`,
              },
              {
                type: "text",
                text: `Browserbase Live Debugger URL: ${debugUrl}`,
              },
              createUIResource({
                uri: "ui://analytics-dashboard/main",
                content: { type: "externalUrl", iframeUrl: debugUrl },
                encoding: "text",
              }) as unknown as TextContent,
            ],
          };
        } catch (error: unknown) {
          const errorMessage =
            error instanceof Error ? error.message : String(error);
          process.stderr.write(
            `[tool.createSession] Action failed: ${errorMessage}\n`,
          );
          // Re-throw to be caught by Context.run's error handling for actions
          throw new Error(`Failed to create Browserbase session: ${errorMessage}`);
        }
      };
    
      // Return the ToolResult structure expected by Context.run
      return {
        action: action,
        waitForNetwork: false,
      };
    }
  • Zod schema definition for the tool's input parameters and the ToolSchema object defining name, description, and inputSchema.
    // --- Tool: Create Session ---
    const CreateSessionInputSchema = z.object({
      // Keep sessionId optional
      sessionId: z
        .string()
        .optional()
        .describe(
          "Optional session ID to use/reuse. If not provided or invalid, a new session is created.",
        ),
    });
    type CreateSessionInput = z.infer<typeof CreateSessionInputSchema>;
    
    const createSessionSchema: ToolSchema<typeof CreateSessionInputSchema> = {
      name: "browserbase_session_create",
      description:
        "Create or reuse a Browserbase browser session and set it as active.",
      inputSchema: CreateSessionInputSchema,
    };
  • src/index.ts:168-198 (registration)
    Registration loop that iterates over all tools from src/tools/index.ts TOOLS array and registers each with the MCP server using server.tool(), wrapping the tool handler via context.run.
    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}`,
        );
      }
    });
  • Aggregation of all tools into the TOOLS export array, importing and including sessionTools which contains browserbase_session_create.
    export const TOOLS = [
      ...sessionTools,
      navigateTool,
      actTool,
      extractTool,
      observeTool,
      screenshotTool,
      getUrlTool,
      agentTool,
    ];
  • Helper function to create and initialize Stagehand instance used by SessionManager for browser sessions, configuring Browserbase parameters.
    export const createStagehandInstance = async (
      config: Config,
      params: CreateSessionParams = {},
      sessionId: string,
    ): Promise<Stagehand> => {
      const apiKey = params.apiKey || config.browserbaseApiKey;
      const projectId = params.projectId || config.browserbaseProjectId;
    
      if (!apiKey || !projectId) {
        throw new Error("Browserbase API Key and Project ID are required");
      }
    
      const modelName = params.modelName || config.modelName || "gemini-2.0-flash";
      const modelApiKey =
        config.modelApiKey ||
        process.env.GEMINI_API_KEY ||
        process.env.GOOGLE_API_KEY;
    
      const stagehand = new Stagehand({
        env: "BROWSERBASE",
        apiKey,
        projectId,
        model: modelApiKey
          ? {
              apiKey: modelApiKey,
              modelName: modelName,
            }
          : modelName,
        ...(params.browserbaseSessionID && {
          browserbaseSessionID: params.browserbaseSessionID,
        }),
        experimental: config.experimental ?? false,
        browserbaseSessionCreateParams: {
          projectId,
          proxies: config.proxies,
          keepAlive: config.keepAlive ?? false,
          browserSettings: {
            viewport: {
              width: config.viewPort?.browserWidth ?? 1288,
              height: config.viewPort?.browserHeight ?? 711,
            },
            context: config.context?.contextId
              ? {
                  id: config.context?.contextId,
                  persist: config.context?.persist ?? true,
                }
              : undefined,
            advancedStealth: config.advancedStealth ?? undefined,
          },
          userMetadata: {
            mcp: "true",
          },
        },
        logger: (logLine) => {
          console.error(`Stagehand[${sessionId}]: ${logLine.message}`);
        },
      });
    
      await stagehand.init();
      return stagehand;
    };
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It mentions session creation/reuse and activation but omits critical details like authentication requirements, rate limits, session lifecycle, or error handling. This is inadequate for a tool that likely involves resource allocation.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core functionality without unnecessary words. Every part of the sentence contributes directly to understanding the tool's purpose.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of session management, lack of annotations, and no output schema, the description is insufficient. It fails to explain what a 'session' entails, how activation works, or what the tool returns, leaving significant gaps for an AI agent to operate effectively.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents the 'sessionId' parameter fully. The description adds no additional meaning beyond what the schema provides, such as examples or edge cases, but does not contradict it either.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Create or reuse') and resource ('Browserbase browser session'), and specifies the outcome ('set it as active'). However, it does not explicitly differentiate from sibling tools like 'browserbase_session_close', which handles session termination, leaving some ambiguity in scope.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives, such as whether to prefer creating a new session over reusing an existing one, or how it relates to other session management tools. It lacks context on prerequisites or typical workflows.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

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

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