Diffinery

homeflare/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

diffinery/apps/diffinery/src/http/mcp-registry-auth.ts
36 lines · 1.8 KBRaw History
1import { Hono } from "hono";
2import type { AppVars } from "../auth/principal.js";
3import 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. */
17const PROOFS: Record<string, string> = {
18 "diffinery.com": "v=MCPv1; k=ed25519; p=HbHhQYSP5tE1guSK2Df9MqxTJ8F+ojzjAesiFpgABTU=",
19};
20
21const 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. */
24export 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
30export const mcpRegistryAuthRoutes = new Hono<{ Bindings: Env; Variables: AppVars }>();
31
32mcpRegistryAuthRoutes.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});