Phase 1, PR 5: MCP v1 (read tools, known conditions, attach tickets) behind Authelia OAuth #9

Merged
jared merged 3 commits from mcp into main 2026-11-26 17:32:03 -05:00
Owner

Fifth phase 1 PR from the plan on #1. Stacked on #8 (UI): this branch contains #8's commit, so merge #8 first; after that this PR shows only its own commit.

What it is

Thor's MCP server, built into the Flask app, following the wiki's checklist (Architecture/mcp-servers) like the Tinker, Wiki and Kuma servers:

  • Transport: stateless Streamable HTTP, POST https://thor.lotusguild.org/mcp, JSON-RPC 2.0 (single and batch). GET/DELETE answer 405, since there are no sessions or streams.
  • Discovery: /.well-known/oauth-protected-resource[/mcp] (RFC 9728). Without a token you get a 401 whose WWW-Authenticate names resource_metadata and the scopes.
  • Token checks, done by Thor itself against Authelia's JWKS: RS256 signature, iss https://auth.lotusguild.org, aud https://thor.lotusguild.org/mcp, exp, typ: at+jwt, client_id: thor-mcp, and scopes from the scp array. Group admin only, like the Kuma MCP.
  • Remote-* headers are never trusted. Every tool call is logged as one JSON line with the user, sub, tool, arguments and ok.

Tools

Tool Scope What
list_incidents thor:read active incidents and the last 7 days
get_incident thor:read everything on the incident page, plus attached tickets
what_covers thor:read Gandalf's and hwmonDaemon's pre-check for a host or scope ("what would Thor do")
thor_status thor:read last cycle, catching up, each source answering or blind, blind periods
list_known_conditions thor:read
add_known_condition thor:write kind, subject (old host names work), optional ticket, and a reason (required, shown in every note)
end_known_condition thor:write
attach_ticket / detach_ticket thor:write attach another app's symptom ticket to a Thor incident. Live, Thor comments "Root cause" on it; in shadow it's only recorded. Timeline-only incidents are refused.

Tools a token's scopes can't use aren't listed. Maintenance stays in Gandalf (thor#1).

Migration 4: incident_links (who attached which ticket, and when).

After merge (production setup; I'll do these step by step, like the Kuma MCP setup)

  1. Authelia: add client thor-mcp: public, PKCE S256, two_factor, group admin; scopes thor:read/thor:write; audience https://thor.lotusguild.org/mcp; callback port 47826, or 47827 if the Gandalf MCP already took 47826. Back up first, validate-config, restart, check health.
  2. NPM host 60: add location = /mcp and location ^~ /.well-known/oauth-protected-resource ahead of the Authelia-protected location /, with the Remote-* headers blanked. Back up the DB, nginx -t, reload.
  3. Wiki: add a Thor row and the callback port to Architecture/mcp-servers.
  4. Verify: a no-token POST gives 401 with resource_metadata; then claude mcp add ... thor and one read and one write.

Verification

  • 147 tests pass, including 16 MCP tests: bad audience, client, typ, signer, expiry and scope all give 401; a non-admin gets 403; scopes filter tools/list; validation; attach and detach; DB away; batches.
  • Coverage is 98% with MariaDB. flake8, bandit and pip-audit are clean. No em or en dashes.

🤖 Generated with Claude Code

Fifth phase 1 PR from the plan on #1. **Stacked on #8 (UI)**: this branch contains #8's commit, so merge #8 first; after that this PR shows only its own commit. ## What it is Thor's MCP server, built into the Flask app, following the wiki's checklist (`Architecture/mcp-servers`) like the Tinker, Wiki and Kuma servers: - **Transport:** stateless Streamable HTTP, `POST https://thor.lotusguild.org/mcp`, JSON-RPC 2.0 (single and batch). `GET`/`DELETE` answer 405, since there are no sessions or streams. - **Discovery:** `/.well-known/oauth-protected-resource[/mcp]` (RFC 9728). Without a token you get a 401 whose `WWW-Authenticate` names `resource_metadata` and the scopes. - **Token checks**, done by Thor itself against Authelia's JWKS: RS256 signature, `iss` https://auth.lotusguild.org, `aud` https://thor.lotusguild.org/mcp, `exp`, `typ: at+jwt`, `client_id: thor-mcp`, and scopes from the `scp` array. **Group `admin` only**, like the Kuma MCP. - `Remote-*` headers are never trusted. Every tool call is logged as one JSON line with the user, `sub`, tool, arguments and ok. ## Tools | Tool | Scope | What | |---|---|---| | `list_incidents` | thor:read | active incidents and the last 7 days | | `get_incident` | thor:read | everything on the incident page, plus attached tickets | | `what_covers` | thor:read | Gandalf's and hwmonDaemon's pre-check for a host or scope ("what would Thor do") | | `thor_status` | thor:read | last cycle, catching up, each source answering or blind, blind periods | | `list_known_conditions` | thor:read | | | `add_known_condition` | thor:write | kind, subject (old host names work), optional ticket, and a reason (required, shown in every note) | | `end_known_condition` | thor:write | | | `attach_ticket` / `detach_ticket` | thor:write | attach another app's symptom ticket to a Thor incident. Live, Thor comments "Root cause" on it; in shadow it's only recorded. Timeline-only incidents are refused. | Tools a token's scopes can't use aren't listed. Maintenance stays in Gandalf (thor#1). **Migration 4:** `incident_links` (who attached which ticket, and when). ## After merge (production setup; I'll do these step by step, like the Kuma MCP setup) 1. **Authelia:** add client `thor-mcp`: public, PKCE S256, `two_factor`, group `admin`; scopes `thor:read`/`thor:write`; audience `https://thor.lotusguild.org/mcp`; callback port 47826, or 47827 if the Gandalf MCP already took 47826. Back up first, `validate-config`, restart, check health. 2. **NPM host 60:** add `location = /mcp` and `location ^~ /.well-known/oauth-protected-resource` ahead of the Authelia-protected `location /`, with the `Remote-*` headers blanked. Back up the DB, `nginx -t`, reload. 3. **Wiki:** add a Thor row and the callback port to `Architecture/mcp-servers`. 4. **Verify:** a no-token POST gives 401 with `resource_metadata`; then `claude mcp add ... thor` and one read and one write. ## Verification - 147 tests pass, including 16 MCP tests: bad audience, client, `typ`, signer, expiry and scope all give 401; a non-admin gets 403; scopes filter `tools/list`; validation; attach and detach; DB away; batches. - Coverage is 98% with MariaDB. flake8, bandit and pip-audit are clean. No em or en dashes. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
jared force-pushed mcp from 9f993dabf1 to 45afd6e247 2026-11-26 13:07:28 -05:00 Compare
jared added 2 commits 2026-11-26 13:52:43 -05:00
- thor/web/mcp.py: stateless Streamable HTTP at POST /mcp (JSON-RPC 2.0), RFC 9728 metadata at
  /.well-known/oauth-protected-resource[/mcp], 401 with resource_metadata. Tokens checked
  against Authelia's JWKS: RS256, iss, aud (this server), exp, typ at+jwt, client_id thor-mcp,
  scp array; group admin only; Remote-* headers never trusted; every tool call logged with the
  user.
- Read tools (thor:read): list_incidents, get_incident, what_covers, thor_status,
  list_known_conditions. Write tools (thor:write): add_known_condition, end_known_condition,
  attach_ticket, detach_ticket. Tools a token's scopes can't use aren't listed.
- Migration 4: incident_links (who attached which ticket to which incident). attach_ticket
  records an 'attach' action; in shadow nothing is sent, live sends a 'Root cause' comment.
- Incident page lists attached tickets. PyJWT[crypto] added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CI fixes for the MCP PR: tolerate slow web fonts in screenshots; reword a log line
CI / Lint (flake8, house style) (pull_request) Successful in 1m2s
CI / Test (pytest) (pull_request) Successful in 1m1s
CI / Integration (MariaDB) (pull_request) Canceled after 39s
CI / Security (bandit, pip-audit) (pull_request) Canceled after 38s
CI / Scan (secrets, workflows, shell, patterns) (pull_request) Canceled after 0s
CI / UI (screenshots) (pull_request) Canceled after 0s
CI / Deploy (pull_request) Canceled after 0s
af5fb0c609
- screenshots.py waited for network idle, which never comes when Google Fonts hangs in the CI
  container. Wait for the DOM, give the network 10 s, then screenshot either way; a 4xx/5xx
  still fails the job.
- semgrep flagged 'MCP token refused: %s' as a possible credential leak. It only ever logged
  PyJWT's reason; reworded so the rule is satisfied without a suppression.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jared force-pushed mcp from 45afd6e247 to af5fb0c609 2026-11-26 13:52:43 -05:00 Compare
jared added 1 commit 2026-11-26 13:54:28 -05:00
Migration 5: end the stale UPS battery known condition
CI / Lint (flake8, house style) (pull_request) Successful in 1m24s
CI / Test (pytest) (pull_request) Successful in 1m29s
CI / Security (bandit, pip-audit) (pull_request) Successful in 1m6s
CI / Integration (MariaDB) (pull_request) Successful in 1m50s
CI / Scan (secrets, workflows, shell, patterns) (pull_request) Successful in 1m38s
CI / UI (screenshots) (pull_request) Successful in 1m48s
CI / Deploy (pull_request) Skipped
5e7e2fc695
The APC UPS was replaced by a UniFi UPS 2U Pro on 2026-08-08 (Tinker 387982348, closed
2026-09-25); the 'old battery' entry seeded in migration 3 came from Grafana silences that
predate it. Ended, not deleted, so the history stays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jared merged commit 2e3d74d608 into main 2026-11-26 17:32:03 -05:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: LotusGuild/thor#9