Public: anyone can read this; the crew can change it.
llms.txt, the MCP server card and tools/list name the same number of tools a stranger can sign in for #15874
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/15869-llms-txt-the-mcp-server-card-and-tools-l → main 0 commits fork 1 thread · 1 unresolved
2 files
changed +22
−5
src/web/llms_tools.ts +3 −5
| @@ -7,7 +7,7 @@ | ||
| 7 | 7 | * token-free tools and one line for the rest, so /llms.txt varies by reader. |
| 8 | 8 | */ |
| 9 | 9 | import type { Tool } from "../http/mcp/shared.ts"; |
| 10 | import { TOKEN_FREE_TOOLS } from "../core/mcp_card.ts"; | |
| 10 | import { moreAfterSignIn, TOKEN_FREE_TOOLS } from "../core/mcp_card.ts"; | |
| 11 | 11 | import { scopeAllows, type TokenScope } from "../core/tokens.ts"; |
| 12 | 12 | |
| 13 | 13 | /** What a line reads: `report` is not a `Tool`, having no `run`, but is listed. */ |
| @@ -61,7 +61,6 @@ export function callableBy(t: Listed, r: Reader): boolean { | ||
| 61 | 61 | |
| 62 | 62 | // The index is handed in at boot by the server rather than imported here: |
| 63 | 63 | // importing it from a page module closes an import cycle through the actions. |
| 64 | let toolCount = 0; | |
| 65 | 64 | let open: readonly Listed[] = []; |
| 66 | 65 | let every: readonly Listed[] = []; |
| 67 | 66 | let sections = new Map<string, readonly string[]>(); |
| @@ -73,7 +72,6 @@ export const setMcpTools = (all: readonly Listed[], tokenFree: readonly Listed[] | ||
| 73 | 72 | // The open tools in TOOLS (explore, search) are listed with `report` and `start`, |
| 74 | 73 | // not counted among the rest. |
| 75 | 74 | const free = all.filter((t) => TOKEN_FREE_TOOLS.includes(t.name)); |
| 76 | toolCount = all.length - free.length; | |
| 77 | 75 | open = [...tokenFree, ...free]; |
| 78 | 76 | every = [...all, ...tokenFree]; |
| 79 | 77 | sections = new Map(); // every kept per-reader list goes with the old index |
| @@ -81,11 +79,11 @@ export const setMcpTools = (all: readonly Listed[], tokenFree: readonly Listed[] | ||
| 81 | 79 | |
| 82 | 80 | /** The kept-body key for a reader: tools differ by scope and by admin, nothing else. */ |
| 83 | 81 | export const readerKey = (r: Reader | undefined, ssh: boolean): string => |
| 84 | r === undefined ? "anon" : `${r.scope}${r.isAdmin ? "+admin" : ""}${ssh ? "+ssh" : ""}`; | |
| 82 | r === undefined ? `anon${ssh ? "+ssh" : ""}` : `${r.scope}${r.isAdmin ? "+admin" : ""}${ssh ? "+ssh" : ""}`; | |
| 85 | 83 | |
| 86 | 84 | /** A signed-in reader's section is the tools they may call; a stranger's is folded. */ |
| 87 | 85 | export function mcpToolsSection(origin: string, reader: Reader | undefined, ssh: boolean): readonly string[] { |
| 88 | if (reader === undefined) return anonymousToolsSection(open, toolCount, origin); | |
| 86 | if (reader === undefined) return anonymousToolsSection(open, moreAfterSignIn(ssh), origin); | |
| 89 | 87 | const k = readerKey(reader, ssh); |
| 90 | 88 | let s = sections.get(k); |
| 91 | 89 | if (s === undefined) sections.set(k, (s = toolsSection(every.filter((t) => (t.needsSwitch === undefined || ssh) && callableBy(t, reader))))); |
test/tool-count-agree.test.ts added +19
| @@ -0,0 +1,19 @@ | ||
| 1 | /** | |
| 2 | * llms.txt, the server card and tools/list give a stranger the same count of tools an account adds. | |
| 3 | */ | |
| 4 | import { expect, test } from "bun:test"; | |
| 5 | import { loadMcp } from "../src/http/mcp/load.ts"; | |
| 6 | import { mcpCard, TOKEN_FREE_TOOLS } from "../src/core/mcp_card.ts"; | |
| 7 | import { mcpToolsSection } from "../src/web/llms_tools.ts"; | |
| 8 | ||
| 9 | for (const ssh of [false, true]) { | |
| 10 | test(`the three counts agree with the terminal ${ssh ? "on" : "off"}`, async () => { | |
| 11 | await loadMcp(); | |
| 12 | const origin = "https://forge.test"; | |
| 13 | const line = mcpToolsSection(origin, undefined, ssh).find((l) => / more tools with an account/.test(l))!; | |
| 14 | const llms = Number(line.match(/^- (\d+) more tools/)![1]); | |
| 15 | const anonymous = TOKEN_FREE_TOOLS.length; | |
| 16 | const total = mcpCard(origin, undefined, ssh).tools.length; | |
| 17 | expect(llms).toBe(total - anonymous); | |
| 18 | }); | |
| 19 | } |
new above
offline
Anonymous count is TOKEN_FREE_TOOLS.length, not the length of an anonymous tools/list; call the handler so the test follows what a stranger really gets.