Backend Framework · dotfiles: allow

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.

Manifest Parameters

Zero Telemetry · Strict CSP
Authentication & OAuth Scopes
MCP Registry server.json Options
Exact Well-Known Endpoint URL application/json
https://agent.example.com/.well-known/mcp/server.json
/.well-known/mcp/server.json
{
  "$schema": "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json",
  "name": "com.example/acme-assistant",
  "title": "Acme Cloud Assistant",
  "description": "Autonomous API diagnostics, billing lookups, and workflow execution via A2A and MCP.",
  "version": "1.0.0",
  "websiteUrl": "https://docs.example.com/agents",
  "repository": {
    "url": "https://github.com/example/acme-mcp",
    "source": "github"
  },
  "remotes": [
    {
      "type": "streamable-http",
      "url": "https://agent.example.com/mcp",
      "headers": [
        {
          "name": "Authorization",
          "description": "OAuth 2.1 or Bearer token (never embed static secrets in server.json)",
          "isRequired": true,
          "isSecret": true
        }
      ]
    }
  ]
}
curl Verification Command
curl -fsSL -D - "https://agent.example.com/.well-known/mcp/server.json" | head -n 40
HTTP 401 WWW-Authenticate Header (RFC 9728 / MCP-PRO-002)
WWW-Authenticate: Bearer resource_metadata="https://agent.example.com/.well-known/oauth-protected-resource", scope="agent:invoke mcp:tools mcp:read"
Live Well-Known Audit 98.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.

Required Endpoints, Paths & MIME Types

Path / Endpoint Purpose Required Content-Type
/.well-known/agent-card.json Express GET router handler for A2A v0.3 card application/json; charset=utf-8
/.well-known/oauth-protected-resource Express GET router handler paired with 401 WWW-Authenticate middleware on /mcp application/json; charset=utf-8

Express.js Discovery Router + Protected /mcp 401 Middleware

agent-discovery-router.mjs
import express from "express";

export function createAgentDiscoveryRouter({ baseUrl, authServerUrl, scopes = ["mcp:read", "mcp:tools"] }) {
  const router = express.Router();

  router.use("/.well-known", (req, res, next) => {
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS");
    res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
    res.setHeader("Cache-Control", "public, max-age=300");
    if (req.method === "OPTIONS") return res.status(204).end();
    next();
  });

  router.get("/.well-known/agent-card.json", (_req, res) => {
    res.type("application/json").json({
      protocolVersion: "0.3.0",
      name: "Acme Express Agent",
      description: "Autonomous workflow execution and MCP tool server.",
      url: `${baseUrl}/a2a`,
      preferredTransport: "JSONRPC",
      version: "1.0.0",
      capabilities: { streaming: true, pushNotifications: false, stateTransitionHistory: true },
      skills: [{ id: "query", name: "Query Workspace", description: "Search bounded account telemetry." }]
    });
  });

  router.get("/.well-known/oauth-protected-resource", (_req, res) => {
    res.type("application/json").json({
      resource: `${baseUrl}/mcp`,
      authorization_servers: [authServerUrl],
      scopes_supported: scopes,
      bearer_methods_supported: ["header"]
    });
  });

  return router;
}

export function requireMcpBearer(baseUrl, scopes = ["mcp:read"]) {
  return (req, res, next) => {
    const auth = req.headers.authorization || "";
    if (!auth.startsWith("Bearer ")) {
      res.setHeader(
        "WWW-Authenticate",
        `Bearer resource_metadata="${baseUrl}/.well-known/oauth-protected-resource", scope="${scopes.join(" ")}"`
      );
      return res.status(401).json({ error: "unauthorized" });
    }
    next();
  };
}

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().
  • Adding WWW-Authenticate: Bearer resource_metadata="..." inside your 401 middleware eliminates MCP-PRO-002 auth discovery failures.

Common Production Pitfalls to Avoid

  • 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

← Back to AI Agent Discovery Generator & Validator Hub

Frequently Asked Questions

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'.