Running a multi-user MCP server for $0 on Cloudflare
Context is a remote MCP server. ChatGPT, Claude and other assistants connect to it over OAuth and use it as a shared memory for one person. Each user has their own memories and their own grants, and the assistants connect from the vendors' clouds rather than from a laptop. I'm one developer, and I wanted the running cost to be zero until people actually use it. This is how it's built, with the free-plan limits it has to fit inside and the places I expect it to break.
The whole thing is one Worker
ChatGPT / Claude / Gemini / Grok you, in a browser
| MCP over HTTPS + Bearer token |
v v
+---------------------------- one Worker -----------------------------+
| static assets (public/: CSS, fonts) served before the Worker runs |
| |
| OAuthProvider (@cloudflare/workers-oauth-provider) |
| /.well-known/*, /oauth/register, /oauth/token --> KV (grants, |
| tokens) |
| /mcp (valid token only) --> createMcpHandler --> src/mcp.ts |
| everything else --> Hono app (src/web.ts): |
| /login, /auth/google/callback, /authorize (consent), |
| /, /import, /connect, /history, /export ... |
| all --> D1 |
| scheduled(): daily 03:17 UTC --> purge expired history in D1 |
+----------------------------------------------------------------------+
There's one deployable, two storage bindings (D1 for data, and KV only because the OAuth library wants it), one cron trigger and a folder of static files. No Durable Objects, no queues, no vector index, and no LLM calls from the server.
OAuth: the part you can't skip
Claude and ChatGPT connect to remote MCP servers with OAuth. I didn't want to write an authorization server, so @cloudflare/workers-oauth-provider wraps the whole Worker. It serves discovery metadata, client registration and the token endpoint, and it checks the bearer token before a request ever reaches /mcp:
provider ??= new OAuthProvider<Env>({
apiRoute: "/mcp",
apiHandler: mcpApi,
defaultHandler: web,
authorizeEndpoint: "/authorize",
tokenEndpoint: "/oauth/token",
// Dynamic client registration: how Claude and ChatGPT sign themselves up today.
clientRegistrationEndpoint: "/oauth/register",
// Client ID Metadata Documents: the newer MCP registration style.
clientIdMetadataDocumentEnabled: true,
scopesSupported: ["memory"],
resourceMetadata: { resource: `${base}/mcp`, authorization_servers: [base], ... }
});
Clients can arrive two ways. With dynamic client registration they POST their own metadata to /oauth/register. With Client ID Metadata Documents (CIMD) the client id is an HTTPS URL that the library fetches. That fetch is why wrangler.jsonc enables the global_fetch_strictly_public compatibility flag: the library needs it as SSRF protection. The consent page uses the difference between the two. A CIMD client gets "Published by host", and a self-registered one gets "This app registered itself; its name is not verified." It also shows the host the authorization will be sent to.
The /authorize page is mine. If you aren't signed in, it redirects to Google. Then it either skips consent (if you already approved this app in this browser) or renders the "Let Claude use your Context?" page. On approval, the grant gets two props, userId and a normalized client name. The library stores those encrypted with the grant and hands them back on every MCP request as ctx.props. That's the entire multi-tenancy mechanism: every query in src/memories.ts is scoped by that userId.
Google is used only for identity, with the openid email scopes and PKCE. The ID token comes straight from Google's token endpoint over TLS, authenticated with the client secret. OpenID Connect allows trusting a token received that way without re-verifying its signature, so the callback only checks iss, aud, exp and email_verified. Users are keyed on Google's sub, not the email, because emails can change.
A stateless MCP handler
The common way to host MCP on Cloudflare uses a Durable Object per session. I don't need sessions. Every tool call is a self-contained read or write against D1, so each request builds a fresh server:
// A fresh MCP server per request keeps the Worker stateless: no Durable Objects to pay for.
const mcpApi = {
fetch(request: Request, env: Env, ctx: ExecutionContext) {
const props = (ctx as ExecutionContext & { props: AuthProps }).props;
return createMcpHandler(() => buildMcpServer(env, props), {
route: "/mcp",
allowedHostnames: [new URL(env.PUBLIC_URL).hostname]
})(request, env, ctx);
}
};
buildMcpServer registers six tools and closes over env.DB and the user's id. Building it is cheap: no I/O, just a few objects. The trade-off is that the server can't push anything to the client between requests, and nothing survives from one request to the next. For a memory store that's fine. For something that streams long-running progress, it wouldn't be.
Being stateless also made testing easy. The same buildMcpServer runs in-process in my test bench against a node:sqlite database shaped like D1.
D1 as the only real database
The schema is two tables, users and memories, plus seven small migrations. A memory is a single sentence with a category, a source ("ChatGPT", "Claude"), whether the user said it or an AI inferred it, and a status of active, superseded or forgotten. Edits on Pro insert a new row and mark the old one superseded, in one db.batch(). Free accounts overwrite in place.
One D1 quirk I hit: SQLite can't alter a CHECK constraint, so adding a category meant rebuilding the table. D1 enforces foreign keys and ignores PRAGMA foreign_keys=OFF. The migration uses PRAGMA defer_foreign_keys = true and renames the old table out of the way first. Building a new table and dropping the original would make every self-reference look orphaned at commit.
Keyword search in the Worker, not a vector DB
This is the choice that looks most like a shortcut. search_context loads the user's active memories and scores them in JavaScript:
export function searchMemories(memories: Memory[], query: string, limit: number): Memory[] {
const q = new Set(terms(query));
if (q.size === 0) return [];
return memories
.map((m) => {
const words = new Set(terms(`${m.content} ${CATEGORY_LABELS[m.category]}`));
let hits = 0;
for (const t of q) if (words.has(t)) hits++;
// Coverage of the query dominates; explicit facts win ties over inferences.
const score = hits / q.size + (hits > 0 ? m.explicit * 0.05 : 0);
return { m, score };
})
...
terms() lowercases, drops stopwords and does crude suffix stripping, so "decided" and "decision" meet. That's the whole search.
It's fine because of the shape of the data. One person has a few hundred short sentences, not a corpus. The caller is an LLM that writes a focused query and can call again with different words. get_context hands it a categorized overview of up to 150 memories, which covers most of what a typical account holds. And when there's no match, the tool says so and suggests a broader query or get_context. A vector index would add a second store to keep in sync and an embedding call on every write, and it would be one more thing to pay for. The code comment states the rule I'm following: swap in embeddings only if real usage shows keyword search missing relevant memories.
Import without an LLM
Moving your memory from ChatGPT to Context could have been "paste your chat export and we'll summarize it", which means an LLM bill on my side. Instead the import page gives you a prompt to paste into the assistant that knows you best. That assistant does the extraction on your subscription and answers in a strict line format:
[preferences] [said] [unknown] Prefers concise answers without long introductions.
[decisions] [said] [2023-11-20] Decided to stop pursuing cybersecurity to focus on entrepreneurship.
A regex parses it. Lines inside a code fence count, chatter outside the fence is dropped, and impossible or future dates become "unknown". One paste saves at most 2,500 rows, in batches of 500. The Markdown export uses the same line format, so an export can be imported again.
A session cookie without a session table
The web UI's login is a signed cookie: userId.sessionVersion.expiresAt.hmac, HMAC-SHA256 with a secret, in a __Host- cookie that's HttpOnly, Secure and SameSite=Lax, for 30 days. There are no session rows to create or clean up. For revocation, each user has a session_version column. Logout increments it, and every cookie carrying the old number stops working. So it isn't zero database access (each signed-in page reads one integer), but there's nothing to garbage-collect. POSTs also check the Origin header as a second layer against cross-site forms.
Write limits instead of rate limits
An assistant stuck in a loop is a more realistic threat than an attacker. So every write path (remember, update_memory, merge_memories, import, restore) asks writeAllowance first:
export const MAX_ACTIVE = 10_000;
export const MAX_TOTAL_ROWS = 30_000;
// ...one query: the user's plan, active count and total rows (history included)
const byTotal = MAX_TOTAL_ROWS - (row?.total ?? 0);
const byActive = opts.addsActive ? MAX_ACTIVE - (row?.active ?? 0) : Infinity;
Updates and merges don't add active memories, so only the total cap applies to them. That total cap exists because Pro keeps every old version, and an update loop would otherwise grow an account forever. When a cap is hit, the tool returns an error whose text tells the model what to do: merge or forget memories, or clean up on the website.
Cron and static assets
One cron trigger (17 3 * * *) calls purgeExpiredHistory, which deletes old versions for free accounts whose 30-day grace period has ended, in a single batch. Active memories are never touched.
CSS and self-hosted fonts live in public/ and are served as Workers static assets, with a _headers file making fonts immutable. Cloudflare's docs say requests to static assets are free and unlimited. Only requests that run the Worker script count.
The free plan limits this has to live within
From Cloudflare's docs as of today (Workers limits, D1 pricing, D1 limits, KV pricing, static assets billing):
| resource | free plan |
|---|---|
| Worker requests | 100,000 / day |
| CPU time per HTTP request | 10 ms |
| Subrequests per invocation | 50 |
| Cron triggers per account | 5 |
| D1 rows read | 5 million / day |
| D1 rows written | 100,000 / day |
| D1 database size / account storage | 500 MB / 5 GB |
| D1 queries per invocation | 50 (a db.batch() counts as one) |
| KV reads | 100,000 / day |
| KV writes, deletes, lists | 1,000 / day each |
| Static asset requests | free, unlimited |
When a D1 daily limit is exceeded, queries fail until the reset. They aren't throttled. A KV limit is the same: further operations of that type error until 00:00 UTC.
What breaks first, honestly
KV writes, not D1. This surprised me when I worked it out. The OAuth library keeps grants and tokens in KV, and by default an access token lasts one hour. In the library's code, a refresh writes the grant and a new access token: two KV writes. With 1,000 writes a day, that's about 500 refreshes, before counting new registrations and grants. At one refresh an hour, a couple of dozen people with assistants that refresh around the clock could use it all up. So I set the access-token lifetime to a day: a connected app refreshes about once a day, and the same budget covers a few hundred connected apps. The trade-off is that a leaked access token stays valid for longer, though deleting the account still revokes every grant and its tokens at once. Past that, the paid Workers plan removes the problem.
D1 rows read on search. Each search_context call reads every active memory the user has, and each write's allowance check counts all of their rows. At a few hundred memories per user, 5 million rows a day is still tens of thousands of searches, but it grows linearly with account size. The fix would be SQLite FTS5 in D1, so the database narrows the candidates instead of the Worker.
CPU at the high end. Scoring a few hundred sentences is about a millisecond, which is what the code comment estimates. I haven't measured an account at the 10,000-memory cap, and that's where I'd expect the 10 ms CPU limit to bite first.
Requests. Each MCP message is an HTTP request to the Worker, so 100,000 a day is generous for one person and finite for a product. Static assets don't count, which helps the web UI.
What I'd change with real traffic: FTS5 before embeddings, the paid plan once KV writes get close to the limit, and per-minute rate limiting on /mcp, which today relies only on the write caps. Durable Objects come in only if I need server-initiated messages. None of these require changing the overall architecture, which was the point of keeping it this simple.
"$0" here means it's designed to fit inside Cloudflare's free plan. The domain costs money, and Stripe takes its cut only when someone pays for Pro. The free plan of Context itself is unlimited for personal use. If you want to see what this architecture looks like from the user's side, it's running at keepyourcontext.com, and you can connect it to Claude or ChatGPT. The full source is on GitHub, so everything described here can be read in the code.