MCP server project (TypeScript) · Chapter 7 of 12
A token per member
By the end of this chapter every request to Parda's MCP server carries a token that one member made in the Parda app. The server keeps only the token's SHA-256 hash, knows from it who is calling and what the member allowed, answers 401 to anyone without a live one, and lets the member revoke it on its own. Over stdio the same token comes from the environment. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: One member at a time
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of MCP server project (TypeScript) are free. This one comes with the all labs plan.
Why you need this
In October Parda kept 4 integrations, and every one of them logged in with somebody's password or cookie. The custom GPT action had a member's password. The film club's script ran with Asha's admin session, so it could do everything an admin does: cancel a showtime, change a price, see every member's bookings. If its file had leaked, the only fix was to change Asha's password, which also logs Asha out everywhere.
Sayali's rule for 5 November: an assistant acts for one member, with only what that member gave it, and the member can take it back without changing their password.
The server from chapter 6 cannot keep that rule. It has no idea who is calling. currentMember(extra) found no authInfo on an HTTP request, so it returned the development member from config.ts: Neha, user 2, for every call. Anyone who could reach /mcp acted as Neha, and Pranav's assistant would have listed Neha's bookings.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
tokens.ts: making and revoking tokens
Write newSecret, hashSecret, createToken and revokeToken in src/mcp/tokens.ts. newSecret() is pmcp_ and randomBytes(32) in base64url; hashSecret(secret) is its SHA-256 in hex. createToken(userId, name, scopes) refuses anyone who is not a member (forbidden, 403) and scopes that are empty or not in SCOPES (AppError 422 bad_scopes), stores a row in mcp_tokens with the secret's hash, its first 12 characters as prefix, the scopes in SCOPES' order and expiresAt TOKEN_DAYS from now, and returns { token: the row, secret }. revokeToken(userId, tokenId) sets revokedAt on the member's own live token with one update and throws notFound when no row changed.
The Check makes tokens with your functions and through POST /mcp/tokens as Neha (201, the secret shown once, only its hash in mcp_tokens), as Asha (403) and with a scope that does not exist (422), then revokes one of Neha's tokens as Pranav (404) and as Neha (204).
- Task 2
auth.ts: who is calling
Write lookup(secret) and verifier.verifyAccessToken(token) in src/mcp/auth.ts. lookup finds the token whose tokenHash is hashSecret(secret), with its user's role, and returns null for none, a revoked token, an expired one or one whose user is not a member; otherwise it sets lastUsedAt and returns the SDK's AuthInfo { token, clientId: the user's id as text, scopes, expiresAt in seconds }. verifyAccessToken returns lookup's AuthInfo, or throws InvalidTokenError when it is null.
The Check sends POST /mcp to the whole app with no token (401 with a WWW-Authenticate header), with a made-up token (401), with Neha's lab token (find_showtimes works, last_used_at is set), with a token it revokes, one it makes expire and one of Asha's (401 each).
- Task 3 · interview question
Interview: auth.ts, over stdio
Write stdioMember() in src/mcp/auth.ts. It reads PARDA_MCP_TOKEN from the environment (trimmed) and returns { id, scopes } for its member, or throws Error("PARDA_MCP_TOKEN is not set: make a token in the Parda app and put it in your client's config") when it is empty, or Error("PARDA_MCP_TOKEN is not a live Parda MCP token: make a new one in the Parda app") when lookup finds nothing.
The Check calls list_my_bookings over HTTP with Pranav's token and with Neha's (each sees only their own bookings), then starts main.ts --stdio with Pranav's token, with a wrong one and with none.
Nikhil will ask: the database leaks tomorrow. What can the attacker do with mcp_tokens, and what could they do if it held the tokens themselves?
What you will learn
- Bearer tokens: the Authorization header, and why each member and each app gets its own.
- Storing a token: why only a SHA-256 hash and a short prefix go in the database.
- Scopes and revoking: what a token may do is chosen when it is made; one update takes it back.
- The SDK's bearer check: requireBearerAuth, your verifier, InvalidTokenError and the real 401.
- stdio: the token from PARDA_MCP_TOKEN, checked once at start.
- The lab tokens: known secrets for development only, like the seed's passwords.
More in One member at a time
Build "A token per member" yourself
Parda wants AI assistants like Claude Desktop to book seats for its members, safely. You build the MCP server that lets them, and only as the member who allowed it. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of MCP server project (TypeScript) are free. This one comes with the all labs plan.