Principle:21st dev Magic mcp API Key Management
| Knowledge Sources | |
|---|---|
| Domains | Authentication, Configuration |
| Last Updated | 2026-10-19 16:40 GMT |
Overview
A configuration pattern that obtains a fresh API key from an issuer, accepts it in every legacy input form (argv/env), and supplies it as a bearer/header credential to an authenticated service — with one-time resolution and no mid-process re-read.
Description
API key management is the act of acquiring a key, accepting it through all the input forms a population of legacy clients may pass, and providing it to an authenticated service on each request. It addresses two limitations: (1) keys are resettable credentials that lapse, so the issuer must be the source of truth for fresh ones; and (2) when a client's install command is baked into countless configs and model memories, the key may arrive in many syntactic forms (positional, flag, env), and a single resolver must normalize them all. The resolved value is read once at startup and never re-read — a key cannot become valid inside a running process, so re-reading would only mask a settled refusal.
Usage
Use this principle when designing a client that authenticates to a service with a key that may be passed in multiple legacy forms. Acquire the key from the issuer (web console); accept it via argv patterns and env fallbacks in a defined precedence; resolve once at startup; send it as a header on every request. On auth failure, the recovery is to restart with a fresh key from the issuer, not to retry within the same process.
Theoretical Basis
The core operation is a one-time, precedence-ordered resolution:
# Abstract algorithm description (NOT real implementation)
function resolve_key(argv, env):
# 1. Try every accepted argv form (positional, --flag, /flag:, -flag=, -flag value)
for form in argv_patterns:
if match(argv, form): return strip_quotes(value)
# 2. Fall back to env in precedence order
for name in env_precedence:
if env[name]: return env[name]
return undefined
key = resolve_key(argv, env) # once, at startup; never re-read
header = { "x-api-key": key } # sent on every request
The one-time resolution is intentional: a 401 means the key is invalid for the life of the process, so re-reading would only delay the same refusal.
Practical Guide
- Acquire a fresh key from the issuer (e.g. https://21st.dev/mcp); old keys may be invalid.
- Accept all legacy forms (positional `KEY=value`, `--KEY=value`, `/KEY:value`, `-KEY value`, env vars) with a single resolver.
- Define and document the resolution precedence (argv first, then env).
- Resolve once at startup; never re-read mid-process.
- Send the key as the auth header on every request.
- On auth failure, restart with a fresh key — don't retry in the same process.
Related Pages
Implemented By
Notes
- Feeds Principle:21st_dev_Magic_mcp_Auth_Failure_Recovery (the latch that makes one-time resolution safe).
- The resolved key is sent by Principle:21st_dev_Magic_mcp_MCP_Transport_Proxy on every proxied request.