The mind's MCP server tells its client which part of the config's class failed, and run does not wait for the class
Jonas Templestein, 2026-11-26 15:56 UTC · 5bddc52f173f975c7d2f14a06c6536bd7e023156 · parent ac30cfa
4 files changed
- M core/lib/src/mind-config.ts
- M core/lib/src/sdk/mcp.ts
- M core/mind-worker/src/mcp-config.test.ts
- M core/mind-worker/src/mcp.ts
M core/lib/src/mind-config.ts
@@ -66,7 +66,8 @@ * that answers an MCP server's class for the client that asks (iterate/sdk `McpServerTarget`): * its instructions, the description of `run`, more tools, and the mind's skills. Absent: the * platform's defaults. Set, and the method fails, is slow or answers what is no such class: - * the failure is logged and the defaults answer. */ + * the failure is logged, the defaults answer, and the server tells the client which part + * failed. */ mcpTarget: z .string() .regex(/^[A-Za-z_$][\w$]{0,63}$/, "the name of a method")
M core/lib/src/sdk/mcp.ts
@@ -17,7 +17,8 @@ // - THE MIND'S OWN MCP SERVER, at its control-plane hostname, for members. The platform serves // it, with its tool `run`. A deploy's config may name a method of the config's class // (`mcpTarget`, iterate/mind-config) that answers the class for a client; what the class says -// is added to the platform's defaults, and when it fails the defaults answer alone. +// is added to the platform's defaults. When a part of it fails, the default answers for that +// part, and the server tells the client which part failed. // - ANY HOST OF THE MIND, from the config's `fetch`: `this.auth.mcpServer(request, { target })` // (./auth.ts) answers for `/mcp` and names the method that answers the class. import { RpcTarget } from "cloudflare:workers";
M core/mind-worker/src/mcp-config.test.ts
@@ -218,11 +218,15 @@ const [defaultRun] = (defaults.result || {}).tools as { name: string; description: string }[]; expect(defaultRun).toMatchObject({ name: "run" }); - const custom = classOf({ - instructions: () => "Read the skills first.", - tools: () => [{ name: "run", description: "Run, as this mind says." }, { name: "hello" }], - hello: () => "hi", - }); + const calls: unknown[][] = []; + const custom = classOf( + { + instructions: () => "Read the skills first.", + tools: () => [{ name: "run", description: "Run, as this mind says." }, { name: "hello" }], + hello: () => "hi", + }, + calls, + ); expect((await answerOf(await mind(custom, "initialize", INIT))).result).toMatchObject({ instructions: "Read the skills first.", }); @@ -231,6 +235,19 @@ expect.objectContaining({ name: "run", description: "Run, as this mind says." }), ]); + // A CALL OF `run` ASKS THE CLASS NOTHING: the tool is the platform's, so a slow class holds no + // run. A call of the class's own tool asks for its tools, and then calls the method. + calls.length = 0; + const ran = await answerOf( + await mind(custom, "tools/call", { name: "run", arguments: { script: "async () => 1" } }), + ); + expect(ran.result).toHaveProperty("content"); + expect(calls).toEqual([]); + expect( + (await answerOf(await mind(custom, "tools/call", { name: "hello", arguments: {} }))).result, + ).toMatchObject({ content: [{ type: "text", text: "hi" }], isError: false }); + expect(calls.map(([, method]) => method)).toEqual(["tools", "hello"]); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); const broken = classOf({ instructions: () => { @@ -244,17 +261,76 @@ await vi.advanceTimersByTimeAsync(6000); vi.useRealTimers(); const init = await answerOf(await pending); - expect(init.result?.instructions).toMatch(/^One tool, `run/); + // the client is told what failed, before the default instructions + const [note, defaultInstructions] = String(init.result?.instructions).split("\n\n"); + expect(note).toMatch(/^THIS MIND'S OWN MCP SETTINGS FAILED/); + expect(note).toContain( + "What failed: instructions() threw an error; skills() gave no answer in 5000 ms; tools() answered what the platform cannot use.", + ); + expect(defaultInstructions).toMatch(/^One tool, `run/); expect(init.result?.capabilities).not.toHaveProperty("resources"); - expect((await answerOf(await mind(broken, "tools/list"))).result?.tools).toEqual([ - expect.objectContaining({ name: "run", description: defaultRun.description }), + // A LIST OF THE TOOLS THAT THE CLASS FAILED TO ANSWER has `run` alone, and its description + // begins with the note: the instructions do not travel with a list + const listed = ((await answerOf(await mind(broken, "tools/list"))).result || {}).tools; + expect(listed).toEqual([ + expect.objectContaining({ + name: "run", + description: expect.stringMatching( + /^THIS MIND'S OWN MCP SETTINGS FAILED.* What failed: tools\(\) answered what the platform cannot use\. /, + ), + }), ]); + expect(listed).toEqual([ + expect.objectContaining({ description: expect.stringContaining(defaultRun.description) }), + ]); + // THE ERROR'S OWN TEXT REACHES NO CLIENT: the config's code wrote it, perhaps from another + // party's answer, and a client hands what it gets to a model + expect(JSON.stringify([init, listed])).not.toContain("the config broke"); // each failure is one log line: the initialize asked all three, the list its tools again expect(warn.mock.calls.map(([line]) => (line as { what: string }).what).sort()).toEqual([ "iterate-mind.instructions", "iterate-mind.skills", "iterate-mind.tools", "iterate-mind.tools", + ]); + warn.mockRestore(); +}); + +test("the mind's own server tells its client which part of the class failed; an app's server does not", async () => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + const half = classOf({ + instructions: () => "Read the skills first.", + skills: () => { + throw new Error("no folder\n /srv/config/skills"); + }, + }); + // ONE PART FAILS: the note names it in the platform's words, and the class's own instructions + // stay + const own = await serveMcp( + post("initialize", INIT, { "x-itx-principal": JSON.stringify(member) }), + () => Promise.reject(new Error("no file is read")), + half, + ).then(answerOf); + expect(own.result?.instructions).toMatch( + /^THIS MIND'S OWN MCP SETTINGS FAILED.* What failed: skills\(\) threw an error\. .*\n\nRead the skills first\.$/, + ); + expect(JSON.stringify(own)).not.toContain("/srv/config"); + // a class that has no such method failed nothing: no note + const quiet = await serveMcp( + post("initialize", INIT, { "x-itx-principal": JSON.stringify(member) }), + () => Promise.reject(new Error("no file is read")), + classOf({ instructions: () => "Read the skills first." }), + ).then(answerOf); + expect(quiet.result?.instructions).toBe("Read the skills first."); + + // AN APP'S SERVER answers people who are no members: the words of the config's code stay in + // the log + const told = await app(half, "initialize", INIT); + expect(told.result?.instructions).toBe("Read the skills first."); + expect(JSON.stringify(told)).not.toContain("/srv/config"); + expect(warn.mock.calls.map(([line]) => (line as { what: string }).what)).toEqual([ + "iterate-mind.skills", + "guestbook.skills", ]); warn.mockRestore(); });
M core/mind-worker/src/mcp.ts
@@ -24,7 +24,9 @@ // // THE CONFIG CANNOT BREAK A SERVER. Each thing it is asked (`askConfig`) has a time limit and a // schema. What fails, is slow or does not fit is logged, and the default stands: the mind's own -// server then answers as it does with no `mcpTarget`. +// server then answers that part as it does with no `mcpTarget`, and its instructions tell the +// client what failed (`configFailedNote`). A call of the platform's own `run` asks the config +// nothing, so a slow class holds no run. /** One call of the config's class for this server: `method` with `args` * (iterate/sdk `IterateConfigEntrypoint.mcpCall`). */ @@ -334,6 +336,9 @@ instructions?: string; tools?: z.infer<typeof ConfigTools>; skills?: SkillCatalog; + /** Each part that failed, as "<method>() <why>" in the platform's words, on the mind's own + * server alone. */ + failed?: string[]; }; const CONFIG_ANSWER_TIMEOUT_MS = 5000; @@ -369,27 +374,32 @@ const AnyResult = z.looseObject({}); /** ONE QUESTION TO THE CONFIG'S CLASS, which cannot break the server: its answer within the time - * limit and in `schema`'s shape, or undefined, with one log line that says what went wrong. */ + * limit and in `schema`'s shape, or undefined, with one log line that says what went wrong. + * `failed` gets why in the platform's own words, one of three. The error's text goes to the log + * alone: the config's code wrote it, and may have copied it from somewhere else. */ async function askConfig<T>( ask: () => Promise<unknown>, schema: z.ZodType<T>, what: string, + failed: (why: string) => void, ): Promise<T | undefined> { let timer: ReturnType<typeof setTimeout> | undefined; + let why = "threw an error"; try { const answer = await Promise.race([ ask(), new Promise<never>((_resolve, reject) => { - timer = setTimeout( - () => reject(new Error(`no answer in ${CONFIG_ANSWER_TIMEOUT_MS} ms`)), - CONFIG_ANSWER_TIMEOUT_MS, - ); + timer = setTimeout(() => { + why = `gave no answer in ${CONFIG_ANSWER_TIMEOUT_MS} ms`; + reject(new Error(`no answer in ${CONFIG_ANSWER_TIMEOUT_MS} ms`)); + }, CONFIG_ANSWER_TIMEOUT_MS); }), ]); // the class has no such method: the default stands, and nothing is wrong if (!answer) return undefined; const parsed = schema.safeParse(answer); if (parsed.success) return parsed.data; + why = "answered what the platform cannot use"; throw new Error(`not the expected shape: ${parsed.error.issues[0]?.message || "invalid"}`); } catch (error) { console.warn({ @@ -397,14 +407,31 @@ what, message: (error instanceof Error ? error.message : String(error)).slice(0, 300), }); + failed(why); return undefined; } finally { clearTimeout(timer); } } +/** WHAT THE MIND'S OWN SERVER TELLS A CLIENT WHEN THE CONFIG'S CLASS FAILED. Without it a broken + * config looks like a mind that has no instructions, tools or skills of its own, and only the + * log says otherwise. It goes first in the instructions, and first in the description of `run` + * when a list of the tools failed: a client may cut either (Claude Code keeps 4,096 characters + * of the instructions). + * + * EVERY WORD OF IT IS THE PLATFORM'S. `failed` names one of the three methods and one of three + * reasons (`askConfig`). An error's own text stays in the log: a client hands these words to its + * model, so text that the config's code took from another party would instruct that model, and + * a secret in it would go to the model's provider. + * + * An app's server does not say it: the people it answers can do nothing about the mind's config. */ +const configFailedNote = (failed: string[]) => + `THIS MIND'S OWN MCP SETTINGS FAILED, and the platform's defaults answer in their place. What failed: ${failed.join("; ")}. Tell the person you work for: the mind's own instructions, tools or skills can be missing here. \`run\` works. The class that failed is in the mind's config repo (\`mcp.ts\` in the default config), and the mind Worker's log says why (\`mcp.config-target-failed\`).`; + function buildServer(caller: Caller, served: Served, said: Said, client: McpClient): McpServer { - const instructions = said.instructions || (served.run ? runInstructions : undefined); + const own = said.instructions || (served.run ? runInstructions : undefined); + const instructions = said.failed && own ? `${configFailedNote(said.failed)}\n\n${own}` : own; const mcpServer = new McpServer( { name: served.name, version: "0.1.0" }, instructions ? { instructions } : {}, @@ -443,7 +470,11 @@ "run", { title: "Run a script", - description: said.tools?.find((tool) => tool.name === "run")?.description || runDescription, + // a list that the class failed to answer has `run` alone: its description says so + description: [ + ...(said.failed ? [configFailedNote(said.failed)] : []), + said.tools?.find((tool) => tool.name === "run")?.description || runDescription, + ].join("\n\n"), inputSchema: fromJsonSchema( { type: "object", @@ -553,17 +584,28 @@ const said: Said = {}; const { config } = served; if (config) { + const failed: string[] = []; const say = <T>(method: string, schema: z.ZodType<T>) => - askConfig(() => config(client, method, []), schema, `${served.name}.${method}`); - const [instructions, tools, files] = await Promise.all([ + askConfig( + () => config(client, method, []), + schema, + `${served.name}.${method}`, + (why) => failed.push(`${method}() ${why}`), + ); + // A call of the platform's own `run` needs nothing of the class: it does not wait for one + // that is slow. + const tools = asked.tools && !(served.run && asked.callsRunAlone); + const [instructions, toolList, files] = await Promise.all([ asked.instructions ? say("instructions", ConfigInstructions) : undefined, // an initialize says whether the server has tools and skills at all - asked.tools || asked.instructions ? say("tools", ConfigTools) : undefined, + tools || asked.instructions ? say("tools", ConfigTools) : undefined, asked.skills || asked.instructions ? say("skills", ConfigSkillFiles) : undefined, ]); said.instructions = instructions; - said.tools = tools; + said.tools = toolList; if (files) said.skills = await skillCatalog(files); + // in one order, whichever part failed first + if (served.run && failed.length > 0) said.failed = failed.sort(); } return createMcpHandler( () => buildServer(caller, served, said, client), @@ -616,13 +658,14 @@ } const ClientInfo = z.looseObject({ name: z.string().max(256), version: z.string().max(64) }); -/** One JSON-RPC message, as far as this file reads it before the SDK does: its method, and how - * the client named itself (`initialize`'s `clientInfo`, or each call's envelope from MCP - * 2026-07-28 on). */ +/** One JSON-RPC message, as far as this file reads it before the SDK does: its method, the tool + * a `tools/call` names, and how the client named itself (`initialize`'s `clientInfo`, or each + * call's envelope from MCP 2026-07-28 on). */ const AskedMessage = z.looseObject({ method: z.string(), params: z .looseObject({ + name: z.string().optional().catch(undefined), clientInfo: ClientInfo.optional().catch(undefined), _meta: z .looseObject({ @@ -647,6 +690,7 @@ return parsed.success ? [parsed.data] : []; }); const methods = messages.map((message) => message.method); + const ofTools = messages.filter((message) => message.method.startsWith("tools/")); const named = messages .map( (message) => @@ -656,7 +700,11 @@ return { client: named ? { name: named.name, version: named.version } : {}, instructions: methods.some((method) => method === "initialize" || method === "server/discover"), - tools: methods.some((method) => method.startsWith("tools/")), + tools: ofTools.length > 0, + /** Every message about tools is a call of the tool named `run`. */ + callsRunAlone: + ofTools.length > 0 && + ofTools.every((message) => message.method === "tools/call" && message.params?.name === "run"), skills: methods.some( (method) => method.startsWith("skills/") || method.startsWith("resources/"), ),