Repository navigation
feat(integrations): add Grok Build integration + Experimental ACP #2772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
41f3557
feat(integrations): add Grok Build integration and related docs
antonvishal 304c403
fix(integrations): harden Grok ACP lifecycle
antonvishal a3743c2
Merge origin/main into acp+grok-build
antonvishal 88adff8
docs(integrations): list grok-build in the package overview
antonvishal 8b20f74
Merge remote-tracking branch 'upstream/main' into acp+grok-build
antonvishal ef54737
Merge remote-tracking branch 'upstream/main' into acp+grok-build
antonvishal 6219756
fix(integrations): address ACP review feedback
antonvishal 7744b73
Merge origin/main into ACP and Grok Build integration
antonvishal 46a63e6
feat(integrations): add Grok Build harness
antonvishal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| --- | ||
| title: "Grok Build" | ||
| description: "Give Grok Build persistent Stagehand browser tools through its CLI and project MCP configuration." | ||
| --- | ||
|
|
||
| Grok Build can load the Stagehand facade as a project MCP server. The facade owns one persistent browser and exposes `run`, `snapshot`, and `screenshot` for the full CLI run. The integration uses Grok's headless streaming output directly. | ||
|
|
||
| <Note> | ||
| Stagehand ships this experimental integration from the repository rather than publishing it as a standalone adapter. | ||
| </Note> | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Node.js 24 or newer | ||
| - pnpm 11.10.0 | ||
| - The Grok Build CLI and either `XAI_API_KEY` or an existing `grok login` | ||
| - Google Chrome for local browser mode | ||
|
|
||
| ## Quickstart | ||
|
|
||
| <Steps> | ||
| <Step title="Clone and build Stagehand"> | ||
| ```bash | ||
| git clone https://github.com/browserbase/stagehand.git | ||
| cd stagehand | ||
| pnpm install --frozen-lockfile | ||
| pnpm exec turbo run build --filter @browserbasehq/stagehand-integrations | ||
| ``` | ||
| </Step> | ||
| <Step title="Install and authenticate Grok Build"> | ||
| ```bash | ||
| npm install --global @xai-official/grok | ||
| grok login | ||
| # or: export XAI_API_KEY="your-xai-api-key" | ||
| ``` | ||
| </Step> | ||
| <Step title="Configure the MCP server"> | ||
| Copy `packages/integrations/grok-build/.grok/config.toml` into the project where Grok will run. Replace the absolute facade path and browser credentials. | ||
| </Step> | ||
| <Step title="Run a browser task"> | ||
| ```bash | ||
| cd packages/integrations/grok-build | ||
| grok -p \ | ||
| --output-format streaming-json \ | ||
| --always-approve \ | ||
| --tools search_tool,use_tool \ | ||
| --disallowed-tools Agent \ | ||
| --no-plan \ | ||
| --no-subagents \ | ||
| --disable-web-search \ | ||
| "Open https://example.com, snapshot it, and report the heading." | ||
| ``` | ||
| </Step> | ||
| </Steps> | ||
|
|
||
| ## Eval harness | ||
|
|
||
| The registered `grok_build` harness follows the same CLI pattern as the Cursor harness. It creates an isolated temporary home and workspace, copies cached Grok authentication only when no API key is present, writes the selected Stagehand MCP mount to project configuration, runs `grok -p --output-format streaming-json`, and maps native tool and usage events into the shared verifier trajectory. | ||
|
|
||
| ```bash | ||
| evals run b:webvoyager \ | ||
| --harness grok_build \ | ||
| --tool stagehand_facade \ | ||
| -l 1 -t 1 -e browserbase | ||
| ``` | ||
|
|
||
| | Variable | Purpose | | ||
| | --- | --- | | ||
| | `XAI_API_KEY` | Grok credential. The Stagehand MCP server receives only its own configured environment. | | ||
| | `GROK_HOME` | Source for cached `auth.json` when `XAI_API_KEY` is unset. | | ||
| | `EVAL_GROK_BUILD_PATH` | Optional path to the `grok` binary for eval runs. | | ||
| | `EVAL_GROK_BUILD_MAX_TURNS` | Grok turn limit. Defaults to 50. | | ||
| | `EVAL_GROK_BUILD_SANDBOX` | Optional Grok sandbox profile. | | ||
| | `STAGEHAND_BROWSER` | `local` or `browserbase`. | | ||
| | `BROWSERBASE_API_KEY` | Required for Browserbase. | | ||
|
|
||
| The harness supports `stagehand_facade`, `playwright_mcp`, and `chrome_devtools_mcp`. It restricts Grok to its MCP discovery and invocation tools, disables subagents, plan mode, and web search, and asks Grok not to edit repository files. The temporary Grok home disables compatibility MCP imports, memory, and background leader reuse so user configuration cannot add unrelated tools. | ||
|
|
||
| <Warning> | ||
| `run` executes model-authored JavaScript in the browser. Use Browserbase for untrusted tasks and review the [integration security boundary](/v4/integrations/overview#security-boundary). | ||
| </Warning> | ||
|
|
||
| <Card title="Grok Build integration source" icon="github" href="https://github.com/browserbase/stagehand/tree/main/packages/integrations/grok-build"> | ||
| Open the CLI configuration and eval harness instructions. | ||
| </Card> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| import { | ||
| buildGrokBuildTranscript, | ||
| extractGrokBuildToolCall, | ||
| runGrokBuildSession, | ||
| stringifyError, | ||
| type GrokBuildProcessRunner, | ||
| type GrokBuildTokenUsage, | ||
| } from "@browserbasehq/stagehand-integrations-grok-build-sdk"; | ||
| import type { AvailableModel } from "stagehand-v3"; | ||
| import type { EvalLogger } from "../logger.js"; | ||
| import type { ExternalHarnessTaskPlan } from "./externalHarnessPlan.js"; | ||
| import type { PreparedGrokBuildToolAdapter } from "./grokBuildToolAdapter.js"; | ||
| import { grokBuildAdapter } from "./harnesses/grokBuildAdapter.js"; | ||
| import { | ||
| buildExternalHarnessPrompt, | ||
| metricValue, | ||
| parseEvalResult, | ||
| runExternalHarnessTask, | ||
| type ExternalHarnessToolAdapterLike, | ||
| type MetricValue, | ||
| type ParsedEvalResult, | ||
| } from "./harnesses/externalRunner.js"; | ||
| import type { TaskResult } from "./types.js"; | ||
| import type { ExternalHarnessVerifierConfig } from "./verifierAdapter.js"; | ||
|
|
||
| export type { GrokBuildProcessRunner } from "@browserbasehq/stagehand-integrations-grok-build-sdk"; | ||
|
|
||
| export interface GrokBuildRunnerInput { | ||
| plan: ExternalHarnessTaskPlan; | ||
| model: AvailableModel; | ||
| logger: EvalLogger; | ||
| toolAdapter?: PreparedGrokBuildToolAdapter; | ||
| signal?: AbortSignal; | ||
| runProcess?: GrokBuildProcessRunner; | ||
| verifier?: ExternalHarnessVerifierConfig; | ||
| } | ||
|
|
||
| export interface ParsedGrokBuildResult extends ParsedEvalResult {} | ||
|
|
||
| const MCP_ONLY_LINE = | ||
| "Your only browser access is the MCP server configured in this workspace. Never launch a browser yourself or run shell commands to browse."; | ||
|
|
||
| function composeGrokBuildToolInstructions(toolInstructions?: string): string { | ||
| return [ | ||
| toolInstructions ?? "Use the available browser tools to complete the task.", | ||
| MCP_ONLY_LINE, | ||
| "Do not edit repository files.", | ||
| ].join("\n"); | ||
| } | ||
|
|
||
| export function buildGrokBuildPrompt( | ||
| plan: ExternalHarnessTaskPlan, | ||
| toolInstructions?: string, | ||
| ): string { | ||
| return buildExternalHarnessPrompt({ | ||
| plan, | ||
| toolInstructions: composeGrokBuildToolInstructions(toolInstructions), | ||
| resultContract: "marker", | ||
| }); | ||
| } | ||
|
|
||
| export function parseGrokBuildResult(raw: string): ParsedGrokBuildResult { | ||
| return parseEvalResult(raw); | ||
| } | ||
|
|
||
| export async function runGrokBuildAgent({ | ||
| plan, | ||
| model, | ||
| logger, | ||
| toolAdapter, | ||
| signal, | ||
| runProcess, | ||
| verifier, | ||
| }: GrokBuildRunnerInput): Promise<TaskResult> { | ||
| const adapterLike: ExternalHarnessToolAdapterLike = { | ||
| promptInstructions: composeGrokBuildToolInstructions(toolAdapter?.promptInstructions), | ||
| captureEvidence: toolAdapter?.captureEvidence, | ||
| drainStepObservations: toolAdapter?.drainStepObservations, | ||
| observedToolMatcher: toolAdapter?.observedToolMatcher, | ||
| }; | ||
| return runExternalHarnessTask({ | ||
| harness: "grok_build", | ||
| plan, | ||
| logger, | ||
| toolAdapter: adapterLike, | ||
| verifier, | ||
| resultContract: "marker", | ||
| fallbackErrorMessage: "Grok Build did not report success", | ||
| parseResult: parseGrokBuildResult, | ||
| runSession: async (prompt) => { | ||
| const sessionResult = await runGrokBuildSession({ | ||
| prompt, | ||
| model, | ||
| logger, | ||
| signal, | ||
| runProcess, | ||
| session: { | ||
| ...(toolAdapter?.cwd && { cwd: toolAdapter.cwd }), | ||
| ...(toolAdapter?.env && { env: toolAdapter.env }), | ||
| ...(process.env.EVAL_GROK_BUILD_PATH && { | ||
| binaryPath: process.env.EVAL_GROK_BUILD_PATH, | ||
| }), | ||
| maxTurns: readGrokBuildMaxTurns(), | ||
| ...(process.env.EVAL_GROK_BUILD_SANDBOX && { | ||
| sandbox: process.env.EVAL_GROK_BUILD_SANDBOX, | ||
| }), | ||
| }, | ||
| onToolResult: toolAdapter?.onToolResult | ||
| ? (name) => toolAdapter.onToolResult!(name) | ||
| : undefined, | ||
| }); | ||
| const usage = sessionResult.tokenUsage; | ||
| return { | ||
| raw: sessionResult, | ||
| resultText: sessionResult.resultText, | ||
| transcriptText: buildGrokBuildTranscript(sessionResult.events), | ||
| iterationError: sessionResult.iterationError, | ||
| status: sessionResult.status, | ||
| stopReason: | ||
| sessionResult.stopReason || | ||
| (sessionResult.status === "sdk_error" | ||
| ? stringifyError(sessionResult.iterationError) || undefined | ||
| : undefined), | ||
| usage: { | ||
| inputTokens: usage.inputTokens, | ||
| outputTokens: usage.outputTokens, | ||
| totalTokens: usage.totalTokens, | ||
| ...(usage.reported && { | ||
| cachedInputTokens: usage.cachedInputTokens, | ||
| cacheCreationInputTokens: usage.cacheCreationInputTokens, | ||
| reasoningOutputTokens: usage.reasoningOutputTokens, | ||
| }), | ||
| }, | ||
| costUsd: sessionResult.costUsd, | ||
| metrics: buildGrokBuildMetrics(usage, sessionResult.endEvent, sessionResult.events), | ||
| }; | ||
| }, | ||
| toTrajectory: ( | ||
| { raw, parsed, finalObservation, stepObservations, observedToolName, status }, | ||
| taskSpec, | ||
| ) => | ||
| grokBuildAdapter.fromHarnessResult( | ||
| { | ||
| events: raw.events, | ||
| ...(finalObservation && { finalObservation }), | ||
| ...(stepObservations?.length && { stepObservations }), | ||
| ...(observedToolName && { observedToolName }), | ||
| finalAnswer: parsed.finalAnswer ?? raw.resultText, | ||
| status, | ||
| usage: { | ||
| input_tokens: raw.tokenUsage.inputTokens, | ||
| output_tokens: raw.tokenUsage.outputTokens, | ||
| cached_input_tokens: raw.tokenUsage.cachedInputTokens, | ||
| reasoning_tokens: raw.tokenUsage.reasoningOutputTokens, | ||
| }, | ||
| }, | ||
| taskSpec, | ||
| ), | ||
| }); | ||
| } | ||
|
|
||
| export function readGrokBuildMaxTurns(): number { | ||
| for (const key of ["EVAL_GROK_BUILD_MAX_TURNS", "AGENT_EVAL_MAX_STEPS"]) { | ||
| const parsed = Number.parseInt(process.env[key] ?? "", 10); | ||
| if (Number.isFinite(parsed) && parsed > 0) return parsed; | ||
| } | ||
| return 50; | ||
| } | ||
|
|
||
| function buildGrokBuildMetrics( | ||
| usage: GrokBuildTokenUsage, | ||
| endEvent: Record<string, unknown> | undefined, | ||
| events: Array<Record<string, unknown>>, | ||
| ): Record<string, MetricValue> { | ||
| const toolSteps = events.filter( | ||
| (event) => extractGrokBuildToolCall(event)?.subtype === "completed", | ||
| ).length; | ||
| return { | ||
| grok_build_input_tokens: metricValue(usage.inputTokens), | ||
| grok_build_output_tokens: metricValue(usage.outputTokens), | ||
| grok_build_total_tokens: metricValue(usage.totalTokens), | ||
| grok_build_cached_input_tokens: metricValue(usage.cachedInputTokens), | ||
| grok_build_reasoning_tokens: metricValue(usage.reasoningOutputTokens), | ||
| grok_build_num_turns: metricValue(endEvent?.num_turns), | ||
| grok_build_tool_steps: metricValue(toolSteps), | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.