MCP server project · Chapter 7 of 12

A token per member

By the end of this chapter every request to DabbaDrop's MCP server carries a token that one member made in the DabbaDrop 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 with one click. 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
  • Python
  • In the all labs plan

The first 2 chapters of MCP server project are free. This one comes with the all labs plan.

Why you need this

In October DabbaDrop kept 4 integrations, and every one of them logged in with somebody's password or cookie. The custom GPT action had Tara's password. The office script ran with Bhavna's admin session, so it could do everything an admin does: close the 12:30 Thane slot, change a menu, see every member's orders. If its file leaked, the only fix was to change Bhavna's password, which also logged Bhavna out everywhere.

Farah's rule for launch week: 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. current_member() returned the development member from the settings, Tara (user 2), for every request, so anyone who could reach /mcp acted as Tara. Advait's assistant would have seen Tara's orders.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    tokens.py: making and revoking tokens

    Write new_secret, hash_secret, create_token and revoke_token in mcp_server/tokens.py. new_secret() is dmcp_ and secrets.token_urlsafe(32); hash_secret(secret) is its SHA-256 in hex. create_token(db, user_id, 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 an McpToken with the secret's hash, its first 12 characters as prefix and the scopes in SCOPES' order, commits, and returns (the row, the secret). revoke_token(db, user_id, token_id) sets revoked_at on the member's own live token with one UPDATE and raises not_found when no row changed.

    The Check makes tokens with your functions and through POST /mcp/tokens as Tara (201, the secret shown once, only its hash in mcp_tokens), as Bhavna (403) and with a scope that does not exist (422), then revokes one of Tara's tokens as Advait (404) and as Tara (204).

  2. Task 2

    auth.py: who is calling

    Write lookup(secret) and DabbaTokenVerifier.verify_token(token) in mcp_server/auth.py. lookup finds the McpToken whose token_hash is hash_secret(secret), with its user's role, and returns None for none, a revoked token, an expired one or one whose user is not a member; otherwise it sets last_used_at, commits, and returns AccessToken(token, client_id the user's id as text, scopes, expires_at). verify_token runs lookup in a worker thread (anyio.to_thread.run_sync).

    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 Tara's lab token (find_slots works), with a token it revokes and one it makes expire (401), and reads last_used_at.

  3. Task 3 · interview question

    Interview: auth.py, over stdio

    Write stdio_member() in mcp_server/auth.py. It reads DABBA_MCP_TOKEN from the environment and returns Member(id, scopes) for it, or raises SystemExit("DABBA_MCP_TOKEN is not set: make a token in the DabbaDrop app and put it in your client's config") when it is empty, or SystemExit("DABBA_MCP_TOKEN is not a live DabbaDrop MCP token: make a new one in the DabbaDrop app") when lookup finds nothing.

    The Check calls list_my_orders over HTTP with Advait's token and with Tara's (each sees only their own orders), then starts python -m mcp_server --stdio with Advait's token, with a wrong one and with none.

    Kunal 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 token verifier: the real 401 with WWW-Authenticate, and why the database call runs in a worker thread.
  • stdio: the token from DABBA_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

DabbaDrop wants AI assistants like Claude Desktop to order 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 are free. This one comes with the all labs plan.