Skip to content

feat(gateway): MCP gateway and Connect route for apps on hearth - #85

Merged
bketelsen merged 4 commits into
mainfrom
irulan/mcp-gateway
Nov 16, 2026
Merged

bketelsen merged 4 commits into
mainfrom
irulan/mcp-gateway

Conversation

@frostyard-machines

@frostyard-machines frostyard-machines commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Agents reach apps on hearth through 1 MCP endpoint of lodge's, /gateway/mcp, on lodge's OAuth server (ADR-0024, Proposed). The frame of an app on hearth calls its RPCs at /app/<id>/connect/.

  • Registration. app.json accepts "hearth": "manifest.json" and "tools": "tools.json" (the catalog in the shape of an MCP tools/list result). lodge reads the manifest's operations and checks the catalog against them. An unknown hearth format major disables the app with a reason that names both versions. gateway is a reserved app ID. An app on hearth is 404 on /mcp/<id>.
  • /gateway/mcp (internal/mcpgw, the only package that imports the Go MCP SDK v1.8.0): 1 MCP server lodge per principal, stateful sessions bound to the OAuth client, tools.listChanged: true. A principal sees <id>_<tool> for each agent operation whose permission its grant holds, plus request_access. Hints come from the operation's effect.
  • Calls read the grant again and send a Connect unary JSON call to the app's socket, with Lodge-Caller: principal, Lodge-Principal and Lodge-Grant. A call outside the grant (listed or not) is the tool error insufficient_scope: …, and the app sees nothing.
  • Access. request_access(app, items) queues a consent request and returns its link. An approval, the grants page's new per-item Remove, or a reload re-computes the tools; open sessions get notifications/tools/list_changed and stay open. A revocation ends the client's requests and streams.
  • Token checks. No token or a dead token: 401 with resource_metadata. A live token without scope mcp: 403 with error="insufficient_scope" (oauth.ErrScope).
  • Tests with real clients. internal/hearthtest runs hearth v0.1.0's example app (notes-lite, copied: the example is its own module) for the Go tests and the development build (dev/hearthapp). make mcp-e2e (nsl) drives the gateway with pi-mcp 1.5.2 (the MCP client of Pi Pocket's mcp module) and with MCP Inspector 2.15.0's mcpdo, both pinned: sign-in, request_access, approval, list_changed, calls.

Specs: lodge-http.md (The MCP gateway; App calls), app-contract.md (Apps on hearth), oauth.md; roadmap Phase 8; site guide configure.md.

Risk tier

Risk tier: 3 — risk:high: new auth surfaces. Matched paths, and why each change is safe:

  • internal/mcpgw/** (new, L1): lodge's checks run before it (Host, no Origin, method, peer identity, OAuth token with scope mcp, no upgrade); it trusts only the caller lodge put in the context. Sessions are bound to the client (TokenInfo.UserID): another client's token on a session is 403 (test). Every call reads the grant again, so a narrowed grant refuses the next call (tests).
  • internal/gateway/{gateway,kinds,proxy,connect}.go (L1): /gateway/mcp and /app/<id>/connect/ are new routes with their own ordered checks (Origin is lodge's, Sec-Fetch-Site same-origin, POST only, every identity check, operations exposed to ui only, content type, a login with @, 4 MiB body). The proxy deletes every client Lodge-* header (test). /mcp/<id> only gets stricter (404 for an app on hearth).
  • internal/oauth/** (L1): Check now reports a missing scope as ErrScope (was ErrInvalid; both refuse) and returns the expiry; RemoveGrantItem narrows a grant in 1 transaction, from the grants page with its CSRF token (403 without), admin only as before.
  • internal/apps/apps.go (L1): reads and bounds the operations and the tool catalog (1 MiB, 128 tools, names, schemas of type object); any break disables only that app.
  • kit/manifest/** (L1): additive: tools member and the reserved ID gateway.
  • cmd/lodge/main.go (L1): wires the gateway and the OAuth hooks; no admission change.
  • internal/repocheck/** (L2): new import-boundary tests (MCP SDK only in internal/mcpgw; hearth only in internal/hearthtest and dev/hearthapp).
  • Makefile (L2): e2e-build builds lodge-hearthapp; new mcp-e2e target (not in a gate: it needs the network).
  • .github/risk-paths.json (L2): adds internal/mcpgw/** (L1) and connect.go.

Docs housekeeping

  • New docs started from their category's TEMPLATE.md (ADR-0024)
  • Every new doc indexed in docs/README.md
  • Cross-links added in both directions (ADR ↔ design ↔ spec ↔ plan)
  • New significant decision recorded as an ADR first, in this PR
  • New or edited ADRs are fact-only: context, decision and consequences (ADR-0006 gets only a superseded-for-hearth status line)
  • Conformance aliases and canonical symlinks are unchanged; canonical targets are edited instead

Verification

  • make ci green on the host and nsl (verify, race, cross, client-check, ui-check, shell-check)
  • make vulncheck: No vulnerabilities found (both modules)
  • make mcp-e2e (nsl): pi-mcp — 401 with resource metadata, sign-in, request_access only, approval, list_changed on the same session, new tools, calls, a folder outside the grant refused by the app; Inspector — sign-in, listChanged capability, the change on the open session (lodge's mcp tools changed … sessions=1), new tools, a call
  • The apps that pin the kit still build on it: the kit/manifest change is additive (a new optional member and 1 more reserved ID)

Cross-review

Reviewer openai/gpt-6.1-sol; coders deepseek/deepseek-flash (Go code and tests) and anthropic/claude-opus-5-5 (docs, dev/mcp-e2e/). Base 16b6ff6e2d2b1d1bad9ab3c56207a25e1c224073.

  • Initial review at d476b58b028cc3ab7bebe621d4d0e38bce956a08, in 5 batches (gateway/mcpgw/oauth/wiring; loaders, scripts and governance; the 2 gateway test files; the other tests; docs). Excluded from line-by-line review: dev/mcp-e2e/pnpm-lock.yaml, go.sum, and the notes-lite copy in internal/hearthtest/notelite/ (compared read-only with hearth v0.1.0). Not ready, 5 blocking: (1) a revocation or a token expiry did not cancel a tool call in flight (the SDK runs handlers on the session context); (2) MCP calls had their own app pool, past the 128-connection limit; (3) a reload that kept a tool's bytes kept a handler with the old operation; (4) an older sync snapshot could be applied after a newer one; (5) the Inspector test read lodge's log, not what Inspector received.
  • Fixed in c5ed735 and 47e8832: per-call context registered under the client with a revoked-client set; 1 shared app transport (gateway.NewAppTransport, Config.Transport, mcpgw.Options.Transport); the definition hash covers the operation and a call resolves the current one; a per-server sync lock; dev/mcp-e2e/tap.mjs records the messages in Inspector's own transport, and the test asserts the notification on Inspector's session and the same session afterwards.
  • Re-review 1 at 47e8832: findings 1–5 fixed; 1 new blocking (6): the late-revocation 401 lacked the challenge and Connection: close. Fixed in 5fbe8fc (Caller.Refuse, lodge's own 401).
  • Re-review 2 at 5fbe8fc3a1207619adc349ae53f47611febdc699: Ready, Blocking: none.

Correction records: .memory/corrections/2026-11-16-pr-85-gateway-calls-follow-revocation-and-reloads.json, .memory/corrections/2026-11-16-pr-85-late-revocation-answers-as-lodge.json.

Revert tests

In a scratch worktree from the PR head, with the tests kept, each fix reverted alone:

Reverted Failing test(s)
the unlisted-call middleware (srv.AddReceivingMiddleware(s.unlistedCall)) TestMCPGatewayGrantNarrowing, TestMCPGatewayUngrantedToolByName: unknown tool "notelite_create_note"
the call's own context (g.callContext in call) TestMCPGatewayRevokeEndsACall, TestMCPGatewayExpiryEndsACall: the app's request context did not end
the sync lock (s.syncMu) TestSyncNewestWins: final tools lack notelite_create_note
the operation in definitionHash TestMCPGatewayToolFollowsItsOperation: no re-registration after the reload
gateway.New using Config.Transport TestAppTransportIsShared: the third call reached the app past MaxConnsPerHost
the Caller.Refuse call TestMCPGatewayRevokedAfterTheCheck: no challenge, connection kept
lodge's OnGrant hook (cmd/lodge/main.go), with make mcp-e2e pi-mcp: notifications/tools/list_changed: nothing within 10000 ms; Inspector: Inspector received no notifications/tools/list_changed on session … within 10000 ms

Command form: go test -count=1 ./internal/gateway/ -run '<test>' (and ./internal/mcpgw/); for the last row, the reverted build/lodge-e2e with dev/mcp-e2e/run.sh in nsl.

lodge reads an app's hearth manifest and tool catalog (app.json `hearth`
and `tools`), serves its RPCs to its frame at /app/<id>/connect/, and
gives agents 1 MCP endpoint, /gateway/mcp, on lodge's OAuth server:
each principal sees <id>_<tool> for what its grant holds, asks for more
with request_access, and gets notifications/tools/list_changed when the
owner approves or narrows the grant. Each call reads the grant again and
sends a Connect call with Lodge-Principal and Lodge-Grant.

ADR-0024; docs/specs/lodge-http.md (The MCP gateway, App calls);
docs/specs/app-contract.md (Apps on hearth); docs/specs/oauth.md.
@github-actions github-actions Bot added level:L1 Read every line risk:high Touches a path in .github/risk-paths.json labels Nov 16, 2026
…ollow reloads

The MCP SDK runs tool handlers on the session's context, so each call now
registers its own context under its client, with a deadline at its token's
expiry; a set of revoked clients closes the race between lodge's check and
the registration. Calls to apps take connections from the routes' pool,
so that 1 limit covers both. A handler's hash covers its operation, and a
call resolves the current operation by its tool. 1 sync at a time spans
its snapshot and its application.

The MCP Inspector end-to-end test now reads the notification in
Inspector's own transport (dev/mcp-e2e/tap.mjs) and checks that its
session stays the same.
@frostyard-machines frostyard-machines added the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 16, 2026
@bketelsen
bketelsen merged commit 422dd8b into main Nov 16, 2026
13 checks passed
@bketelsen
bketelsen deleted the irulan/mcp-gateway branch November 16, 2026 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:L1 Read every line ready-for-human Reviewed, CI green: ready for Brian to review and merge risk:high Touches a path in .github/risk-paths.json

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants