Repository navigation
Add an MCP server for bb with OAuth sign-in through bb connect - #5450
Merged
SawyerHood merged 1 commit intoOct 13, 2026
Merged
SawyerHood merged 1 commit into
SawyerHood merged 1 commit into
Conversation
A new built-in plugin, bb--mcp (off by default), serves a Model Context Protocol endpoint so ChatGPT, Claude and other MCP clients can control a bb. It answers MCP 2026-07-28 and 2025-era clients statelessly and exposes three tools: search, read and run. read and run execute a script in a QuickJS sandbox whose only capability is calling the bb CLI on the server host. Remote clients reach https://<handle>.<base>/mcp through the connect gate, which accepts only an OAuth bearer token issued for that bb and its owner. The account app gains a small OAuth 2.1 authorization server (client ID metadata documents, PKCE, rotating refresh tokens), a consent page, and a Connected apps section. Approving an app grants full access. bb mcp status, bb mcp clients and bb mcp revoke manage it from the CLI. The host daemon now refuses to read non-regular files. Co-Authored-By: Claude Code <[email protected]>
SawyerHood
marked this pull request as ready for review
October 13, 2026 21:06
SawyerHood
deleted the
bb/can-you-add-support-for-an-mcp-for-bb-that-suppo-thr_tyau7z98ih
branch
October 13, 2026 21:19
SawyerHood
added a commit
that referenced
this pull request
Oct 14, 2026
## Human comments ## What was wrong `plugins/mcp/src/sandbox.test.ts` › "fails cleanly when the script exhausts its memory limit" has timed out at Vitest's 5 s default since #5450 (runs 38469661984, 38472496938, 38485481595). The sandbox's memory limit was never enforced. The Emscripten build of QuickJS cannot measure allocation sizes. `runtime.dumpMemoryUsage()` prints "malloc_usable_size unavailable" and "8.0 per block", and 20 MiB of live data is counted as 13,208 bytes. `setMemoryLimit` therefore only refuses a single allocation larger than the limit. With a 4 MiB limit the test script kept 1,935 arrays of 100,000 elements alive and grew the WASM heap from 16 MiB to its 2 GiB ceiling before a real out-of-memory error ended it. The 500 ms deadline did not stop it either. QuickJS consults the interrupt handler on the first poll and then once per 10,000 polls, and native calls such as `Array.prototype.fill` do not poll, so the handler was consulted once in those 1,935 iterations. The test only asserted `ok === false`. Where it passed, it passed because the late finish was reported as the computation limit. ## What changed `plugins/mcp/src/sandbox.ts`: - Each sandbox instance gets its own `WebAssembly.Memory` sized to the limit (initial equals maximum), so the engine cannot allocate past it. An instance is reused after a clean run and discarded after exhaustion or any error. - Running out of memory stops the script with "The script ran out of memory and was stopped. …", including when the script catches the error. - Strings copied into the sandbox are checked for room first. The QuickJS glue does not check its own allocation: a host result that did not fit was written through a null pointer, trapped inside the settle callback, escaped as an unhandled rejection, and left the script waiting for the 25 s deadline. - Settling a host call runs under a fresh deadline, and a failure there stops the script instead of escaping. - `memoryLimitBytes` below 16 MiB or above 2 GiB throws a `RangeError`, because the WASM module cannot enforce it. The memory test uses 16 MiB instead of 4. Behavior change: a script could previously use up to 2 GiB. The 64 MB default is now real, and about 5 MiB of it is interpreter overhead. The tool description in `mcp-handler.ts` and `skills/mcp/SKILL.md` now mention the 64 MB limit. Not changed: the computation deadline still overshoots for loops that spend their time in native calls on large values. Measured overruns ranged from seconds to over a minute. Enforcing it needs the interpreter on a worker thread that can be terminated, which is a separate change. ## How you verified Tests in `sandbox.test.ts`: - The memory test asserts the memory message. - New tests: the amount a script can keep scales with its limit (16 MiB and 32 MiB), a script that swallows the error is still stopped, a host result that does not fit stops the script, an out-of-range limit is refused, an unserialisable host result stops the script. - Six tests that only checked `ok === false`, or the phrase "was stopped" that every stop message shares, now assert the reason. - Against the old `sandbox.ts`, six of these fail: the old memory script ends on the computation message, and an unserialisable host result waits 25 s with an unhandled rejection. Duration of the memory test: | Machine | Before | After | |---|---|---| | Local, Node 24 | 1.80–1.91 s | 15–19 ms | | Local, Node 22 | 2.82 s | 19 ms | | 20% CPU quota (about 5.8× slower) | timed out at 5 s (ran 12.3 s) | 0.30–0.40 s | | 10% CPU quota (about 12× slower) | timed out (ran 28.2 s) | 0.40–0.70 s | At the 10% quota all 30 tests in the file pass and the slowest takes 2.5 s. Commands: - `pnpm exec turbo run test typecheck lint --filter=bb-plugin-bb--mcp` passes (125 tests). - `pnpm exec turbo run prepare:bundled --filter=bb-plugin-bb--mcp` builds, and the bundle imports. Probes through `runSandboxedScript`, not committed: - Scripts that fill the heap completely, let a pending host call settle into a full heap, receive an oversized host result or error, or replace the `ArrayBuffer` global all stop with the memory message within 75 ms, with no unhandled rejection and no teardown failure. - 200 exhausted runs keep resident memory flat. - 300 sequential trivial runs take a median of 0.40 ms each, the same as before. Not run: Windows, and a real MCP client against the app. 🤖 Generated with [Claude Code](https://claude.com/claude-code) > AGENT GENERATED Co-authored-by: Claude Code <[email protected]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Human comments
What was wrong
bb could not be controlled from ChatGPT, Claude, or any other MCP client. There was no MCP endpoint, and the bb connect gate accepted only account cookies or machine credentials, so a remote app had no way to sign in to a user's bb.
What changed
New built-in plugin
bb--mcp(plugins/mcp, off by default).@modelcontextprotocol/serverv2.search(findbbcommands),readandrun.readandrunexecute a JavaScript snippet in a QuickJS sandbox whose only capability is calling thebbCLI on the server host, so the whole CLI is reachable and several steps fit in one call.readis marked read-only so clients can run it without an approval prompt; it accepts only a pinned list of commands that change nothing. It is not a separate permission.bbcalls, 4 at a time (8 per server).bb mcp status,bb mcp clients,bb mcp revoke <grant-id>. Skill, overview,docs/configuration.mdand the plugins guide are updated.Account app (
apps/web). A small OAuth 2.1 authorization server:/oauth/authorize(consent page),/oauth/token,/oauth/revoke,/.well-known/oauth-authorization-server. Clients identify themselves with client ID metadata documents; there is no dynamic registration. PKCE is required, refresh tokens rotate, and approving an app grants full access to that bb (one scope,bb). The dashboard gains a Connected apps section, and/api/mcp/grantsbacks the CLI.Connect gate (
apps/connect). On a bb's address,/mcpaccepts only a bearer token issued for that bb and its owner, and answers anything else with an OAuth challenge. It serves the protected-resource metadata and forwards to the plugin's local route withx-bb-gate-auth: oauth; that local route returns 404 from outside. No other path changes.Connect database. Migration
0009_mcp_oauth.sqladdsoauth_grant,oauth_codeandoauth_refresh_token. No existing table changes. The gate caches the grant lookup for 30 seconds and writes once per grant; malformed codes and tokens are rejected before any query; rotated refresh tokens are pruned after 24 hours.Host daemon. File read now refuses non-regular files (reading a device such as
/dev/zerocould exhaust memory). No wire fields change, soHOST_DAEMON_PROTOCOL_VERSIONis not bumped.bb-account plugin. Its account fetch proxy lets only the MCP plugin call
/api/mcp/.Merging deploys the gate and the account app and applies the migration.
How you verified
mcp-gate.test.tsplus routing tests inworker.test.ts); bb-account path rule; daemon device read.pnpm exec turbo run lint typecheck --continue --filter='...[origin/main]'passes (153 tasks).pnpm exec turbo run test --filter=@bb/web --filter=@bb/connect --filter=@bb/connect-db --filter=bb-plugin-bb--mcp --filter=bb-plugin-bb-account --filter=@bb/templatespasses.node scripts/check-plugin-forks.mjs plugins/mcppasses.pnpm cloud:devandpnpm dev: a real MCP client signed in through the consent page, called tools through the gate and tunnel against the real CLI, refreshed its token, and lost access within 30 seconds ofbb mcp revoke.🤖 Generated with Claude Code