understanding-mcp
A zero-dependency Node.js playground for debugging how MCP clients authenticate to an MCP server over HTTP.
README
understanding-mcp
A zero-dependency Node.js playground for debugging how MCP clients authenticate to an MCP server over HTTP. It runs two servers in one process:
- MCP server (resource server) on
http://localhost:3001— a minimal streamable-HTTP MCP server (initialize,tools/list,tools/call,ping). - OAuth authorization server on
http://localhost:3002— discovery metadata, Dynamic Client Registration (DCR), authorization + token endpoints.
Every request and every response — headers included — is printed to the console and
kept in a ring buffer you can fetch from GET /__log on either server. Point a real
MCP client at http://localhost:3001/mcp (or run node client.js) and watch the whole
OAuth handshake happen hop by hop.
No npm install, no dependencies. Requires Node 18+.
Quick start
node server.js # terminal 1: watch every request/response
node client.js # terminal 2: drive the full flow end-to-end
client.js models how a real MCP client connects:
- POST an MCP request without a token.
- Read the
WWW-Authenticatechallenge. If it carriesresource_metadata, use that URL; otherwise construct it from the MCP endpoint path:<mcp_base_url>/.well-known/oauth-protected-resource/<url_remaining_path>. - Fetch the protected resource metadata, read
authorization_servers. - Discover the authorization server metadata by building
<auth_server_base_url>/.well-known/oauth-authorization-server/<authz_server_remaining_url>. - If
registration_endpointis advertised → DCR (POST /register). If it is absent → skip straight to the preconfigured client_id. - Run the authorization-code flow (PKCE S256 +
resourceparameter +issvalidation), exchange the code for an access token. - Retry the MCP request with
Authorization: Bearer <token>.
Endpoints
| Server | Endpoint | Purpose |
|---|---|---|
| MCP (3001) | POST /mcp |
Streamable-HTTP MCP endpoint. No/expired token → 401 with WWW-Authenticate. Missing scope → 403 insufficient_scope. |
| MCP (3001) | GET /.well-known/oauth-protected-resource/mcp |
RFC 9728 protected resource metadata. Only the path derived from the MCP endpoint serves it; any other /.well-known/oauth-protected-resource* (including the root, when the MCP server lives at /mcp) returns 404. Non-GET methods return 405. |
| MCP (3001) | GET /__log |
Recent request/response log entries as JSON. |
| AUTH (3002) | GET /.well-known/oauth-authorization-server |
RFC 8414 authorization server metadata. |
| AUTH (3002) | POST /register |
RFC 7591 Dynamic Client Registration. |
| AUTH (3002) | GET /authorize |
Authorization endpoint (auto-approves, redirects with code, state, iss). |
| AUTH (3002) | POST /token |
Token endpoint (authorization_code with PKCE, and client_credentials). |
| AUTH (3002) | GET /__log |
Recent request/response log entries as JSON. |
Configuration
All settings are environment variables with sensible defaults.
| Variable | Default | Meaning |
|---|---|---|
MCP_URL |
http://localhost:3001/mcp |
Canonical URI of the MCP server. Everything else (listen port, endpoint path, resource audience, well-known metadata path) is derived from it. |
MCP_PORT |
(from MCP_URL) |
Override for the MCP server's listen port. |
MCP_BASE_URL |
(origin of MCP_URL) |
Override for the origin used to build well-known URLs. |
MCP_ENDPOINT |
(path of MCP_URL) |
Override for the MCP endpoint path. |
AUTH_PORT |
3002 |
Port of the authorization server. |
AUTH_BASE_URL |
http://localhost:3002 |
Issuer of the authorization server. |
WWW_AUTH_RESOURCE_METADATA |
on |
off → 401 challenges omit resource_metadata, forcing the client to construct the well-known URI itself. |
AUTH_DCR |
on |
off → registration_endpoint is not advertised and /register is disabled (forces the preconfigured-client path). |
DCR_ALLOWED_REDIRECT_URIS |
(empty = allow any) | Comma-separated whitelist. When set, DCR rejects any redirect_uri not on the list. |
PRECONFIGURED_CLIENT_ID |
preconfigured-client |
Static client_id available without registration (public client, no secret). |
PRECONFIGURED_REDIRECT_URIS |
http://localhost:8899/callback |
redirect_uris allowed for the preconfigured client. |
TOKEN_TTL_SECONDS |
3600 |
Access token lifetime. |
Reproduce each scenario
# 1. DCR succeeds -> token issued to a dynamically registered client
node server.js
node client.js
# 2. DCR advertised, but registration rejected (redirect URI not whitelisted)
# -> client falls back to the preconfigured client_id
DCR_ALLOWED_REDIRECT_URIS='http://only-whitelisted.example/callback' node server.js
node client.js
# 3. No DCR at all -> client uses the preconfigured client_id directly
AUTH_DCR=off node server.js
node client.js
# 4. 401 challenge without resource_metadata -> client derives the well-known URI
WWW_AUTH_RESOURCE_METADATA=off node server.js
node client.js
You can also connect a real MCP client to http://localhost:3001/mcp and watch the
handshake in the server log.
Findings worth blogging about
These are the non-obvious behaviors the log surfaces. All four are reproducible above.
1. WWW-Authenticate may or may not tell you where the metadata lives
The 401 challenge can carry resource_metadata="<uri>" (RFC 9728 §5.1), or just
Bearer scope="...". When it does not, the client has to guess the URL by
inserting the MCP endpoint's path:
<mcp_base_url>/.well-known/oauth-protected-resource/<url_remaining_path>.
Set WWW_AUTH_RESOURCE_METADATA=off and the log shows the client doing exactly this
derivation.
2. Metadata gives you no guarantee that DCR will succeed
This is the big one. The presence of registration_endpoint only means the endpoint
exists — it says nothing about whether your registration will be accepted. Real
servers (e.g. Figma) advertise DCR but enforce a redirect-URI whitelist, so a
client registering its own redirect_uri gets a 400 invalid_redirect_uri.
Reproduce it: set DCR_ALLOWED_REDIRECT_URIS to anything that isn't the client's
callback. The metadata still advertises registration_endpoint, /register still
responds, and registration still fails. A robust client therefore treats DCR as
best-effort and falls back to a preconfigured client_id when registration is
rejected — client.js prints this fallback explicitly.
3. Preconfigured client_id skips DCR entirely
When the client already has a client_id for this server (many MCP clients let you
configure one), it never calls /register. It goes straight to
authorization_endpoint + token_endpoint from the metadata. Set AUTH_DCR=off to
see the pure preconfigured flow.
4. Scope challenges happen at runtime too
A token with mcp but not tools:execute gets a 403 with
WWW-Authenticate: Bearer error="insufficient_scope", scope="tools:execute" when
calling tools/call — a step-up authorization trigger, not a login failure.
Manual debugging
Without client.js, you can drive the flow by hand:
# discover
curl -i http://localhost:3001/mcp -X POST -H 'Content-Type: application/json' \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{}}}'
curl http://localhost:3001/.well-known/oauth-protected-resource/mcp
curl http://localhost:3002/.well-known/oauth-authorization-server
# dynamic client registration
curl -i http://localhost:3002/register -X POST -H 'Content-Type: application/json' \
-d '{"client_name":"manual","application_type":"native","redirect_uris":["http://localhost:8899/callback"]}'
# authorize (auto-approves; paste into a browser and read the Location header)
curl -i "http://localhost:3002/authorize?response_type=code&client_id=<client_id>&redirect_uri=http%3A%2F%2Flocalhost%3A8899%2Fcallback&scope=mcp+tools%3Aexecute&code_challenge_method=S256"
# token
curl -i http://localhost:3002/token -X POST -H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=authorization_code&code=<code>&redirect_uri=http://localhost:8899/callback&client_id=<client_id>&code_verifier=<verifier>'
# authenticated MCP call
curl -i http://localhost:3001/mcp -X POST -H 'Content-Type: application/json' \
-H 'Authorization: Bearer <access_token>' \
-d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"echo","arguments":{"message":"hi"}}}'
The default preconfigured client is a public client (preconfigured-client, no
secret, PKCE required, token_endpoint_auth_method: none). DCR-registered clients
behave the same unless application_type: web is sent, in which case they are issued
a client_secret and must authenticate at the token endpoint.
The flow
Client MCP Server (3001) Auth Server (3002)
|--- POST /mcp (no token) ---------->|
|<-- 401 + WWW-Authenticate ---------|
| (resource_metadata URI, |
| or client derives it) |
|--- GET /.well-known/oauth-protected-resource/mcp -->|
|<-- { authorization_servers: [...] }----------------|
|--- GET /.well-known/oauth-authorization-server -->| (3002)
|<-- { registration_endpoint?, authorize, token } --|
|--- POST /register (DCR) ---------->|
|<-- { client_id, client_secret } ---|
| (or registration rejected -> use preconfigured client_id)
|--- GET /authorize (PKCE+resource) ->|
|<-- 302 redirect_uri?code&iss ------|
|--- POST /token -------------------->|
|<-- { access_token } ---------------|
|--- POST /mcp (Bearer token) ------->|
|<-- 200 MCP JSON-RPC result --------|
What it deliberately does not do
- No real user login / consent UI —
/authorizeauto-approves so the flow is scriptable. - No refresh tokens, JWT, revocation, or OIDC userinfo. Scopes are
mcp(any access) andtools:execute(tools/call). - No Client ID Metadata Documents (the newer, preferred registration mechanism) — this repo focuses on the DCR vs preconfigured-client question.
- Tokens are opaque and stored in memory; restarting the server invalidates them.
Files
server.js— MCP resource server + OAuth authorization server + request/response logger.client.js— demo client that runs the discovery → DCR-or-preconfigured → OAuth → MCP flow.README.md— this file.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。