Express.js Middleware for A2A, MCP & RFC 9728 Metadata
Mount /.well-known/agent-card.json, /.well-known/mcp/server.json, and RFC 9728 oauth-protected-resource routes in Express.js — avoiding the express.static dotfiles trap and emitting compliant HTTP 401 WWW-Authenticate headers.
Verify live A2A, MCP, OAuth & AEO discovery in production
In RungLab's N=140 B2B SaaS Readiness Benchmark, 98.4% (62/63) of full-telemetry domains lacked agent-card.json. Audit your live domain with ReadyCheck, cut MCP schema token tax with MCPDiet, or automate fleet discovery checks via Apify & RapidAPI.
Serve with `Content-Type: application/json; charset=utf-8` (never `text/plain` or SPA HTML fallback).
Include `Access-Control-Allow-Origin: *` so browser-based agent inspectors and web clients can fetch discovery JSON.
Use `Cache-Control: public, max-age=300` instead of `immutable` so skill/scope updates propagate quickly.
Return `WWW-Authenticate: Bearer resource_metadata="https://<domain>/.well-known/oauth-protected-resource"` on HTTP 401 (prevents `MCP-PRO-002`).
Live Well-Known Audit98.4% Lack Agent Cards
Verify live A2A, MCP, OAuth & AEO discovery in production
In RungLab's N=140 B2B SaaS Readiness Benchmark, 98.4% (62/63) of full-telemetry domains lacked agent-card.json. Audit your live domain with ReadyCheck, cut MCP schema token tax with MCPDiet, or automate fleet discovery checks via Apify & RapidAPI.
If Using express.static(), Enable dotfiles: 'allow' Safely
express-static-dotfiles.mjs
// By default, express.static() returns 404 for dotfiles like /.well-known/*!
// Only allow the specific /.well-known directory — NEVER expose root .env files:
app.use(
"/.well-known",
express.static("public/.well-known", {
dotfiles: "allow",
setHeaders(res) {
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.setHeader("Access-Control-Allow-Origin", "*");
}
})
);
Key Empirical & Architectural Takeaways
express.static() ignores dotfiles (paths starting with `.`) by default (`dotfiles: 'ignore'`), silently causing /.well-known/* static files to return 404.
Never enable dotfiles: 'allow' on your entire public/ folder if .env or .git could live there — scope static mounting strictly to '/.well-known' or serve typed JSON handlers via express.Router().
Mounting app.use(helmet()) with default Cross-Origin-Resource-Policy: same-origin without allowing cross-origin reads on /.well-known/* if third-party web tools fetch it.
Returning 401 JSON without the WWW-Authenticate response header.
Placing a catch-all app.get('*', ...) SPA handler before the /.well-known router.
More Agent Discovery Specifications & Framework Guides
Author valid Model Context Protocol server.json manifests for the official MCP Registry and self-hosted /.well-known/mcp/server.json discovery with Streamable HTTP and clean token budgets.
Implement RFC 9728 OAuth 2.0 Protected Resource Metadata and HTTP 401 WWW-Authenticate headers so MCP clients dynamically discover your Authorization Server without manual client setup.
Deploy /.well-known/agent-card.json, /.well-known/mcp/server.json, and /.well-known/oauth-protected-resource on Cloudflare Workers and Cloudflare Pages with strict JSON MIME types and CORS.
Why does Express return 404 for public/.well-known/agent-card.json?
express.static defaults to dotfiles: 'ignore', treating '.well-known' as a hidden dotfile. Either mount dedicated router.get('/.well-known/...') JSON routes or mount express.static strictly on '/.well-known' with dotfiles: 'allow'.
Is setting dotfiles: 'allow' on app.use(express.static('public')) safe?
No — only enable dotfiles: 'allow' on a narrow '/.well-known' directory mount, or better yet, serve explicit route handlers so unrelated dotfiles can never leak.
How do I avoid CORS errors when browser MCP/A2A inspectors test my Express server?
Attach Access-Control-Allow-Origin: * and respond 204 to OPTIONS requests under '/.well-known'.