REST APIs from a bundled OpenAPI spec with personal-token auth; Forgejo first (0.1.1-dev.5) #109

Merged
john merged 2 commits from feat/openapi-upstreams into main 2026-11-27 02:58:06 +00:00
Owner

In 15 seconds

An upstream can now be a REST API described by an OpenAPI spec, not just an MCP server. It plugs into the existing UserScopedMcpProxy through one new transport factory, so per-user tokens, the session pool, the cache, namespacing and error handling are all reused. Users connect with a personal token pasted on the Connections page. Forgejo is the first example; it stays off until you add a config entry.

flowchart LR
  C["Client"] --> MW["Middleware · auth · timing · batch"]
  MW --> N["native (unchanged)"]
  MW --> U["UserScopedMcpProxy (existing)"]
  U -- "url" --> R["Remote MCP · PocketSmith · Coder"]
  U -- "openapi (new)" --> L["In-process FastMCP from spec<br/>httpx client = this user's token"]
  L --> FJ["code.yawn.dev/api/v1"]

Changes

File What
config.py openapi: {spec, base_url, operations} as an alternative to url; auth: {type: token, header?, prefix?}. Validates file name, HTTPS, 1–64 unique operationIds, header/prefix; rejects stray OAuth fields with token auth. Existing entries parse unchanged.
openapi_upstream.py (new, ~150 lines) Loads and filters the bundled spec (fails on missing operationIds), builds a per-user FastMCP server whose client carries the user's token, strips any non-allowlisted headers, uses permissive output schemas.
upstream_mcp.py Token-auth paths: save_token, access_token, status, is_connected, disconnect; begin/complete refuse token upstreams; the refresher skips them; remote MCP can use a static token.
auth_bundles.py UpstreamTokenBundle (encrypted; listing shows only a ••••last4 hint).
server.py Uses the OpenAPI transport for openapi entries; checks specs at startup.
Connections UI Paste / Replace / Remove token form (CSRF-bound, password field, maxlength 4096); token upstreams don't widen the CSP form-action.
specs/forgejo.json Forgejo 17.0.1, 512 ops, OAS3 (minified, -diff linguist-generated).
scripts/vendor_openapi.py Reproducible regeneration (verified byte-identical).

How to enable Forgejo (after merge and deploy)

Add to CORN_UPSTREAM_MCP_SERVERS in /etc/corn/app.env. The full 19-operation entry is in deploy/README.md:

"forgejo":{"display_name":"Forgejo","auth":{"type":"token"},"openapi":{"spec":"forgejo.json","base_url":"https://code.yawn.dev/api/v1","operations":["repoGet","repoListPullRequests", "..."]}}

Then go to Connections, paste a Forgejo token (Settings → Applications), and the forgejo_* tools appear for you only.

Verification

  • 578 tests pass, plus both browser smoke suites. New tests/test_openapi_upstream.py (23 tests) covers:

    • config parsing and fail-closed cases;
    • a missing operationId stopping startup;
    • the allowlist filter;
    • Alice and Bob each sending their own token, and an unconnected user seeing nothing and sending no requests;
    • non-allowlisted tools rejected;
    • an upstream 401 becoming a ToolError, and a replaced token taking effect;
    • caller headers (cookie, x-session-id, x-forwarded-for) never forwarded;
    • tokens absent from DEBUG logs;
    • list results returned without client parse errors.

    tests/test_web.py adds the token form: CSRF, a bad token rejected without echo, no secret in the page, per-user isolation, and remove.

  • Live run against code.yawn.dev, using Corn's real proxy path with the workspace token and only the read operations:

    • 19 tools, about 15.6 KB of descriptions;
    • repoGet john/corn → public, branch main;
    • last closed PRs #108, #107, #106;
    • main combined status success (8 contexts);
    • repoGetContents pyproject.toml.

    The token didn't appear in any log.

  • Ruff is clean on the new files; changed files have no new findings.

Review notes and limits

  • Generated tools act with the token's own permissions, so scope the token in Forgejo. The allowlist includes writes (repoMergePullRequest, issueCreateComment, DispatchWorkflow); remove them from your config entry if you don't want them.
  • Pasted tokens aren't refreshed or revoked by Corn. Remove deletes Corn's copy only, so revoke the token in Forgejo itself.
  • The bundled spec adds about 530 KB to the image. Only the filtered spec is held in memory per process.
  • Not in this MR: Forgejo OAuth (it needs a pre-registered app), more specs (Vikunja, TREK), and group-based access.
## In 15 seconds An upstream can now be a **REST API described by an OpenAPI spec**, not just an MCP server. It plugs into the **existing** `UserScopedMcpProxy` through one new transport factory, so per-user tokens, the session pool, the cache, namespacing and error handling are all reused. Users connect with a **personal token** pasted on the Connections page. Forgejo is the first example; it stays off until you add a config entry. ```mermaid flowchart LR C["Client"] --> MW["Middleware · auth · timing · batch"] MW --> N["native (unchanged)"] MW --> U["UserScopedMcpProxy (existing)"] U -- "url" --> R["Remote MCP · PocketSmith · Coder"] U -- "openapi (new)" --> L["In-process FastMCP from spec<br/>httpx client = this user's token"] L --> FJ["code.yawn.dev/api/v1"] ``` ## Changes | File | What | |---|---| | `config.py` | `openapi: {spec, base_url, operations}` as an alternative to `url`; `auth: {type: token, header?, prefix?}`. Validates file name, HTTPS, 1–64 unique operationIds, header/prefix; rejects stray OAuth fields with token auth. Existing entries parse unchanged. | | `openapi_upstream.py` (new, ~150 lines) | Loads and filters the bundled spec (fails on missing operationIds), builds a per-user FastMCP server whose client carries the user's token, strips any non-allowlisted headers, uses permissive output schemas. | | `upstream_mcp.py` | Token-auth paths: `save_token`, `access_token`, `status`, `is_connected`, `disconnect`; `begin`/`complete` refuse token upstreams; the refresher skips them; remote MCP can use a static token. | | `auth_bundles.py` | `UpstreamTokenBundle` (encrypted; listing shows only a ••••last4 hint). | | `server.py` | Uses the OpenAPI transport for `openapi` entries; checks specs at startup. | | Connections UI | Paste / Replace / Remove token form (CSRF-bound, password field, `maxlength` 4096); token upstreams don't widen the CSP `form-action`. | | `specs/forgejo.json` | Forgejo 17.0.1, 512 ops, OAS3 (minified, `-diff linguist-generated`). | | `scripts/vendor_openapi.py` | Reproducible regeneration (verified byte-identical). | ## How to enable Forgejo (after merge and deploy) Add to `CORN_UPSTREAM_MCP_SERVERS` in `/etc/corn/app.env`. The full 19-operation entry is in `deploy/README.md`: ```json "forgejo":{"display_name":"Forgejo","auth":{"type":"token"},"openapi":{"spec":"forgejo.json","base_url":"https://code.yawn.dev/api/v1","operations":["repoGet","repoListPullRequests", "..."]}} ``` Then go to Connections, paste a Forgejo token (Settings → Applications), and the `forgejo_*` tools appear for you only. ## Verification - **578 tests pass**, plus both browser smoke suites. New `tests/test_openapi_upstream.py` (23 tests) covers: - config parsing and fail-closed cases; - a missing operationId stopping startup; - the allowlist filter; - Alice and Bob each sending their own token, and an unconnected user seeing nothing and sending no requests; - non-allowlisted tools rejected; - an upstream 401 becoming a ToolError, and a replaced token taking effect; - caller headers (`cookie`, `x-session-id`, `x-forwarded-for`) never forwarded; - tokens absent from DEBUG logs; - list results returned without client parse errors. `tests/test_web.py` adds the token form: CSRF, a bad token rejected without echo, no secret in the page, per-user isolation, and remove. - **Live run against `code.yawn.dev`**, using Corn's real proxy path with the workspace token and only the read operations: - 19 tools, about 15.6 KB of descriptions; - `repoGet john/corn` → public, branch `main`; - last closed PRs #108, #107, #106; - `main` combined status `success` (8 contexts); - `repoGetContents pyproject.toml`. The token didn't appear in any log. - Ruff is clean on the new files; changed files have no new findings. ## Review notes and limits - Generated tools act with **the token's own permissions**, so scope the token in Forgejo. The allowlist includes writes (`repoMergePullRequest`, `issueCreateComment`, `DispatchWorkflow`); remove them from your config entry if you don't want them. - Pasted tokens aren't refreshed or revoked by Corn. Remove deletes Corn's copy only, so revoke the token in Forgejo itself. - The bundled spec adds about 530 KB to the image. Only the filtered spec is held in memory per process. - Not in this MR: Forgejo OAuth (it needs a pre-registered app), more specs (Vikunja, TREK), and group-based access.
feat(upstream): REST APIs from a bundled OpenAPI spec with personal-token auth (0.1.1-dev.5)
All checks were successful
Corn CI / release-check-1 (pull_request) Successful in 3s
Corn CI / release-check (pull_request) Successful in 0s
Corn CI / host (pull_request) Successful in 19s
Security / scan (pull_request) Successful in 23s
Security / security (pull_request) Successful in 0s
Corn CI / test (pull_request) Successful in 10m14s
Corn CI / release (pull_request) Has been skipped
Review / change impact (pull_request_target) Successful in 11m7s
6e6ec533ea
An upstream entry may set openapi {spec, base_url, operations} instead of url.
Corn filters the bundled OpenAPI 3 document to the allowlisted operationIds and
serves the generated tools through the existing UserScopedMcpProxy: the only
new piece is a transport factory that builds an in-process FastMCP server per
user whose HTTP client carries that user's token. Namespacing, session pool,
list cache and error handling are reused.

auth {type: token, header?, prefix?} lets each user paste a personal access
token under Connections -> Linked accounts (CSRF-bound; replace/remove). Stored
encrypted under provider upstream_token; never refreshed or displayed.

Bundles Forgejo 17.0.1's spec (converted from Swagger 2.0) and adds
scripts/vendor_openapi.py to regenerate it reproducibly. Unknown operationIds
or a missing spec stop start-up; caller request headers are stripped before
any upstream request; generated output schemas are permissive.

Change review · f334709cc7eb → c849fce03195

Call paths changed by this PR

Caller → changed symbol → callee. Each edge labels the line in the caller. One detected entry path (up to 3 hops) is included when found. Static evidence, not a runtime stack trace.

After

flowchart LR
    n0["main - added"]
    n1["module scope"]
    n2["redact_bundle - modified"]
    n3["ConnectionService._metadata"]
    n4["ConnectionService.put_for_user"]
    n5["_hint"]
    n6["bundle_to_storage - modified"]
    n7["ConnectionService._mutate_namespace"]
    n8["UpstreamMcpServer.uses_oauth - added"]
    n9["_token_auth - added"]
    n10["env"]
    n11["upstream_mcp_servers"]
    n12["_openapi_source - added"]
    n13["_https_url"]
    n1 -->|L43| n0
    n2 -->|L93| n5
    n12 -->|L269| n13
    n11 -->|L289| n12
    n11 -->|L288| n9
    n3 -->|L336| n2
    n7 -->|L232| n6
    n4 -->|L171| n6
    n4 -->|L176| n2
    n10 -->|L68| n11

Changed symbols:

  • added main — scripts/vendor_openapi.py:21
  • modified redact_bundle — src/corn/auth_bundles.py:86
  • modified bundle_to_storage — src/corn/auth_bundles.py:103
  • added UpstreamMcpServer.uses_oauth — src/corn/config.py:236
  • added _token_auth — src/corn/config.py:240
  • added _openapi_source — src/corn/config.py:252
    Bounded view: 67 more changed symbols and 0 neighbour references omitted.
Overall architecture after this change

Source: c849fce03195 · code-review 0.1.0-dev.1.

Generated from resolved static calls between reviewed code areas. Numbers are call sites, not request counts.
Framework registration, dynamic dispatch, and unresolved calls are not shown. This is not a deployment map.
Showing 7 of 7 inter-area edges. Edit the reviewed source instead of this generated page.

flowchart LR
    n1["Console"]
    n2["Identity"]
    n3["Linked MCP"]
    n4["NSW integrations"]
    n5["Persistence"]
    n6["REST handlers"]
    n7["Server composition"]
    n8["Shared contracts"]
    n9["Shopping"]
    n5 -->|"9"| n2
    n7 -->|"5"| n6
    n7 -->|"2"| n8
    n1 -->|"1"| n4
    n1 -->|"1"| n9
    n3 -->|"1"| n2
    n7 -->|"1"| n2

This diagram is for the linked commit. It refreshes after required checks pass for a new head.

<!-- john/release:architecture-review:v1 --> # Change review · `f334709cc7eb` → [`c849fce03195`](https://code.yawn.dev/john/corn/commit/c849fce03195ad02f6571a82c2bd3e12052ba930) ## Call paths changed by this PR Caller → changed symbol → callee. Each edge labels the line in the caller. One detected entry path (up to 3 hops) is included when found. Static evidence, not a runtime stack trace. ### After ```mermaid flowchart LR n0["main - added"] n1["module scope"] n2["redact_bundle - modified"] n3["ConnectionService._metadata"] n4["ConnectionService.put_for_user"] n5["_hint"] n6["bundle_to_storage - modified"] n7["ConnectionService._mutate_namespace"] n8["UpstreamMcpServer.uses_oauth - added"] n9["_token_auth - added"] n10["env"] n11["upstream_mcp_servers"] n12["_openapi_source - added"] n13["_https_url"] n1 -->|L43| n0 n2 -->|L93| n5 n12 -->|L269| n13 n11 -->|L289| n12 n11 -->|L288| n9 n3 -->|L336| n2 n7 -->|L232| n6 n4 -->|L171| n6 n4 -->|L176| n2 n10 -->|L68| n11 ``` Changed symbols: - **added** `main` — `scripts/vendor_openapi.py:21` - **modified** `redact_bundle` — `src/corn/auth_bundles.py:86` - **modified** `bundle_to_storage` — `src/corn/auth_bundles.py:103` - **added** `UpstreamMcpServer.uses_oauth` — `src/corn/config.py:236` - **added** `_token_auth` — `src/corn/config.py:240` - **added** `_openapi_source` — `src/corn/config.py:252` Bounded view: 67 more changed symbols and 0 neighbour references omitted. <details><summary>Overall architecture after this change</summary> Source: [c849fce03195](https://code.yawn.dev/john/corn/commit/c849fce03195ad02f6571a82c2bd3e12052ba930) · `code-review 0.1.0-dev.1`. Generated from resolved static calls between reviewed code areas. Numbers are call sites, not request counts. Framework registration, dynamic dispatch, and unresolved calls are not shown. This is not a deployment map. Showing 7 of 7 inter-area edges. Edit the reviewed source instead of this generated page. ```mermaid flowchart LR n1["Console"] n2["Identity"] n3["Linked MCP"] n4["NSW integrations"] n5["Persistence"] n6["REST handlers"] n7["Server composition"] n8["Shared contracts"] n9["Shopping"] n5 -->|"9"| n2 n7 -->|"5"| n6 n7 -->|"2"| n8 n1 -->|"1"| n4 n1 -->|"1"| n9 n3 -->|"1"| n2 n7 -->|"1"| n2 ``` </details> This diagram is for the linked commit. It refreshes after required checks pass for a new head.
feat(console): one card per vended integration on Connections
All checks were successful
Corn CI / release-check-1 (pull_request) Successful in 2s
Corn CI / release-check (pull_request) Successful in 0s
Corn CI / host (pull_request) Successful in 17s
Security / scan (pull_request) Successful in 22s
Security / security (pull_request) Successful in 0s
Corn CI / test (pull_request) Successful in 4m33s
Corn CI / release (pull_request) Has been skipped
Review / change impact (pull_request_target) Successful in 5m20s
c849fce031
Each integration Corn vends (built-in FuelCheck, TfNSW, Proton Calendar and
every CORN_UPSTREAM_MCP_SERVERS entry) is a card: name, what it does, its tool
prefix, a status badge (with a ••••last4 hint, never the secret) and only that
integration's own credential form (sign in, personal token, API key, key and
secret, or private ICS link). Connected cards fold the form away and show
Replace / Remove. Connected integrations sort first.

Replaces the integration dropdown and its show/hide script; legacy stored
namespaces without a card stay listed and removable.
Author
Owner

UI added (c849fce): one card per vended integration on Connections.

Every integration Corn vends gets a card: the built-ins (NSW FuelCheck, Transport for NSW, Proton Calendar) and each CORN_UPSTREAM_MCP_SERVERS entry (Coder, PocketSmith, Forgejo, ...). Each card has:

  • the integration's name, what it does, and its tool prefix (e.g. forgejo_*);
  • a status badge: Not connected / Connected / Connected · renews automatically. It shows a ••••last4 hint, never the secret;
  • only that integration's credential form: Sign in to connect (OAuth), a personal access token, an API key, an API key + secret (FuelCheck), or a private ICS link (Proton).

Connected cards fold the form away behind Replace … and show Remove / Disconnect. Connected integrations sort first.

This replaces the old integration dropdown and its show/hide JavaScript. Legacy stored entries without a card are still listed under Other stored credentials and can be removed. Integrations are still vended by the operator; users only supply credentials.

Verified: 578 tests and both browser suites pass, including filling in FuelCheck through its own card and a 390 px mobile layout with no horizontal scroll.

**UI added (c849fce): one card per vended integration on Connections.** Every integration Corn vends gets a card: the built-ins (NSW FuelCheck, Transport for NSW, Proton Calendar) and each `CORN_UPSTREAM_MCP_SERVERS` entry (Coder, PocketSmith, Forgejo, ...). Each card has: - the integration's name, what it does, and its tool prefix (e.g. `forgejo_*`); - a status badge: Not connected / Connected / Connected · renews automatically. It shows a `••••last4` hint, never the secret; - **only that integration's credential form**: *Sign in to connect* (OAuth), a personal access token, an API key, an API key + secret (FuelCheck), or a private ICS link (Proton). Connected cards fold the form away behind *Replace …* and show *Remove* / *Disconnect*. Connected integrations sort first. This replaces the old integration dropdown and its show/hide JavaScript. Legacy stored entries without a card are still listed under *Other stored credentials* and can be removed. Integrations are still vended by the operator; users only supply credentials. Verified: 578 tests and both browser suites pass, including filling in FuelCheck through its own card and a 390 px mobile layout with no horizontal scroll.
john merged commit ebe0a1b09c into main 2026-11-27 02:58:06 +00:00
john deleted branch feat/openapi-upstreams 2026-11-27 02:58:06 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
john/corn!109
No description provided.