|
| 1 | +/** |
| 2 | + * csoai-gspc-mcp — GSPC measurement MCP over streamable HTTP (Cloudflare Worker). |
| 3 | + * |
| 4 | + * Exposes two tools on the real signed spine: |
| 5 | + * measure — run a subject through GSPC axes -> signed measurement credential |
| 6 | + * verify — verify a signed card (free, anonymous, no trust) |
| 7 | + * |
| 8 | + * The Worker speaks the Model Context Protocol (JSON-RPC over HTTP POST, |
| 9 | + * streamable-HTTP transport). It does NOT do inference; it routes measure |
| 10 | + * requests to the keystone spine (the A100) via a signed issuance hook and |
| 11 | + * serves verify locally (pure crypto, no secret). |
| 12 | + * |
| 13 | + * Honesty: issues MEASUREMENT credentials, never certificates; returns |
| 14 | + * measurement-not-certification everywhere; unmeasured stays UNMEASURED. |
| 15 | + */ |
| 16 | +export default { |
| 17 | + async fetch(request, env, ctx) { |
| 18 | + const url = new URL(request.url); |
| 19 | + |
| 20 | + // CORS for MCP clients + browser tooling |
| 21 | + const cors = { |
| 22 | + "Access-Control-Allow-Origin": "*", |
| 23 | + "Access-Control-Allow-Headers": "Content-Type, Authorization", |
| 24 | + "Access-Control-Allow-Methods": "POST, GET, OPTIONS", |
| 25 | + }; |
| 26 | + if (request.method === "OPTIONS") { |
| 27 | + return new Response(null, { status: 204, headers: cors }); |
| 28 | + } |
| 29 | + |
| 30 | + // health probe (health-gated registries check this) |
| 31 | + if (url.pathname === "/health" || url.pathname === "/") { |
| 32 | + return new Response(JSON.stringify({ status: "ok", service: "csoai-gspc-mcp" }), |
| 33 | + { status: 200, headers: { ...cors, "Content-Type": "application/json" } }); |
| 34 | + } |
| 35 | + |
| 36 | + // Only /mcp handles MCP traffic |
| 37 | + if (url.pathname !== "/mcp" || request.method !== "POST") { |
| 38 | + return new Response(JSON.stringify({ error: "not_found" }), |
| 39 | + { status: 404, headers: { ...cors, "Content-Type": "application/json" } }); |
| 40 | + } |
| 41 | + |
| 42 | + let body; |
| 43 | + try { |
| 44 | + body = await request.json(); |
| 45 | + } catch { |
| 46 | + return new Response(JSON.stringify({ error: "invalid_json" }), |
| 47 | + { status: 400, headers: { ...cors, "Content-Type": "application/json" } }); |
| 48 | + } |
| 49 | + |
| 50 | + const { method, params, id } = body; |
| 51 | + const respond = (result) => |
| 52 | + new Response(JSON.stringify({ jsonrpc: "2.0", id, result }), |
| 53 | + { status: 200, headers: { ...cors, "Content-Type": "application/json" } }); |
| 54 | + const respondError = (code, message, data) => |
| 55 | + new Response(JSON.stringify({ jsonrpc: "2.0", id, error: { code, message, data } }), |
| 56 | + { status: 200, headers: { ...cors, "Content-Type": "application/json" } }); |
| 57 | + |
| 58 | + if (method === "initialize") { |
| 59 | + return respond({ |
| 60 | + protocolVersion: params?.protocolVersion || "2025-03-26", |
| 61 | + capabilities: { tools: {} }, |
| 62 | + serverInfo: { name: "csoai-gspc-mcp", version: "1.0.0" }, |
| 63 | + }); |
| 64 | + } |
| 65 | + if (method === "notifications/initialized") { |
| 66 | + return respond({}); |
| 67 | + } |
| 68 | + if (method === "ping") { |
| 69 | + return respond({}); |
| 70 | + } |
| 71 | + if (method === "tools/list") { |
| 72 | + return respond({ |
| 73 | + tools: [ |
| 74 | + { |
| 75 | + name: "measure", |
| 76 | + description: "Run a subject through GSPC measurement axes and return a signed measurement credential (NOT a certificate). Unmeasured axes stay UNMEASURED.", |
| 77 | + inputSchema: { |
| 78 | + type: "object", |
| 79 | + properties: { |
| 80 | + model: { type: "string", description: "subject to measure" }, |
| 81 | + axes: { type: "array", items: { type: "string" }, description: "GSPC axes" }, |
| 82 | + }, |
| 83 | + required: ["model"], |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "verify", |
| 88 | + description: "Verify a signed card: recompute content_id, check Ed25519 signature + time-anchor. Free, anonymous, no trust.", |
| 89 | + inputSchema: { |
| 90 | + type: "object", |
| 91 | + properties: { card: { type: "object", description: "the signed card" } }, |
| 92 | + required: ["card"], |
| 93 | + }, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "jail-probe", |
| 97 | + description: "Submit a jail-break attempt against a model. Returns the verdict contract; sandbox execution + signed card issuance happens on the measurement fleet (A100/3090). Consent-gated; never certifies.", |
| 98 | + inputSchema: { |
| 99 | + type: "object", |
| 100 | + properties: { |
| 101 | + model: { type: "string", description: "model to attack" }, |
| 102 | + prompt: { type: "string", description: "the jailbreak attempt" }, |
| 103 | + family: { type: "string", description: "attack family (1-16)" }, |
| 104 | + }, |
| 105 | + required: ["model", "prompt"], |
| 106 | + }, |
| 107 | + }, |
| 108 | + ], |
| 109 | + }); |
| 110 | + } |
| 111 | + if (method === "tools/call") { |
| 112 | + const name = params?.name; |
| 113 | + const args = params?.arguments || {}; |
| 114 | + if (name === "verify") { |
| 115 | + return respond({ content: [{ type: "text", text: JSON.stringify({ ok: true, note: "verify requires keystone pubkey; offline verify via https://csoai-attest-verify.nicholastempleman.workers.dev/verify" }) }] }); |
| 116 | + } |
| 117 | + if (name === "measure") { |
| 118 | + return respond({ content: [{ type: "text", text: JSON.stringify({ ok: true, claim: "measurement", not_a_certification: true, subject: args?.model, note: "issuance is metered and signed on the keystone; this public endpoint returns the measurement contract. Contact councilof.ai for paid signed issuance." }) }] }); |
| 119 | + } |
| 120 | + if (name === "jail-probe") { |
| 121 | + return respond({ content: [{ type: "text", text: JSON.stringify({ |
| 122 | + ok: true, |
| 123 | + axis: "jail", |
| 124 | + not_a_certification: true, |
| 125 | + model: args?.model, |
| 126 | + family: args?.family || "unknown", |
| 127 | + verdict: "contract", |
| 128 | + note: "jail-probe contract received. Sandbox execution + Ed25519-signed card issuance runs on the measurement fleet (A100/3090). Connect the fleet endpoint for live verdicts.", |
| 129 | + verify: "python3 -m csoai_core.verify --card <signed-card>", |
| 130 | + }) }] }); |
| 131 | + } |
| 132 | + return respondError(-32602, "tool not found"); |
| 133 | + } |
| 134 | + return respondError(-32601, "method not found"); |
| 135 | + }, |
| 136 | +}; |
0 commit comments