← Context/ plans
Plan C4 — A Remote MCP Connector for the Claude App
Let Claude on the phone search the archive, read a message, and open an attachment, through a login only Michael can pass.
Spec: Attachments-as-First-Class-Mail, Cloud, step C4. Related: A-Gateway-Between-Agents-and-My-Accounts, Attachments-Search-and-a-Knowledge-Graph (The phone gets in through Claude).
Goal
Michael adds one custom connector in the Claude app. After that, Claude anywhere (phone included) can search his mail and open attachments, and nobody else can.
This plan has two stages with a hard stop between them. Stage 1 needs Michael's sign-off, because the login is the whole security story.
Stage 1: research and a spec (can start now)
Research, from current docs, not memory. Cite every source.
How Claude's custom (remote MCP) connectors authenticate today: OAuth flow, dynamic client registration, token lifetimes, which transports (streamable HTTP, SSE).
Cloudflare's options for a remote MCP server: the
agentsSDK (McpAgent),@cloudflare/workers-oauth-provider, Cloudflare Access in front of a Worker. For each: what it needs, how upstream login works (Google sign-in restricted tomichael@humain.vc? GitHub? Access with a one-time PIN?), and how it would fit Claude's flow.How the tree's other connectors in the Claude app do it (MemoPop, the event CRM). Look for their code in the monorepo (
ai-labs/memopop-ai,ai-labs/mcps/) and say what can be reused.
Write
context-v/specs/Mail-Connector-for-the-Claude-App.mdin the repo's spec style (seeAttachments-as-First-Class-Mail.mdfor frontmatter and tone):The recommended login, with the alternatives and why not.
Tools:
search_mail(D1 FTS),read_message(D1 row, body from the R2.eml, cut at 30,000 characters),list_attachments,get_attachment(metadata, plus a way to get the file: a short-lived signed R2 URL or the bytes; recommend one). Same names and argument shapes asmail/kaymcp.py, so prompts carry over. No drafting, no sending.Security rules: one allowed identity; every tool read-only; email bodies, filenames and attachment contents inside the untrusted-content markers
kaymcp.pyuses; every call logged (where?); token lifetime and revocation; what an attacker who controls one email can and can't do through it.Where it lives: a new Worker under
kay-os/connector/, reading D1 and R2 with read-only intent. Can it get read-only bindings, or only by convention? Say which.Cost and limits.
Open questions for Michael.
Stop. Hand back the spec path and a short summary. Build nothing.
Stage 2: build
Rewritten 2026-11-27, after the spec's amendment. Build v1 of the connector: login through id.didi.sh and the four mail tools. Calendar tools come in a later plan, once calendar data is in D1.
Read first
context-v/specs/Mail-Connector-for-the-Claude-App.md, the amendment at the top wins over the research sections below it.AGENTS.md(the hard rule and the read/write split; the connector sends them to every client).mail/kaymcp.pyandmail/test_kaymcp.py: tool names, arguments, untrusted-content markers, and behaviour to match.sync-worker/: how this repo builds a Worker (wrangler.jsonc, hand-written runtime types insrc/cloudflare-workers.d.ts,src/db.tsand thetest/sqlite-db.tsD1 stand-in,test/memory-bucket.ts, node test runner, no frameworks), andmigrations/0001_index.sqlfor the tables it reads.id.didi.sh, read-only:
https://id.didi.sh/.well-known/oauth-authorization-serverand/.well-known/jwks.json(one Ed25519 key), and inai-labs/id-didi-shonorigin/main:lib/id_didi_sh/oauth.ex(token claims, resource matching) andconfig/config.exs(oauth_resources).
Build
connector/, a new Worker namedkay-os-connector, its ownwrangler.jsonc,package.json,tsconfig.json, insync-worker/'s style. Bindings: D1INDEX→kay-os-mail-index(ide23c087c-9638-432e-9f3a-1980a6412fcd), R2MAIL→kay-os-mail, and a second D1LOGfor the call log (database to be created by the parent; leave a placeholder id and say so). Route:{ "pattern": "mcp.kay-os.lol", "custom_domain": true }. Variables:ALLOWED_DIDI_ID,RESOURCE=https://mcp.kay-os.lol,ISSUER=https://id.didi.sh. No secrets are needed: tokens are verified with id.didi.sh's public key.Discovery and 401.
GET /.well-known/oauth-protected-resource(and the/mcp-suffixed path) returns RFC 9728 metadata naminghttps://id.didi.shas the authorization server andRESOURCEas the resource. Any MCP request without a valid token gets401withWWW-Authenticate: Bearer resource_metadata="…".Token check (
src/auth.ts): fetch and cache the JWKS (cache an hour, refetch once on an unknownkid), verify the EdDSA signature with WebCrypto Ed25519, and requiretypat+jwt,iss=ISSUER,aud=RESOURCE, unexpiredexp, andsub=ALLOWED_DIDI_ID. IfALLOWED_DIDI_IDis unset, refuse everyone but log thesubthat tried (only the id), so the first sign-in tells the parent which id to allow.MCP over streamable HTTP at
POST /mcp, JSON-RPC:initialize(with the instructions string fromkaymcp.py, hard rule included),tools/list,tools/call,ping; notifications get202. Plain JSON responses are enough; no SSE stream is needed for these tools. Follow the current MCP spec's streamable-HTTP rules for theMcp-Session-Idheader if Claude sends one; keep the server stateless otherwise.Tools, read-only, same names and arguments as
kaymcp.py:search_mail(D1 FTS5, same filters andsnippet()),read_message(D1 row; body from the R2.emlparsed withpostal-mime, cut at 30,000 characters; thread bythread_id; attachments from D1 without noise),list_attachments,get_attachment(metadata and the messages it came on, plus a link the Worker serves itself:GET /files/<sha>?t=<token>, a 15-minute HMAC-signed token keyed by a secretFILE_LINK_KEY, decoding the file from the.emland checking its SHA-256).draft_replyis not included.Untrusted content, per the spec: every sender-written field (bodies, subjects, senders, filenames, snippets) inside markers with a random tag per response, and any marker look-alikes in the content defused.
Call log: one row per tool call in
LOG(time, tool, arguments, ok or error,sub), via amigrations/0001_log.sqlinconnector/.Tests with the D1 stand-in and memory bucket: discovery documents; 401 without a token; a token signed by a test Ed25519 key is accepted, and wrong
aud, wrongiss, expired, and wrongsubare each refused; unsetALLOWED_DIDI_IDrefuses and logs thesub; each tool against fixture data (reusesync-worker/test/fixtures/deck.eml); an email that says "ignore previous instructions" comes back inside markers; a forged closing marker in a body is defused; the file link checks its signature, expiry, and SHA-256.
Done when
pnpm testand the pinnedtsc --noEmitpass inconnector/; thesync-worker/and Python suites still pass.A local run (
nodeagainst the handler with the stand-ins) answersinitialize,tools/list, and asearch_mailcall with a test token.
Hand back
Branch and commits, test counts, anything in the spec that didn't hold up
against the MCP or Claude docs, and what the parent must do before deploying
(create the LOG database, set FILE_LINK_KEY, register the resource in
id.didi.sh, set ALLOWED_DIDI_ID after the first sign-in).
Rules for whoever builds this
Stage 2: branch
cloud-c4-connectoroffdevelopment. No deploy, no--remote, nocf.sh, no changes to id.didi.sh. New dependencies: onlypostal-mime(same pinned version assync-worker/).Stage 1 writes one new spec file and nothing else. Branch
cloud-c4-connector-specoffdevelopment. Commit with the repo's conventions, ending with theCo-Authored-Byline. Don't push or merge.Never touch Cloudflare, the Claude app, or any OAuth provider. Research is reading only.
Never read, print, or copy secrets.
Mark anything you couldn't verify from a primary source as unverified.
Hand back
Stage 1: the spec path, a five-line summary of the recommended login and why, and the open questions.