feat: MCP server for AI clients (v2.15.0, ADR-075)

Add an MCP server at SITE_URL/mcp so AI assistants (claude.ai connectors,
Claude Code) can search, read and write notes.

- starpunk/mcp_server: official MCP SDK 2.3, stateless Streamable HTTP with
  JSON responses, served by uvicorn as a sidecar from the same image.
  Ten tools; writes go through the Micropub handlers.
- IndieAuth: RFC 8707 resource indicators bound to codes and tokens
  (migration 022), client ID metadata document + public client metadata,
  draft in scopes_supported, default MCP scopes, consent screen note.
- BACKGROUND_WORKERS_ENABLED flag so the sidecar runs no workers.
- Docs: ADR-075, architecture/mcp.md and updates; CHANGELOG.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
PhilandClaude Opus 5.5 committed 2026-11-11 16:36:58 -07:00
1 parent 4ed2033cf9
commit 84411a16d9
27 files changed
+1248 -45

No files matched your search

+2 -1
View File
@@ -13,10 +13,11 @@ original v0.x technology choices are kept in
|----------|-------------|
| [overview.md](overview.md) | System overview: purpose, request flow, modules and routes, background workers, storage, auth model, URL surface, caching, security posture |
| [technology-stack.md](technology-stack.md) | Current runtime, Python dependencies, standards implemented, testing and CI |
| [database.md](database.md) | SQLite schema after migrations 001–021, migration runner and its lock, connection pool |
| [database.md](database.md) | SQLite schema after migrations 001–022, migration runner and its lock, connection pool |
| [deployment.md](deployment.md) | Container image stages, CI workflow, release process, `/data` layout, reverse proxy, all environment variables |
| [simplified-auth-architecture.md](simplified-auth-architecture.md) | Admin login via OpenID Connect and the built-in IndieAuth server that issues Micropub tokens (ADR-064) |
| [indieweb-protocols.md](indieweb-protocols.md) | How Micropub, IndieAuth, Webmention, microformats2, WebSub and the feeds are implemented (including backfeed responses, web actions and notifications), with compliance status and known gaps |
| [mcp.md](mcp.md) | The MCP server for AI clients: sidecar, authorization flow, tools (ADR-075) |
| [syndication-architecture.md](syndication-architecture.md) | Crossposting (POSSE) to GoToSocial, Bluesky and Threads: syndications table, worker, reconcile rules, backfeed of responses (ADR-073) |
## Where to start
+5 -4
View File
@@ -2,7 +2,7 @@
**Version**: 2.14.0 (2026-11-10)
**Status**: Current. Verified against a database created by `create_app()`
with migrations 001–021.
with migrations 001–022.
StarPunk uses one SQLite file, `starpunk.db` under `DATA_PATH` (`/data` in
the container), in WAL mode. Note *content* is not in the database: it lives
@@ -75,6 +75,7 @@ Background: [ADR-031](../decisions/ADR-031-database-migration-system-redesign.md
| `019_shared_rate_limits.sql` | 2.11.0 | `rate_limits` (counters shared by all processes) |
| `020_backfeed.sql` | 2.13.0 | Rebuilds `webmentions` adding `origin`, `remote_id`, the `quote` type and an index on (`target_note_id`, `origin`); `syndications.backfeed_next_at`, `backfeed_checked_at`, `backfeed_claimed_until`, `backfeed_attempts`, `backfeed_error`, `backfeed_backfill`, `likes_count`, `reposts_count`, `quotes_count` |
| `021_notifications.sql` | 2.14.0 | `webmentions.notified_at`; existing rows are marked as notified |
| `022_indieauth_resource.sql` | 2.15.0 | `indieauth_codes.resource` and `indieauth_tokens.resource` (RFC 8707, MCP tokens; ADR-075) |
New migrations are numbered `NNN_description.sql`, must be safe to run inside
one transaction, and should use `IF NOT EXISTS` where SQLite allows it.
@@ -128,12 +129,12 @@ value), `me`, `created_at`, `expires_at`, `last_used_at`, `user_agent`,
(primary key), `nonce`, `code_verifier` (PKCE), `next_url`, `expires_at`.
Consumed once on callback.
**`indieauth_codes`**: authorization codes issued to Micropub clients:
**`indieauth_codes`**: authorization codes issued to Micropub and MCP clients:
`code_hash`, `client_id`, `redirect_uri`, `scope`, `code_challenge`,
`expires_at`, `used_at` (single use).
`resource` (`SITE_URL/mcp` or NULL), `expires_at`, `used_at` (single use).
**`indieauth_tokens`**: access tokens: `token_hash`, `client_id`, `scope`,
`created_at`, `expires_at`, `last_used_at`, `revoked_at`. Listed and revoked
`resource` (`SITE_URL/mcp` for MCP tokens, NULL for Micropub), `created_at`, `expires_at`, `last_used_at`, `revoked_at`. Listed and revoked
in `/admin/tokens`.
**Legacy tables, unused**: `auth_state` (v1 IndieLogin login state), `tokens`
+9
View File
@@ -125,6 +125,14 @@ WebSub rate limits need; without it, every client appears as the proxy's
address and shares one limit. Do not set it if the container port can be
reached without the proxy, because the header could then be forged.
The MCP sidecar (v2.15.0, ADR-075) is a second container from the same
image, with the same environment and `/data` volume plus
`BACKGROUND_WORKERS_ENABLED=false`, running
`uvicorn starpunk.mcp_server.app:app --host 0.0.0.0 --port 8001`. The proxy
sends `/mcp*` and `/.well-known/oauth-protected-resource*` to it, keeping the
`Host` header; everything else goes to port 8000. Without the sidecar the
site simply has no MCP endpoint. See [mcp.md](mcp.md).
`SITE_URL` must be the public `https://` URL: it is used for absolute links,
feed and WebSub topic URLs, the IndieAuth issuer and redirect URIs, and to
decide whether the session cookie is `Secure`.
@@ -175,6 +183,7 @@ ignored with a warning.
| `FLASK_DEBUG` | `0` | Debug off unless `1` (`flask run --debug` sets it locally); the image sets `0` |
| `LOG_LEVEL` | `INFO` | `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL` |
| `VERSION` | package `__version__` | Overrides the reported version; normally unset |
| `BACKGROUND_WORKERS_ENABLED` | `true` | `false` stops the webmention, WebSub and syndication workers; set in the MCP sidecar (v2.15.0) |
### Feeds
+77
View File
@@ -0,0 +1,77 @@
# MCP server (v2.15.0)
How AI clients use StarPunk through the
[Model Context Protocol](https://modelcontextprotocol.io/). The decision and
alternatives are in [ADR-075](../decisions/ADR-075-mcp-server.md).
## Shape
```
client ──HTTPS──> reverse proxy
/mcp*, /.well-known/oauth-protected-resource* ──> starpunk-mcp:8001 (uvicorn, MCP SDK)
everything else (incl. /auth/*, /.well-known/oauth-authorization-server)
──> starpunk:8000 (gunicorn, Flask)
starpunk and starpunk-mcp: same image, same environment, same /data volume
```
| Module | Role |
|---|---|
| `starpunk/mcp_server/app.py` | ASGI entry: `uvicorn starpunk.mcp_server.app:app --host 0.0.0.0 --port 8001` |
| `starpunk/mcp_server/server.py` | `create_mcp_server(app)` and `create_asgi_app(app=None)`: stateless Streamable HTTP, JSON responses, 16 MB request limit, DNS rebinding protection |
| `starpunk/mcp_server/auth.py` | `StarPunkTokenVerifier`: StarPunk tokens to SDK `AccessToken`s |
| `starpunk/mcp_server/tools.py` | The tools; each runs in a thread inside `app.test_request_context(base_url=SITE_URL)` |
`create_asgi_app()` with no argument builds the Flask app from the
environment with `BACKGROUND_WORKERS_ENABLED=false`. The sidecar container
should also set that variable, for clarity.
## Authorization flow
1. The client POSTs to `/mcp` without a token and gets `401` with
`WWW-Authenticate: Bearer ... resource_metadata="SITE_URL/.well-known/oauth-protected-resource/mcp"`.
2. That document names `SITE_URL/mcp` as the resource and `SITE_URL/` as
the authorization server. The client reads
`/.well-known/oauth-authorization-server`, which says it accepts client ID
metadata documents and public clients.
3. The client sends the owner to `/auth/authorize` with its `client_id` URL,
PKCE and `resource=SITE_URL/mcp`. StarPunk fetches the client's metadata
document to check `redirect_uri` (unless it has the same origin as the
`client_id`), and the owner logs in with OIDC and approves. With no
`scope`, the default MCP scopes are offered.
4. The token endpoint exchanges the code. The token is bound to the
resource.
5. Each MCP request carries the token. The verifier looks it up and the SDK
refuses it unless its resource is `SITE_URL/mcp`.
Details of codes, tokens and scopes are in
[simplified-auth-architecture.md](simplified-auth-architecture.md).
## Tools
| Tool | Does | Scope |
|---|---|---|
| `search_notes(query, include_drafts, limit)` | FTS5 search, with snippets | any; drafts need a posting scope |
| `list_notes(include_drafts, limit, offset)` | Newest first, summaries | any; drafts need a posting scope |
| `get_note(url_or_slug)` | Microformats2 properties (as Micropub `q=source`) | any; drafts need a posting scope |
| `list_tags(filter)` | Tags in use, most used first | posting scope |
| `get_site_config()` | Micropub `q=config`, including `syndicate-to` targets | any |
| `create_note(content, title, tags, draft=True, photos, in_reply_to, like_of, repost_of, bookmark_of, syndicate_to, slug, published_at)` | Micropub create | `create` or `draft` |
| `update_note(url, content, title, tags, add_tags, remove_tags, draft, photos, in_reply_to)` | Micropub update | `update` |
| `delete_note(url)`, `undelete_note(url)` | Soft delete and restore | `delete` |
| `upload_media(filename, data_base64)` | Save an image, return its URL | `media` or `create` |
"Posting scope" means `create`, `update`, `delete` or `draft`. Limits are
capped at 100. Micropub errors come back as tool errors (`isError`) with the
Micropub error description.
## Operating it
- Environment: the same as the main container, plus
`BACKGROUND_WORKERS_ENABLED=false`.
- Proxy: route `/mcp*` and `/.well-known/oauth-protected-resource*` to port
8001, keeping the `Host` header (DNS rebinding protection checks it
against `SITE_URL`).
- Connecting: claude.ai Settings → Connectors → Add custom connector with
`SITE_URL/mcp`; Claude Code: `claude mcp add --transport http starpunk
SITE_URL/mcp`, then `/mcp` to authenticate.
- Revoking: Admin → Connected Apps (MCP tokens are marked "(MCP)").
+11 -1
View File
@@ -223,6 +223,9 @@ coordinated through the data volume:
| WebSub worker (`websub_worker.py`) | `WEBSUB_ENABLED` and not testing | Same pattern with `websub-worker.lock` | Verifies subscription intents, delivers topic content (signed with `X-Hub-Signature` when the subscriber gave a secret), retries, expires leases; pings an external hub instead if `WEBSUB_HUB_URL` is set |
| Syndication worker (`syndication.py`) | At least one crosspost target configured, and not testing | No file lock. Rows in `syndications` are claimed with a 5-minute `claimed_until` lease by an atomic `UPDATE`, so any process may work a row but only one at a time; a `version` column keeps edits made during a send. Threads token refreshes are serialised with a lock file next to `threads_token.json`. | Creates, updates or deletes the remote post for each (note, target) pair; up to 8 attempts. Then, only when `BACKFEED_ENABLED`, backfeed: checks copies for responses, each claimed with a `backfeed_claimed_until` lease (v2.13.0, `backfeed.py`, ADR-073) |
`BACKGROUND_WORKERS_ENABLED=false` starts none of them; the MCP sidecar
(below) sets it so the workers only run in the main container.
All workers run inside an app context, are daemon threads (they end with the
process) and sleep for their poll interval when there is nothing to do. Under
the test suite (`TESTING=True`) none of them start; tests call the processing
@@ -273,7 +276,14 @@ are in [simplified-auth-architecture.md](simplified-auth-architecture.md).
OIDC) approves; the client exchanges the code at `/auth/token` (PKCE
required). Codes and tokens are stored hashed. Tokens last
`INDIEAUTH_TOKEN_LIFETIME_DAYS` and can be revoked at `/auth/revoke` or in
`/admin/tokens`. Scopes: `create`, `update`, `delete`, `media`, `profile`.
`/admin/tokens`. Scopes: `create`, `update`, `delete`, `media`, `draft`,
`profile`.
- **MCP (v2.15.0, ADR-075)**: AI clients use the same flow, identified by a
client ID metadata document and asking for `resource=SITE_URL/mcp`. Their
tokens are bound to that resource and checked by the MCP sidecar, a second
container (uvicorn, official MCP SDK) that the proxy sends `/mcp` to. Its
tools read, search and write notes through the Micropub handlers. See
[mcp.md](mcp.md).
- **Development**: `DEV_MODE=true` adds `/dev/login`, which logs in as
`DEV_ADMIN_ME` without any checks. Never enabled in production; start-up
logs a warning when it is on.
@@ -137,8 +137,10 @@ form `{"error", "error_description"}`.
"token_endpoint": "<SITE_URL>/auth/token",
"revocation_endpoint": "<SITE_URL>/auth/revoke",
"revocation_endpoint_auth_methods_supported": ["none"],
"scopes_supported": ["create", "update", "delete", "media", "profile"],
"scopes_supported": ["create", "update", "delete", "media", "draft", "profile"],
"response_types_supported": ["code"],
"token_endpoint_auth_methods_supported": ["none"],
"client_id_metadata_document_supported": true,
"grant_types_supported": ["authorization_code"],
"code_challenge_methods_supported": ["S256"],
"authorization_response_iss_parameter_supported": true
@@ -192,7 +194,7 @@ requested and left checked, issues a code, and redirects with `code`,
| Value | 32 random bytes, URL-safe | 32 random bytes, URL-safe |
| Stored as | SHA-256 hash | SHA-256 hash |
| Lifetime | 10 minutes | `INDIEAUTH_TOKEN_LIFETIME_DAYS` (default 90) |
| Bound to | `client_id`, `redirect_uri`, `scope`, `code_challenge` | `client_id`, `scope` |
| Bound to | `client_id`, `redirect_uri`, `scope`, `code_challenge`, `resource` | `client_id`, `scope`, `resource` |
`redeem_authorization_code` handles redemption for both `POST /auth/token`
and `POST /auth/authorize`:
@@ -213,11 +215,22 @@ refresh tokens: a client re-authorizes when its token expires.
Expired codes are deleted whenever a new code is issued.
**Resource indicators (v2.15.0, ADR-075).** An authorization request may
include `resource` (RFC 8707). The only accepted value is `SITE_URL/mcp`
(a trailing slash is ignored); anything else is `invalid_target`. With it,
a request without `scope` is offered `create update delete media draft`, and
the consent screen says the client is an AI assistant using MCP. The resource
is stored on the code and then on the token. A token request that names a
different resource gets `invalid_target`. Micropub clients send no resource
and their tokens have none. The MCP endpoint only accepts tokens for its
resource; see [mcp.md](mcp.md).
### Verification and revocation
- `verify_access_token(token)` hashes the token and accepts it if it is
neither revoked nor expired, then updates `last_used_at`. It returns
`{me: ADMIN_ME, client_id, scope}`.
`{me: ADMIN_ME, client_id, scope, resource, expires_at}`. The legacy
`GET /auth/token` still answers with only `me`, `client_id` and `scope`.
- `check_scope(required, scope)` matches whole words in the space-separated
scope string.
- Revocation sets `revoked_at`. It can happen by token value, through
@@ -308,8 +321,8 @@ enough. If it is another page, that page must advertise StarPunk's
|---|---|---|
| `sessions` | Admin browser sessions | `session_token_hash`, `me`, `expires_at`, `last_used_at`, `user_agent`, `ip_address` |
| `oidc_login_state` | One pending OIDC login | `state` (PK), `nonce`, `code_verifier`, `next_url`, `expires_at` |
| `indieauth_codes` | Authorization codes | `code_hash`, `client_id`, `redirect_uri`, `scope`, `code_challenge`, `expires_at`, `used_at` |
| `indieauth_tokens` | Access tokens | `token_hash`, `client_id`, `scope`, `expires_at`, `last_used_at`, `revoked_at` |
| `indieauth_codes` | Authorization codes | `code_hash`, `client_id`, `redirect_uri`, `scope`, `code_challenge`, `resource` (migration 022), `expires_at`, `used_at` |
| `indieauth_tokens` | Access tokens | `token_hash`, `client_id`, `scope`, `resource` (migration 022), `expires_at`, `last_used_at`, `revoked_at` |
The legacy v1 tables `auth_state`, `tokens` and `authorization_codes` are
not used (see the migration's header comment).
@@ -338,7 +351,6 @@ connection pinned, a 5-second limit).
Known gaps:
- `scopes_supported` leaves out `draft`, although Micropub honours it and
the consent screen grants it when requested. Unknown scopes are also shown
and granted as requested; they just have no effect.
- Unknown scopes are shown and granted as requested; they just have no
effect. (`draft` has been listed in `scopes_supported` since v2.15.0.)
- No token introspection endpoint and no refresh tokens.
+3
View File
@@ -48,6 +48,8 @@ vulnerabilities (`pip-audit`); check again before each release.
| python-dotenv | `1.2.*` | Loading `.env` |
| Pillow | `13.0.*` | Image validation, resizing and variants; converting response avatars to 96 px WebP (`avatars.py`) |
| pillow-heif | `1.8.*` | HEIC/HEIF uploads (iPhone photos) |
| mcp | `2.3.*` | MCP server for AI clients (`starpunk/mcp_server/`, ADR-075); brings Starlette, pydantic and anyio. Only the sidecar imports it |
| uvicorn | `0.54.*` | ASGI server for the MCP sidecar |
`markupsafe` comes with Flask/Jinja2.
@@ -72,6 +74,7 @@ front-end framework or bundler, no separate search server.
| IndieWeb | Micropub (create, update, delete, undelete, queries, media endpoint), IndieAuth (authorization server with PKCE, metadata endpoint, token revocation), Webmention (send and receive), WebSub (publisher and built-in hub), microformats2 (h-entry, h-feed, h-card markup and parsing), rel=me, web actions (`<indie-action>`), backfeed of responses from the silos |
| Login | OpenID Connect authorization code flow with PKCE (provider: Pocket ID) |
| Feeds | RSS 2.0, Atom 1.0, JSON Feed 1.1, OPML 2.0, HTTP content negotiation |
| AI clients | Model Context Protocol (Streamable HTTP, stateless), OAuth 2.0 Protected Resource Metadata (RFC 9728), Resource Indicators (RFC 8707), Client ID Metadata Documents |
| Crossposting | Mastodon client API (GoToSocial), AT Protocol (Bluesky XRPC), Threads Graph API; the same APIs are read for backfeed |
Protocol details: [indieweb-protocols.md](indieweb-protocols.md),