Files
PhilandClaude Opus 5.5 84411a16d9 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>
2026-11-11 16:36:58 -07:00

57 lines
1.8 KiB
Python

"""
Bearer token checks for the MCP endpoint (v2.15.0, ADR-075)
Tokens are StarPunk IndieAuth tokens bound to the MCP resource (RFC 8707).
The SDK's bearer middleware turns a missing or rejected token into a 401
that points the client at the protected resource metadata, and refuses
tokens whose resource isn't this endpoint (validate_token_resource).
"""
from datetime import datetime, timezone
from typing import Optional
import anyio.to_thread
from flask import Flask
from mcp.server.auth.provider import AccessToken
from starpunk.indieauth import verify_access_token
def _epoch(value) -> Optional[int]:
"""SQLite timestamp (datetime or 'YYYY-MM-DD HH:MM:SS' UTC) -> epoch seconds"""
if value is None:
return None
if isinstance(value, str):
try:
value = datetime.fromisoformat(value)
except ValueError:
return None
if value.tzinfo is None:
value = value.replace(tzinfo=timezone.utc)
return int(value.timestamp())
class StarPunkTokenVerifier:
"""TokenVerifier backed by the indieauth_tokens table"""
def __init__(self, app: Flask):
self.app = app
def _verify(self, token: str) -> Optional[AccessToken]:
with self.app.app_context():
info = verify_access_token(token)
if not info:
return None
return AccessToken(
token=token,
client_id=info["client_id"],
scopes=(info["scope"] or "").split(),
expires_at=_epoch(info.get("expires_at")),
resource=info.get("resource"),
subject=info["me"],
)
async def verify_token(self, token: str) -> Optional[AccessToken]:
# Database work happens off the event loop
return await anyio.to_thread.run_sync(self._verify, token)