| 1 | import { Hono } from "hono"; |
| 2 | import type { AppVars } from "../auth/principal.js"; |
| 3 | import type { Env } from "../env.js"; |
| 4 | |
| 5 | /** |
| 6 | * /.well-known/mcp-registry-auth: the proof record that lets an instance's owner publish its MCP |
| 7 | * server to the official MCP registry (registry.modelcontextprotocol.io) under the instance's own |
| 8 | * domain, such as com.diffinery/diffinery. The registry fetches this file and checks that the key |
| 9 | * `mcp-publisher login http` signs with is the one named here. |
| 10 | * |
| 11 | * The record comes from MCP_REGISTRY_AUTH, or for diffinery.com from PROOFS below, so the project's |
| 12 | * own key is never served by another instance (it would let that key publish under that instance's |
| 13 | * domain). Anything that isn't one well-formed record is a 404, like an unset one. |
| 14 | */ |
| 15 | |
| 16 | /** Built-in records by host. Rotating a key means changing it here; the private key stays with whoever publishes. */ |
| 17 | const PROOFS: Record<string, string> = { |
| 18 | "diffinery.com": "v=MCPv1; k=ed25519; p=HbHhQYSP5tE1guSK2Df9MqxTJ8F+ojzjAesiFpgABTU=", |
| 19 | }; |
| 20 | |
| 21 | const RECORD = /^v=MCPv1; k=(ed25519|ecdsap384); p=[A-Za-z0-9+/]+={0,2}$/; |
| 22 | |
| 23 | /** This instance's proof record, or null when it has none. */ |
| 24 | export function mcpRegistryAuth(env: Pick<Env, "PUBLIC_URL" | "MCP_REGISTRY_AUTH">): string | null { |
| 25 | const own = env.MCP_REGISTRY_AUTH?.trim(); |
| 26 | if (own) return RECORD.test(own) ? own : null; |
| 27 | return PROOFS[new URL(env.PUBLIC_URL).hostname] ?? null; |
| 28 | } |
| 29 | |
| 30 | export const mcpRegistryAuthRoutes = new Hono<{ Bindings: Env; Variables: AppVars }>(); |
| 31 | |
| 32 | mcpRegistryAuthRoutes.get("/.well-known/mcp-registry-auth", (c) => { |
| 33 | const record = mcpRegistryAuth(c.env); |
| 34 | if (!record) return c.text("Not found", 404); |
| 35 | return c.body(`${record}\n`, 200, { "content-type": "text/plain; charset=utf-8", "cache-control": "public, max-age=300" }); |
| 36 | }); |