Skip to content

Grok Bot MCP Events: Cloudflare Worker host polyfill + unified remote MCP inbox - #27

Open
infoxicator wants to merge 8 commits into
idosal:mainfrom
infoxicator:grok-events-remote-inbox
Open

infoxicator wants to merge 8 commits into
idosal:mainfrom
infoxicator:grok-events-remote-inbox

Conversation

@infoxicator

@infoxicator infoxicator commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a zero-server Cloudflare Worker + SQLite Durable Object host polyfill (polyfills/events/grok-bot/worker) and upgrades the self-hosted Node bridge (polyfills/events/grok-bot) so Grok Bot can reliably receive MCP Events (SEP-3415) 24/7 while its VM sleeps, with zero scheduled model runs and zero idle token burn.

Cloudflare Worker host polyfill (polyfills/events/grok-bot/worker)

  • Single Worker + SQLite Durable Object (GrokEventsHub) deployed to the user's own Cloudflare account via npm run setup (same deployment pattern as polyfills/events/claude-code/mod/relay, with its own Workers-native client/receiver/scheduler).
  • 7 Grok adapter MCP tools (/mcp): read_batch plus the 6 MCP Events tools (list_event_types, watch_event, unwatch_event, list_watches, get_event, recent_events).
  • Security & credential separation:
    • Separate READER_KEY (POST /mcp only) and ADMIN_KEY (/oauth/start, /admin/servers, DELETE /admin/servers/*, /admin/delivery/{retry,accept}).
    • All outbound MCP, OAuth, and routine wake requests use redirect: 'manual' (rejecting 3xx) and strict https:// / issuer / metadata URL validation.
    • Streaming 256 KiB ingress cap on /hooks/*, Standard Webhooks HMAC-SHA256 verification over exact received bytes and exact raw webhook-timestamp string, and synchronous post-await watch state re-read before committing events/dedupe state.
    • Watches bind to their upstream endpoint URL and are explicitly retired (returning 410 on hooks) when a server is removed or re-authenticated to a different URL.
    • Full stand-down via MCP_EVENTS_OFF=1 (empty tool list, stopped alarms, and 503 on /hooks/* and OAuth endpoints).
  • SEP-3415 protocol handling:
    • Speaks MCP 2026-07-28 (server/discover, Mcp-Method, Mcp-Name, Mcp-Param-*, per-request _meta envelope) with narrow fallback to legacy initialize and MCP_EVENTS_PRE_STANDARD=off strict mode.
    • Preserves typed JSON-RPC errors on non-2xx responses, clears null/absent cursors, gates truncation notices on supplied cursors, stops on terminal Forbidden, and rediscovers on HeaderMismatch / NotFound / Unsupported.
  • OAuth 2.1 + PKCE sign-in (/oauth/start, /oauth/callback): connects OAuth MCP servers (such as T3 Code) via browser sign-in with least-privilege scope enforcement, truthful revocation reporting (access + refresh tokens), and automatic refresh_token rotation.
  • Resilient wake delivery & recovery: wakes Grok Bot's native webhook routine with an opaque batch ID, retries transient 408/429/5xx errors with exponential backoff, parks cleanly when routine secrets are unconfigured, caps pending backlog with 503 backpressure, and supports operator recovery via npm run setup -- retry and npm run setup -- accept <BATCH>.
  • Simplified setup UX: npm run setup copies the connector header on macOS and npm run setup -- prompt prints/copies the exact Grok Bot routine creation prompt.

Self-hosted Node bridge improvements (polyfills/events/grok-bot)

  • Unified bridge.mjs start (watcher + 7-tool MCP HTTP server in one process) and bridge.mjs init.
  • Exponential backoff retry on transient 408/429/5xx errors, persistent in-flight attempt state for strict holdOnAmbiguous crash recovery, surfaced notices, optional GROK_EVENTS_READER_ORIGIN, and MCP_EVENTS_OFF=1 stand-down.

Verification

  • Automated: Node bridge 14/14 tests + bundle notice check passing; Worker tsc --noEmit clean + 5/5 wrangler dev tests passing (modern-only 2026-07-28, pre-standard MCP_EVENTS_PRE_STANDARD=off, and OAuth 2.1 mocks covering READER_KEY/ADMIN_KEY separation, redirect refusal, raw-byte webhook verification, 503/429 retry, 401 hold & /admin/delivery/* recovery, null cursor clearing, terminal Forbidden, HeaderMismatch unwatch recovery, OAuth scope rejection + confirmed/unconfirmed revocation reporting + refresh rotation, DELETE /admin/servers/* retiring hooks, and MCP_EVENTS_OFF=1 gating).
  • Live end-to-end verified in a signed-in Grok Bot (Foxbot):
    • Public event-feeds (demo.button via poll): watch created from Grok chat, button press woke Foxbot unattended, called read_batch, and reported press-1.
    • Authenticated T3 Code (thread.run_settled via signed webhook + OAuth orchestration:read): watch created from Grok chat, T3 turn completion sent signed webhook to Cloudflare Worker, woke Foxbot unattended, called read_batch, and reported the completed run.

@voucha-checks

voucha-checks Bot commented Nov 28, 2026

Copy link
Copy Markdown

VOUCHA

VOUCHA is currently in early access and is not enabled for this repository yet.

To request access, contact @idosal.

VOUCHA has not added a comprehension check to this PR.

- Separate operator ADMIN_KEY from Grok connector READER_KEY; bind watches to upstream endpoint URL and retire watches/hooks on server removal or endpoint replacement
- Use redirect: 'manual' and strict HTTPS/issuer/URL validation on all outbound MCP and OAuth requests
- Add OAuth refresh_token rotation with scope checks and truthful revocation reporting for access + refresh tokens
- Add /admin/delivery/{retry,accept} recovery for held batches, park delivery when routine secrets are absent, and add backlog backpressure + bounded read_batch
- Clear null/absent cursors, gate truncation notices on supplied cursor, stop on terminal Forbidden, rediscover on HeaderMismatch/NotFound/Unsupported, and narrow 2026-07-28 discovery fallback
- Verify Standard Webhooks HMAC-SHA256 over exact received bytes and raw timestamp string; stream-cap /hooks/* bodies and re-read watch state after await
- Gate /hooks/* and OAuth endpoints during MCP_EVENTS_OFF=1 stand-down
- Persist in-flight attempt state in Node bridge for holdOnAmbiguous crash recovery and fix flaky 503/429 test
- Expand Worker tests with modern 2026-07-28, pre-standard, and OAuth fixtures; remove temporary probe/ folder and simplify setup UX with copy-paste prompt
@infoxicator
infoxicator marked this pull request as ready for review November 30, 2026 16:48
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