|
| 1 | +import { setHeader } from "@lazarv/react-server"; |
| 2 | +import { useUrl } from "@lazarv/react-server"; |
| 3 | + |
| 4 | +import skillContent from "../../../skills/react-server/SKILL.md?raw"; |
| 5 | +import { version } from "../version.mjs"; |
| 6 | + |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | +// Agent-readiness payloads |
| 9 | +// |
| 10 | +// Implements the contracts checked by https://isitagentready.com: |
| 11 | +// - RFC 9727 API Catalog → /.well-known/api-catalog |
| 12 | +// - Agent Skills v0.2 index → /.well-known/agent-skills/index.json |
| 13 | +// - Agent Skill body → /.well-known/agent-skills/react-server/SKILL.md |
| 14 | +// - MCP Server Card → /.well-known/mcp/server-card.json |
| 15 | +// - RFC 8288 Link headers → on every documentation page |
| 16 | +// --------------------------------------------------------------------------- |
| 17 | + |
| 18 | +const SITE = "https://react-server.dev"; |
| 19 | + |
| 20 | +const apiCatalog = { |
| 21 | + linkset: [ |
| 22 | + { |
| 23 | + anchor: `${SITE}/.well-known/api-catalog`, |
| 24 | + item: [ |
| 25 | + { href: `${SITE}/mcp` }, |
| 26 | + { href: `${SITE}/llms.txt` }, |
| 27 | + { href: `${SITE}/sitemap.xml` }, |
| 28 | + { href: `${SITE}/schema.json` }, |
| 29 | + ], |
| 30 | + }, |
| 31 | + { |
| 32 | + anchor: `${SITE}/mcp`, |
| 33 | + "service-doc": [ |
| 34 | + { href: `${SITE}/features/mcp`, type: "text/html" }, |
| 35 | + { href: `${SITE}/features/mcp.md`, type: "text/markdown" }, |
| 36 | + ], |
| 37 | + describedby: [ |
| 38 | + { |
| 39 | + href: `${SITE}/.well-known/mcp/server-card.json`, |
| 40 | + type: "application/json", |
| 41 | + }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + { |
| 45 | + anchor: `${SITE}/llms.txt`, |
| 46 | + describedby: [{ href: `${SITE}/llms.txt`, type: "text/plain" }], |
| 47 | + }, |
| 48 | + { |
| 49 | + anchor: `${SITE}/schema.json`, |
| 50 | + describedby: [ |
| 51 | + { href: `${SITE}/schema.json`, type: "application/schema+json" }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + ], |
| 55 | +}; |
| 56 | + |
| 57 | +const agentSkillsIndex = { |
| 58 | + $schema: "https://agent-skills.dev/schema/v0.2.0.json", |
| 59 | + skills: [ |
| 60 | + { |
| 61 | + name: "react-server", |
| 62 | + description: |
| 63 | + "Build applications with @lazarv/react-server — a React Server Components runtime built on Vite. Covers use directives, file-system router, HTTP hooks, caching, live components, workers, MCP, deployment, and all core APIs.", |
| 64 | + version, |
| 65 | + skill_url: `${SITE}/.well-known/agent-skills/react-server/SKILL.md`, |
| 66 | + homepage: SITE, |
| 67 | + license: "MIT", |
| 68 | + }, |
| 69 | + ], |
| 70 | +}; |
| 71 | + |
| 72 | +const mcpServerCard = { |
| 73 | + $schema: |
| 74 | + "https://modelcontextprotocol.io/schemas/draft/2025-09-29/server-card.json", |
| 75 | + name: "react-server-docs", |
| 76 | + title: "@lazarv/react-server Documentation", |
| 77 | + description: |
| 78 | + "Search and read the @lazarv/react-server documentation as Model Context Protocol resources and tools. Provides a search_docs tool and exposes every documentation page as a markdown resource.", |
| 79 | + version, |
| 80 | + homepage: SITE, |
| 81 | + documentation: `${SITE}/features/mcp`, |
| 82 | + endpoints: { |
| 83 | + streamable_http: `${SITE}/mcp`, |
| 84 | + }, |
| 85 | + capabilities: { |
| 86 | + tools: { listChanged: false }, |
| 87 | + resources: { listChanged: false, subscribe: false }, |
| 88 | + prompts: { listChanged: false }, |
| 89 | + }, |
| 90 | + contact: { |
| 91 | + repository: "https://github.com/lazarv/react-server", |
| 92 | + }, |
| 93 | +}; |
| 94 | + |
| 95 | +const wellKnown = { |
| 96 | + "/.well-known/api-catalog": () => |
| 97 | + json( |
| 98 | + apiCatalog, |
| 99 | + 'application/linkset+json; profile="https://www.rfc-editor.org/info/rfc9727"' |
| 100 | + ), |
| 101 | + "/.well-known/agent-skills/index.json": () => json(agentSkillsIndex), |
| 102 | + "/.well-known/agent-skills/react-server/SKILL.md": () => |
| 103 | + new Response(skillContent, { |
| 104 | + headers: { |
| 105 | + "Content-Type": "text/markdown; charset=utf-8", |
| 106 | + "Cache-Control": "public, max-age=3600", |
| 107 | + }, |
| 108 | + }), |
| 109 | + "/.well-known/mcp/server-card.json": () => json(mcpServerCard), |
| 110 | +}; |
| 111 | + |
| 112 | +function json(body, contentType = "application/json; charset=utf-8") { |
| 113 | + return new Response(JSON.stringify(body, null, 2), { |
| 114 | + headers: { |
| 115 | + "Content-Type": contentType, |
| 116 | + "Cache-Control": "public, max-age=3600", |
| 117 | + }, |
| 118 | + }); |
| 119 | +} |
| 120 | + |
| 121 | +// --------------------------------------------------------------------------- |
| 122 | +// Discovery Link headers (RFC 8288 / RFC 9727) |
| 123 | +// |
| 124 | +// Advertised on every documentation page so any HTTP client (including agents |
| 125 | +// that only do HEAD or GET on `/`) can discover the API catalog, MCP entry, |
| 126 | +// and human-/machine-readable documentation without crawling the whole site. |
| 127 | +// --------------------------------------------------------------------------- |
| 128 | + |
| 129 | +const linkHeader = [ |
| 130 | + '</.well-known/api-catalog>; rel="api-catalog"; type="application/linkset+json"', |
| 131 | + '</mcp>; rel="service-meta"; type="application/json"', |
| 132 | + '</llms.txt>; rel="describedby"; type="text/plain"', |
| 133 | + '</sitemap.xml>; rel="sitemap"; type="application/xml"', |
| 134 | + '</.well-known/agent-skills/index.json>; rel="https://agent-skills.dev/rel/index"; type="application/json"', |
| 135 | +].join(", "); |
| 136 | + |
| 137 | +// Pathnames that should never receive the discovery Link header — they're |
| 138 | +// machine-only endpoints with their own headers/cache semantics. |
| 139 | +const SKIP_LINK_HEADER = (pathname) => |
| 140 | + pathname === "/sitemap.xml" || |
| 141 | + pathname === "/schema.json" || |
| 142 | + pathname === "/mcp" || |
| 143 | + pathname.startsWith("/mcp/") || |
| 144 | + pathname.startsWith("/.well-known/") || |
| 145 | + pathname.startsWith("/md/") || |
| 146 | + pathname.endsWith(".md"); |
| 147 | + |
| 148 | +export default function AgentDiscovery() { |
| 149 | + const { pathname } = useUrl(); |
| 150 | + |
| 151 | + // 1. Static well-known endpoints — short-circuit with the right content type. |
| 152 | + const wellKnownHandler = wellKnown[pathname]; |
| 153 | + if (wellKnownHandler) { |
| 154 | + return wellKnownHandler(); |
| 155 | + } |
| 156 | + |
| 157 | + // 2. Set discovery Link header on documentation pages. |
| 158 | + if (!SKIP_LINK_HEADER(pathname)) { |
| 159 | + setHeader("Link", linkHeader); |
| 160 | + } |
| 161 | +} |
0 commit comments