Backend Framework · Route Handlers + Middleware

Next.js App Router /.well-known/* Route Handlers & Headers

Serve /.well-known/agent-card.json, /.well-known/mcp/server.json, and /.well-known/oauth-protected-resource cleanly in Next.js App Router without middleware auth redirects or trailing-slash loops.

Manifest Parameters

Zero Telemetry · Strict CSP
Authentication & OAuth Scopes
A2A Agent Card (v0.3) Options
Exact Well-Known Endpoint URL application/json
https://agent.example.com/.well-known/agent-card.json
/.well-known/agent-card.json
{
  "protocolVersion": "0.3.0",
  "name": "Acme Cloud Assistant",
  "description": "Autonomous API diagnostics, billing lookups, and workflow execution via A2A and MCP.",
  "url": "https://agent.example.com/a2a",
  "preferredTransport": "JSONRPC",
  "version": "1.0.0",
  "provider": {
    "organization": "Acme Corp",
    "url": "https://agent.example.com"
  },
  "documentationUrl": "https://docs.example.com/agents",
  "capabilities": {
    "streaming": true,
    "pushNotifications": false,
    "stateTransitionHistory": true
  },
  "defaultInputModes": [
    "text/plain",
    "application/json"
  ],
  "defaultOutputModes": [
    "application/json",
    "text/plain"
  ],
  "securitySchemes": {
    "oauth2": {
      "type": "oauth2",
      "flows": {
        "authorizationCode": {
          "authorizationUrl": "https://auth.example.com/oauth/authorize",
          "tokenUrl": "https://auth.example.com/oauth/token",
          "scopes": {
            "agent:invoke": "Authorize agent:invoke access",
            "mcp:tools": "Authorize mcp:tools access",
            "mcp:read": "Authorize mcp:read access"
          }
        }
      }
    }
  },
  "security": [
    {
      "oauth2": [
        "agent:invoke",
        "mcp:tools",
        "mcp:read"
      ]
    }
  ],
  "skills": [
    {
      "id": "diagnose-api",
      "name": "Diagnose API Errors",
      "description": "Inspect trace IDs, rate limits, and webhook delivery status.",
      "tags": [
        "diagnostics",
        "support"
      ],
      "examples": [
        "Run Diagnose API Errors for the current workspace"
      ]
    },
    {
      "id": "lookup-usage",
      "name": "Query Account Usage",
      "description": "Return bounded billing-period token and quota usage metrics.",
      "tags": [
        "billing",
        "analytics"
      ],
      "examples": [
        "Run Query Account Usage for the current workspace"
      ]
    }
  ]
}
curl Verification Command
curl -fsSL -D - "https://agent.example.com/.well-known/agent-card.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
app/.well-known/agent-card.json/route.ts Dynamic or static App Router GET handler for A2A v0.3 discovery application/json; charset=utf-8
app/.well-known/oauth-protected-resource/route.ts RFC 9728 metadata route handler returning exact JSON MIME type application/json; charset=utf-8

Next.js App Router Route Handler (app/.well-known/agent-card.json/route.ts)

route.ts
import { NextResponse } from "next/server";

export const dynamic = "force-static";

const DISCOVERY_HEADERS = {
  "Content-Type": "application/json; charset=utf-8",
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Methods": "GET, OPTIONS",
  "Cache-Control": "public, max-age=300"
};

export async function GET() {
  const card = {
    protocolVersion: "0.3.0",
    name: "Acme Next.js Agent",
    description: "Autonomous SaaS diagnostics and MCP workflows.",
    url: "https://agent.example.com/api/a2a",
    preferredTransport: "JSONRPC",
    version: "1.0.0",
    capabilities: { streaming: true, pushNotifications: false, stateTransitionHistory: true },
    defaultInputModes: ["text/plain", "application/json"],
    defaultOutputModes: ["application/json"],
    skills: [
      {
        id: "diagnose",
        name: "Diagnose Workspace",
        description: "Inspect deployment telemetry and billing quotas.",
        tags: ["diagnostics"]
      }
    ]
  };
  return NextResponse.json(card, { headers: DISCOVERY_HEADERS });
}

Critical middleware.ts Matcher Exclusion (Prevent /login Redirects)

middleware.ts
export const config = {
  // Exclude /.well-known/*, /llms.txt, /robots.txt, and static assets from auth redirects!
  matcher: [
    "/((?!_next/static|_next/image|favicon.ico|\\.well-known/|llms\\.txt|robots\\.txt).*)"
  ]
};

Key Empirical & Architectural Takeaways

  • App Router supports route handler directories with dots and extensions (e.g. app/.well-known/agent-card.json/route.ts), avoiding hidden-dotfile ambiguity.
  • The #1 Next.js production failure mode is Clerk/NextAuth/Supabase auth middleware intercepting /.well-known/* and returning HTTP 307 -> /login HTML.
  • Adding export const dynamic = "force-static" lets Next.js cache discovery manifests at the CDN edge with zero serverless invocations per probe.

Common Production Pitfalls to Avoid

  • Enabling trailingSlash: true in next.config.mjs redirects /.well-known/agent-card.json -> /.well-known/agent-card.json/, breaking strict A2A/RFC 9728 clients that forbid redirects on discovery endpoints.
  • Serving static files from public/.well-known/oauth-protected-resource without setting Content-Type: application/json in next.config.mjs headers() defaults to application/octet-stream on some hosts.
  • Using middleware.ts without excluding \.well-known/ in matcher.

More Agent Discovery Specifications & Framework Guides

← Back to AI Agent Discovery Generator & Validator Hub

Frequently Asked Questions

Why did my Next.js /.well-known/agent-card.json return HTML or a 307 redirect?

Almost always either middleware.ts auth logic intercepted the route and redirected to /login, or trailingSlash: true rewrote the URL. Exclude \.well-known/ in config.matcher and serve via an explicit route.ts handler.

Should I put agent-card.json in public/.well-known/ or app/.well-known/*/route.ts?

Use app/.well-known/agent-card.json/route.ts (and app/.well-known/oauth-protected-resource/route.ts) so you explicitly control Content-Type: application/json, CORS headers, and environment-specific URLs at build/runtime.

Does NextResponse.json() allow cross-origin agent inspectors?

Only if you explicitly attach Access-Control-Allow-Origin: * and an OPTIONS handler — Next.js does not emit CORS headers automatically.