Skip to content

Add an MCP server for bb with OAuth sign-in through bb connect - #5450

Merged
SawyerHood merged 1 commit into
mainfrom
bb/can-you-add-support-for-an-mcp-for-bb-that-suppo-thr_tyau7z98ih
Oct 13, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
bb/can-you-add-support-for-an-mcp-for-bb-that-suppo-thr_tyau7z98ih

Conversation

@SawyerHood

Copy link
Copy Markdown
Collaborator

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).

  • Serves a stateless MCP endpoint that answers protocol 2026-07-28 and 2025-era clients, using @modelcontextprotocol/server v2.
  • Three tools: search (find bb commands), read and run. read and run execute a JavaScript snippet in a QuickJS sandbox whose only capability is calling the bb CLI on the server host, so the whole CLI is reachable and several steps fit in one call.
  • read is 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.
  • Limits per snippet: 25 seconds, 3 seconds of computation, 200 bb calls, 4 at a time (8 per server).
  • CLI: bb mcp status, bb mcp clients, bb mcp revoke <grant-id>. Skill, overview, docs/configuration.md and 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/grants backs the CLI.

Connect gate (apps/connect). On a bb's address, /mcp accepts 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 with x-bb-gate-auth: oauth; that local route returns 404 from outside. No other path changes.

Connect database. Migration 0009_mcp_oauth.sql adds oauth_grant, oauth_code and oauth_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/zero could exhaust memory). No wire fields change, so HOST_DAEMON_PROTOCOL_VERSION is 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

  • Tests added: plugin, 120 tests (sandbox limits and recovery, the read list, CLI runner against real child processes, both protocol versions through the real MCP client); account app OAuth server and UI; gate (mcp-gate.test.ts plus routing tests in worker.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/templates passes.
  • node scripts/check-plugin-forks.mjs plugins/mcp passes.
  • Local end-to-end with pnpm cloud:dev and pnpm 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 of bb mcp revoke.
  • Not verified: anything on Cloudflare (real D1, Workers), the real Claude.ai and ChatGPT connectors, and Windows.

🤖 Generated with Claude Code

AGENT GENERATED

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
SawyerHood marked this pull request as ready for review October 13, 2026 21:06
@SawyerHood
SawyerHood merged commit b466ff0 into main Oct 13, 2026
88 of 89 checks passed
@SawyerHood
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant