The constraint
I write these notes with Claude anyway, so copying drafts into an admin page had become the slowest step. The admin only took a browser cookie, though. I wanted Claude Code on my laptop, and claude.ai on my phone, to read, draft and publish notes as me, and nobody else to be able to.
The Model Context Protocol is the standard way to give Claude tools. The site's API now serves one at /mcp: 21 tools for notes, projects, categories and images, plus the writing guide as a resource.
One process, one sign-in
The server lives inside the existing FastAPI app rather than as a new service. It shares the database, Redis and image storage, and needed no new Kubernetes objects: turning it on was one line in the cluster's config. It runs stateless and answers plain JSON, so any replica can take any request and nothing streams through Cloudflare.
claude.ai only connects to remote servers that speak OAuth 2.1, so the API had to become its own authorization server:
- Dynamic registration: each Claude client registers itself, but only with a redirect URI on an allowlist (claude.ai's callback, or localhost for Claude Code).
- Google sign-in, reused: signing in goes through the admin's existing Google login and callback, and only my account gets through.
- Tokens: access tokens live an hour in Redis, refresh tokens 30 days in PostgreSQL, both stored only as SHA-256 hashes. A code and a refresh token each work once.
The first real connection took 11 seconds from Claude Code registering itself to holding a token, with a consent click and a Google sign-in in between.
The allowlist was not enough
My first design had no consent page. Codes only go to Claude's own callback, so what could go wrong?
Anyone can start a sign-in, and Google lets a signed-in owner through with one click. Someone could begin connecting from their own claude.ai account and send me the link. The code would come back to claude.ai carrying their state, and their connector would end up holding my tokens. The MCP spec calls this a confused deputy. Now a consent page names the client and where the code will go before Google is asked. A form posted from another site cannot answer it.
What a model cannot carry
The plan said "upload an image as base64". But a tool's arguments are text the model writes: a 1 MB photo would be over 1.3 million characters of output. Instead there are two other ways in:
- Claude Code asks for a single-use upload token and sends the file with curl.
- claude.ai has an image fetched from a URL: https only, public addresses only, no redirects, at most 20 MB.
Small things that bit
- A trailing slash. Building the issuer URL ahead of time added one (https://api.yarikama.com/), and clients compare issuers letter for letter.
- Two migration heads. Every test passed; only production's alembic upgrade head would have refused to choose. There is now a test for exactly one head.
- A pre-commit hook deleting imports. It "fixed" the model imports in Alembic's env.py as unused, and autogenerate needs them to know the tables exist.
- Secrets in the logs. The access log, and the traces, kept Google's one-time code from the sign-in callback. Sign-in URLs now lose their query string in both.
What it cost
About 1,300 lines and 99 tests, and an upgrade of uvicorn, pinned at a 2023 version that the MCP SDK would not run on. Sign-in now depends on Redis being up.
New notes and projects are drafts until I say otherwise, and deleting one asks for its slug again. Those are conventions in the tools, not rules the protocol enforces: the server trusts whoever holds my token. This note went in the same way: saved as a draft with create_lab_note.