Public: anyone can read this; the crew can change it.
The MCP initialize answer and /.well-known/mcp name the release number, never "dev" #15374
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15363-the-mcp-initialize-answer-and-well-known → main 0 commits fork 1 thread · 1 unresolved
12 files
changed +119
−23
src/contract/served.ts +2 −2
| @@ -8,7 +8,7 @@ import { encodeOnce, negotiate } from "../http/compress.ts"; | ||
| 8 | 8 | import { holds, weakEtag } from "../http/etag.ts"; |
| 9 | 9 | import type { Auth, Handler } from "../http/context.ts"; |
| 10 | 10 | import type { Router } from "../http/router.ts"; |
| 11 | import { running } from "../kernel/version.ts"; | |
| 11 | import { announcedOr } from "../kernel/version.ts"; | |
| 12 | 12 | import { openapi, ROUTES } from "./openapi.ts"; |
| 13 | 13 | import type { Role } from "./openapi/shared.ts"; |
| 14 | 14 | import { tagList, tagOf } from "./tags.ts"; |
| @@ -86,7 +86,7 @@ export const openapiHandler: Handler = (ctx) => { | ||
| 86 | 86 | ); |
| 87 | 87 | } |
| 88 | 88 | const role = roleOf(ctx.auth); |
| 89 | const hit = tag === null ? openapiText(ctx.url.origin, running().version, role) : openapiSliceText(ctx.url.origin, running().version, tag, role); | |
| 89 | const hit = tag === null ? openapiText(ctx.url.origin, announcedOr(), role) : openapiSliceText(ctx.url.origin, announcedOr(), tag, role); | |
| 90 | 90 | // The document depends on who asks: a shared cache keeps only the anonymous one. |
| 91 | 91 | const headers = { |
| 92 | 92 | "cache-control": role === "anonymous" ? "public, max-age=300" : "private, max-age=300", |
src/core/mcp_card.ts +11 −5
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | import type { Database } from "bun:sqlite"; |
| 10 | 10 | import { claudeInstall, opencodeInstall, pluginSource } from "./plugin_install.ts"; |
| 11 | import { running, type Running } from "../kernel/version.ts"; | |
| 11 | import { announcedVersion, running, type Running } from "../kernel/version.ts"; | |
| 12 | 12 | import { forgeSource } from "../settings/forge_source.ts"; |
| 13 | 13 | import { repoVisibility } from "./repos.ts"; |
| 14 | 14 | import { CARD_OF_TOOL, CARD_URI } from "../web/studio/cards.ts"; |
| @@ -203,6 +203,12 @@ export function setCardTools(tools: readonly { name: string; description: string | ||
| 203 | 203 | /** What the cards name as tools, as one string: a cached body is made again when it changes. */ |
| 204 | 204 | export const toolsKey = (ssh: boolean): string => shownTools(ssh).map((t) => t.name).join(","); |
| 205 | 205 | |
| 206 | /** `version` when the build names one; omitted otherwise, never "dev" (#15363). */ | |
| 207 | const versionField = (now: Running): { version?: string } => { | |
| 208 | const v = announcedVersion(now); | |
| 209 | return v === null ? {} : { version: v }; | |
| 210 | }; | |
| 211 | ||
| 206 | 212 | /** |
| 207 | 213 | * The agent card (A2A, protocol 0.3.0, the stable subset: name, description, |
| 208 | 214 | * url, version, protocolVersion, capabilities, default modes, skills; the |
| @@ -217,7 +223,7 @@ export function agentCard(origin: string, ssh: boolean, now: Running = running() | ||
| 217 | 223 | description: "A forge: repositories, issues, pulls and a crew of agents that build apps. Steer it through its MCP endpoint, signing in with OAuth.", |
| 218 | 224 | url, |
| 219 | 225 | preferredTransport: "JSONRPC", |
| 220 | version: now.version, | |
| 226 | ...versionField(now), | |
| 221 | 227 | documentationUrl: `${origin}${DISCOVERY.docs}`, |
| 222 | 228 | capabilities: { streaming: false, pushNotifications: false }, |
| 223 | 229 | securitySchemes: { oauth: { type: "openIdConnect", openIdConnectUrl: `${origin}/.well-known/openid-configuration` } }, |
| @@ -266,7 +272,7 @@ export function mcpCard(origin: string, repo: string | undefined, ssh: boolean, | ||
| 266 | 272 | const { url, oauth } = mcpPointers(origin); |
| 267 | 273 | return { |
| 268 | 274 | name: "forge", |
| 269 | version: now.version, | |
| 275 | ...versionField(now), | |
| 270 | 276 | commit: now.commit, |
| 271 | 277 | endpoint: url, |
| 272 | 278 | transport: "streamable-http", |
| @@ -293,7 +299,7 @@ export function mcpCard(origin: string, repo: string | undefined, ssh: boolean, | ||
| 293 | 299 | export function mcpDoor(origin: string, ssh: boolean, now: Running = running()) { |
| 294 | 300 | return { |
| 295 | 301 | name: "forge", |
| 296 | version: now.version, | |
| 302 | ...versionField(now), | |
| 297 | 303 | mcp: mcpPointers(origin).url, |
| 298 | 304 | oauth: { |
| 299 | 305 | authorization_endpoint: `${origin}${OAUTH_ENDPOINTS.authorize}`, |
| @@ -315,7 +321,7 @@ export function serverCard(origin: string, scopes: readonly string[], now: Runni | ||
| 315 | 321 | const pr = protectedResource(origin, scopes); |
| 316 | 322 | return { |
| 317 | 323 | name: "forge", |
| 318 | version: now.version, | |
| 324 | ...versionField(now), | |
| 319 | 325 | endpoint: pr.resource, |
| 320 | 326 | transport: "streamable-http", |
| 321 | 327 | authorization: { |
src/http/mcp.ts +5 −2
| @@ -23,7 +23,7 @@ import { getRepo } from "../core/repos.ts"; | ||
| 23 | 23 | import { scopeAllows } from "../core/tokens.ts"; |
| 24 | 24 | import type { User } from "../core/users.ts"; |
| 25 | 25 | import type { Ctx, Handler } from "./context.ts"; |
| 26 | import { running } from "../kernel/version.ts"; | |
| 26 | import { announcedVersion } from "../kernel/version.ts"; | |
| 27 | 27 | import { bearerChallenge } from "./oauth.ts"; |
| 28 | 28 | import { BodyTooLarge, json } from "./respond.ts"; |
| 29 | 29 | import { bodyCapFor, readBounded } from "./limits.ts"; |
| @@ -92,7 +92,10 @@ export const roleHere = (ctx: Ctx, user: User): Role => | ||
| 92 | 92 | user.kind === "user" ? roleOf(ctx.app.db, user) : "member"; |
| 93 | 93 | |
| 94 | 94 | /** Who answers: the forge, at the version its build stamped. */ |
| 95 | const server = () => ({ name: "forge", version: running().version }); | |
| 95 | const server = () => { | |
| 96 | const version = announcedVersion(); | |
| 97 | return version === null ? { name: "forge" } : { name: "forge", version }; | |
| 98 | }; | |
| 96 | 99 | |
| 97 | 100 | /** The role a token acts as, and for a guest what is listed and how to reach the rest. */ |
| 98 | 101 | const roleNote = (role: Role): string => |
src/kernel/version.ts +15
| @@ -66,3 +66,18 @@ const RELEASE = /^\d+\.\d+\.\d+$/; | ||
| 66 | 66 | export function releaseOf(r: Running = running()): string | null { |
| 67 | 67 | return RELEASE.test(r.version) ? r.version : null; |
| 68 | 68 | } |
| 69 | ||
| 70 | /** | |
| 71 | * The version a probe is told (#15363): the stamp when the build has one, else the short commit, | |
| 72 | * else null (the caller omits `version`). Never the word `dev`. | |
| 73 | */ | |
| 74 | export function announcedVersion(r: Running = running()): string | null { | |
| 75 | if (r.version !== UNSTAMPED) return r.version; | |
| 76 | return r.commit === null ? null : shortOf(r.commit); | |
| 77 | } | |
| 78 | ||
| 79 | /** | |
| 80 | * The same for a field that must be a string (openapi `info.version`): `unreleased` when nothing is | |
| 81 | * known. | |
| 82 | */ | |
| 83 | export const announcedOr = (r: Running = running()): string => announcedVersion(r) ?? "unreleased"; |
src/tui/screens/api.ts +2 −2
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | import { narrow, reference, runnable, type Area, type Operation } from "../../contract/reference.ts"; |
| 10 | 10 | import { openapiText, roleOf } from "../../contract/served.ts"; |
| 11 | 11 | import { createSession, destroySession } from "../../core/sessions.ts"; |
| 12 | import { running } from "../../kernel/version.ts"; | |
| 12 | import { announcedOr } from "../../kernel/version.ts"; | |
| 13 | 13 | import type { Ctx, Screen } from "../app/app.ts"; |
| 14 | 14 | import { binding } from "../app/keys.ts"; |
| 15 | 15 | import type { Session } from "../session.ts"; |
| @@ -95,7 +95,7 @@ export function apiScreen(f: Session, ctx: Ctx): Screen { | ||
| 95 | 95 | const state = new ListState(); |
| 96 | 96 | const filter = new Filter(); |
| 97 | 97 | // The rows this person may call, as the page cuts them: a guest reads the anonymous ones (#11286). |
| 98 | const doc = openapiText(f.app.config.url ?? "http://localhost", running().version, roleOf(f)); | |
| 98 | const doc = openapiText(f.app.config.url ?? "http://localhost", announcedOr(), roleOf(f)); | |
| 99 | 99 | const all = apiOperations(reference(JSON.parse(doc.text))); |
| 100 | 100 | const shown = (): Operation[] => apiOperations(narrow([{ name: "", operations: all }], filter.query)).slice(0, MAX_SHOWN); |
| 101 | 101 | let note = ""; |
src/web/pages/api.ts +2 −2
| @@ -16,7 +16,7 @@ import { cappedQuery } from "../../core/query_limit.ts"; | ||
| 16 | 16 | import { canEnterApp } from "../../core/repos.ts"; |
| 17 | 17 | import type { Ctx } from "../../http/context.ts"; |
| 18 | 18 | import { noRoute, prefersJson } from "../../http/respond.ts"; |
| 19 | import { running } from "../../kernel/version.ts"; | |
| 19 | import { announcedOr } from "../../kernel/version.ts"; | |
| 20 | 20 | import { API_TRY_JS, referenceBody } from "../api_reference.ts"; |
| 21 | 21 | import { loadRepo, notFoundPage, repoBase, repoHeader } from "../chrome.ts"; |
| 22 | 22 | import { html } from "../html.ts"; |
| @@ -32,7 +32,7 @@ const count = (areas: readonly Area[]): number => areas.reduce((n, a) => n + a.o | ||
| 32 | 32 | const forgeAreas = (() => { |
| 33 | 33 | const read = new Map<Role, { tag: string; areas: Area[] }>(); |
| 34 | 34 | return (origin: string, role: Role): Area[] => { |
| 35 | const hit = openapiText(origin, running().version, role); | |
| 35 | const hit = openapiText(origin, announcedOr(), role); | |
| 36 | 36 | let held = read.get(role); |
| 37 | 37 | if (held?.tag !== hit.etag) { |
| 38 | 38 | held = { tag: hit.etag, areas: reference(JSON.parse(hit.text)) }; |
src/web/standards.ts +3 −3
| @@ -197,14 +197,14 @@ export async function mcpDoorJson(ctx: Ctx): Promise<Response> { | ||
| 197 | 197 | await loadMcp(); |
| 198 | 198 | const now = running(); |
| 199 | 199 | const ssh = sshOn(ctx.app.db); |
| 200 | return discovery(ctx, "mcp-door", JSON.stringify([now.version, toolsKey(ssh)]), () => mcpDoor(ctx.url.origin, ssh, now), "application/json", "public, max-age=300"); | |
| 200 | return discovery(ctx, "mcp-door", JSON.stringify([now.version, now.commit, toolsKey(ssh)]), () => mcpDoor(ctx.url.origin, ssh, now), "application/json", "public, max-age=300"); | |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | /** GET /.well-known/mcp/server-card.json — the small server card (a draft; see `serverCard`). |
| 204 | 204 | * Public, the same for every reader. */ |
| 205 | 205 | export function serverCardJson(ctx: Ctx): Response { |
| 206 | 206 | const now = running(); |
| 207 | return discovery(ctx, "server-card", JSON.stringify([now.version]), () => serverCard(ctx.url.origin, OAUTH_SCOPES, now), "application/json", "public, max-age=3600", true); | |
| 207 | return discovery(ctx, "server-card", JSON.stringify([now.version, now.commit]), () => serverCard(ctx.url.origin, OAUTH_SCOPES, now), "application/json", "public, max-age=3600", true); | |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | /** |
| @@ -217,7 +217,7 @@ export async function agentCardJson(ctx: Ctx): Promise<Response> { | ||
| 217 | 217 | // The same reader test /llms.txt uses, so the two agree on who sees the full list. |
| 218 | 218 | const signedIn = ctx.auth.user !== undefined; |
| 219 | 219 | const ssh = sshOn(ctx.app.db); |
| 220 | return discovery(ctx, `agent-card ${signedIn}`, JSON.stringify([now.version, toolsKey(ssh)]), () => agentCard(ctx.url.origin, ssh, now, signedIn), "application/json", signedIn ? "private, max-age=3600" : "public, max-age=3600", true, "Cookie, Authorization"); | |
| 220 | return discovery(ctx, `agent-card ${signedIn}`, JSON.stringify([now.version, now.commit, toolsKey(ssh)]), () => agentCard(ctx.url.origin, ssh, now, signedIn), "application/json", signedIn ? "private, max-age=3600" : "public, max-age=3600", true, "Cookie, Authorization"); | |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | /** |
test/mcp-card.test.ts +2 −2
| @@ -6,7 +6,7 @@ import { join } from "node:path"; | ||
| 6 | 6 | import { createApp, type App } from "../src/app.ts"; |
| 7 | 7 | import { serve, type Servers } from "../src/http/server.ts"; |
| 8 | 8 | import { mcpCard, TOKEN_FREE_TOOLS } from "../src/core/mcp_card.ts"; |
| 9 | import { running } from "../src/kernel/version.ts"; | |
| 9 | import { announcedVersion, running } from "../src/kernel/version.ts"; | |
| 10 | 10 | |
| 11 | 11 | describe("the MCP server card", () => { |
| 12 | 12 | let dataDir: string; |
| @@ -39,7 +39,7 @@ describe("the MCP server card", () => { | ||
| 39 | 39 | expect(res.headers.get("access-control-allow-origin")).toBe("*"); |
| 40 | 40 | const card = (await res.json()) as any; |
| 41 | 41 | expect(card.name).toBe("forge"); |
| 42 | expect(card.version).toBe(running().version); | |
| 42 | expect(card.version).toBe(announcedVersion() ?? undefined); | |
| 43 | 43 | expect(card.transport).toBe("streamable-http"); |
| 44 | 44 | expect(card.endpoint).toBe(`${base}/mcp`); |
| 45 | 45 | expect(card.tokenFreeTools).toEqual([...TOKEN_FREE_TOOLS]); |
test/mcp-door.test.ts +2 −2
| @@ -8,7 +8,7 @@ import { serve, type Servers } from "../src/http/server.ts"; | ||
| 8 | 8 | import { TOOLS } from "../src/http/mcp/tools.ts"; |
| 9 | 9 | import { OFFER_TOOL, REPORT_TOOL, START_TOOL } from "../src/http/mcp.ts"; |
| 10 | 10 | import { sshOn } from "../src/settings/ssh.ts"; |
| 11 | import { running } from "../src/kernel/version.ts"; | |
| 11 | import { announcedVersion } from "../src/kernel/version.ts"; | |
| 12 | 12 | |
| 13 | 13 | let dataDir: string; |
| 14 | 14 | let servers: Servers; |
| @@ -35,7 +35,7 @@ test("answers the door, cached five minutes, with the security floor", async () | ||
| 35 | 35 | expect(res.headers.get("content-security-policy")).toBeTruthy(); |
| 36 | 36 | const door = (await res.json()) as any; |
| 37 | 37 | expect(door.name).toBe("forge"); |
| 38 | expect(door.version).toBe(running().version); | |
| 38 | expect(door.version).toBe(announcedVersion() ?? undefined); | |
| 39 | 39 | expect(door.mcp).toBe(`${base}/mcp`); |
| 40 | 40 | }); |
| 41 | 41 |
test/mcp-server-card.test.ts +2 −1
| @@ -7,6 +7,7 @@ import { join } from "node:path"; | ||
| 7 | 7 | import { createApp } from "../src/app.ts"; |
| 8 | 8 | import { serve, type Servers } from "../src/http/server.ts"; |
| 9 | 9 | import { TOKEN_FREE_TOOLS } from "../src/core/mcp_card.ts"; |
| 10 | import { announcedVersion } from "../src/kernel/version.ts"; | |
| 10 | 11 | |
| 11 | 12 | let dataDir: string; |
| 12 | 13 | let servers: Servers; |
| @@ -30,7 +31,7 @@ test("answers 200 JSON, public and cached, with name, version, endpoint and the | ||
| 30 | 31 | expect(res.headers.get("cache-control")).toBe("public, max-age=3600"); |
| 31 | 32 | const card = (await res.json()) as any; |
| 32 | 33 | expect(card.name).toBe("forge"); |
| 33 | expect(typeof card.version).toBe("string"); | |
| 34 | expect(card.version).toBe(announcedVersion() ?? undefined); | |
| 34 | 35 | expect(card.endpoint).toBe(`${base}/mcp`); |
| 35 | 36 | const pr = (await (await fetch(card.authorization.protectedResourceMetadata)).json()) as any; |
| 36 | 37 | expect(card.authorization.authorization_servers).toEqual(pr.authorization_servers); |
test/mcp-version.test.ts added +71
| @@ -0,0 +1,71 @@ | ||
| 1 | /** | |
| 2 | * The MCP initialize answer, /.well-known/mcp and the openapi info.version name the release, never | |
| 3 | * "dev" (#15363). | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp } from "../src/app.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | ||
| 12 | let dataDir: string; | |
| 13 | let servers: Servers; | |
| 14 | let base: string; | |
| 15 | const saved = { v: process.env["FORGE_VERSION"], c: process.env["FORGE_COMMIT"] }; | |
| 16 | ||
| 17 | beforeAll(async () => { | |
| 18 | dataDir = await mkdtemp(join(tmpdir(), "forge-mcp-version-")); | |
| 19 | servers = await serve(createApp({ dataDir, port: 0 })); | |
| 20 | base = servers.url; | |
| 21 | }); | |
| 22 | afterAll(async () => { | |
| 23 | for (const [k, v] of [["FORGE_VERSION", saved.v], ["FORGE_COMMIT", saved.c]] as const) { | |
| 24 | if (v === undefined) delete process.env[k]; | |
| 25 | else process.env[k] = v; | |
| 26 | } | |
| 27 | await servers.stop?.(); | |
| 28 | await rm(dataDir, { recursive: true, force: true }); | |
| 29 | }); | |
| 30 | ||
| 31 | async function answers() { | |
| 32 | const init = await fetch(base + "/mcp", { | |
| 33 | method: "POST", | |
| 34 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream" }, | |
| 35 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "initialize", params: { protocolVersion: "2025-03-26", capabilities: {}, clientInfo: { name: "t", version: "1" } } }), | |
| 36 | }); | |
| 37 | const text = await init.text(); | |
| 38 | const data = text.includes("data:") ? text.split("\n").find((l) => l.startsWith("data:"))!.slice(5) : text; | |
| 39 | const serverInfo = JSON.parse(data).result.serverInfo; | |
| 40 | const door = (await (await fetch(base + "/.well-known/mcp")).json()) as any; | |
| 41 | const spec = (await (await fetch(base + "/openapi.json")).json()) as any; | |
| 42 | return { serverInfo, door, spec, raw: JSON.stringify([serverInfo, door, spec.info]) }; | |
| 43 | } | |
| 44 | ||
| 45 | test("a stamped build names its release in all three", async () => { | |
| 46 | process.env["FORGE_VERSION"] = "v1.2.3"; | |
| 47 | process.env["FORGE_COMMIT"] = "abcdef0123456789"; | |
| 48 | const a = await answers(); | |
| 49 | expect(a.serverInfo.version).toBe("1.2.3"); | |
| 50 | expect(a.door.version).toBe("1.2.3"); | |
| 51 | expect(a.spec.info.version).toBe("1.2.3"); | |
| 52 | expect(a.raw).not.toContain('"dev"'); | |
| 53 | }); | |
| 54 | ||
| 55 | test("an unstamped build answers the short commit, and omits version when there is none", async () => { | |
| 56 | delete process.env["FORGE_VERSION"]; | |
| 57 | process.env["FORGE_COMMIT"] = "abcdef0123456789"; | |
| 58 | const a = await answers(); | |
| 59 | expect(a.serverInfo.version).toBe("abcdef0"); | |
| 60 | expect(a.door.version).toBe("abcdef0"); | |
| 61 | expect(a.spec.info.version).toBe("abcdef0"); | |
| 62 | expect(a.raw).not.toContain('"dev"'); | |
| 63 | ||
| 64 | delete process.env["FORGE_COMMIT"]; | |
| 65 | delete process.env["FORGE_APP_COMMIT"]; | |
| 66 | const b = await answers(); | |
| 67 | expect(b.serverInfo.version).toBeUndefined(); | |
| 68 | expect(b.door.version).toBeUndefined(); | |
| 69 | expect(b.spec.info.version).toBe("unreleased"); | |
| 70 | expect(b.raw).not.toContain('"dev"'); | |
| 71 | }); |
test/version.test.ts +2 −2
| @@ -326,7 +326,7 @@ describe("where the stamp is read", () => { | ||
| 326 | 326 | expect(page).not.toContain("reduced-protection"); |
| 327 | 327 | }); |
| 328 | 328 | |
| 329 | test("with no stamp every one of them says dev", async () => { | |
| 329 | test("with no stamp every one of them says dev, but the MCP door omits it", async () => { | |
| 330 | 330 | stamp(); |
| 331 | 331 | try { |
| 332 | 332 | const health = (await ( |
| @@ -338,7 +338,7 @@ describe("where the stamp is read", () => { | ||
| 338 | 338 | capabilities: {}, |
| 339 | 339 | clientInfo: { name: "test", version: "0" }, |
| 340 | 340 | }); |
| 341 | expect(init.serverInfo.version).toBe("dev"); | |
| 341 | expect(init.serverInfo.version).toBeUndefined(); | |
| 342 | 342 | } finally { |
| 343 | 343 | stamp("9.8.7", COMMIT); |
| 344 | 344 | } |
new above
offline
This helper sits between agentCard's docblock (lines above) and agentCard, so the agentCard description now attaches to versionField. Move versionField above that docblock so each comment documents its own function.