The best way to control Claude Code, Codex and more from any computer or phone
fix(pi): load T3 tool schemas for completion gateways (#18643)
Wout Stiens committed
Oct 13, 2026 at 19:40 UTC
4d7683557ad4a2411d6de405e500e4a56b43d0e1
3 files changed
+88
-32
packages/provider-pi/src/server/mcpBridge.testkit.ts
+4
-1
@@ -30,7 +30,10 @@ interface RegisteredTool {
30
31
type AgentStartHook = (
32
event: { systemPrompt: string },
33
- ctx: { ui: { notify: (message: string, severity: string) => void } },
33
+ ctx: {
34
+ model?: { api: string };
35
+ ui: { notify: (message: string, severity: string) => void };
36
+ },
37
) => Promise<{ systemPrompt: string }>;
38
39
export async function loadMcpBridge(
packages/provider-pi/src/server/mcpExtensionSource.test.ts
+61
-18
@@ -111,18 +111,40 @@ describe("Pi MCP tool results", () => {
111
});
112
113
describe("Pi MCP tool exposure", () => {
114
+ const discoveryContext = {
115
+ model: { api: "anthropic-messages" },
116
+ ui: { notify: () => undefined },
117
+ };
118
+
119
+ it.each(["session_start", "session_tree", "model_select", "before_agent_start"])(
120
+ "declares optional T3 tools for OpenAI-compatible gateways at %s",
121
+ async (hookName) => {
122
+ const bridge = await loadMcpBridge({ modern: true, toolSearchAvailable: true });
123
+ const hook = bridge.handlers.get(hookName);
124
+ assert.isDefined(hook);
125
+ await hook!(
126
+ { systemPrompt: "Pi system prompt" },
127
+ { model: { api: "openai-completions" }, ui: { notify: assert.fail } },
128
+ );
129
+ assert.isTrue(
130
+ bridge.tools
131
+ .filter((tool) => tool.name.startsWith("mcp__t3-code__"))
132
+ .every((tool) => bridge.getActiveTools().includes(tool.name)),
133
+ );
134
+ const result = await bridge.tools
135
+ .find((tool) => tool.name === "mcp__t3-code__preview_snapshot")!
136
+ .execute("gateway-call", {});
137
+ assert.equal(result.content[0]?.text, "browser snapshot");
138
+ assert.equal(bridge.tools.filter((tool) => tool.exposure === "hidden").length, 4);
139
+ },
140
+ );
141
+
142
it("keeps orchestration direct and optional bridge tools discoverable on modern Pi", async () => {
143
const bridge = await loadMcpBridge({ modern: true, toolSearchAvailable: true });
116
- await bridge.handlers.get("session_start")!(
117
- { systemPrompt: "" },
118
- { ui: { notify: () => undefined } },
119
- );
144
+ await bridge.handlers.get("session_start")!({ systemPrompt: "" }, discoveryContext);
145
const start = bridge.handlers.get("before_agent_start");
146
assert.isDefined(start);
122
- const prompt = await start!(
123
- { systemPrompt: "Pi system prompt" },
124
- { ui: { notify: () => undefined } },
125
- );
147
+ const prompt = await start!({ systemPrompt: "Pi system prompt" }, discoveryContext);
148
assert.include(prompt.systemPrompt, "orchestrator_capabilities");
149
assert.equal(bridge.servers.length, 0);
150
assert.equal(bridge.tools.length, 8);
@@ -154,6 +176,33 @@ describe("Pi MCP tool exposure", () => {
176
assert.equal(bridge.requests.at(-1)?.method, "tools/call");
177
});
178
179
+ it("restores gateway declarations after model switches and older tree loadouts without excluded tools", async () => {
180
+ const bridge = await loadMcpBridge({
181
+ modern: true,
182
+ toolSearchAvailable: true,
183
+ allowsTool: (name) => name !== "mcp__t3-code__delegate_task",
184
+ });
185
+ const event = { systemPrompt: "" };
186
+ await bridge.handlers.get("session_start")!(event, discoveryContext);
187
+ const gatewayContext = { model: { api: "openai-completions" }, ui: { notify: assert.fail } };
188
+ await bridge.handlers.get("model_select")!(event, gatewayContext);
189
+ bridge.restoreActiveTools(["read", "mcp__t3-code__task_status"]);
190
+ await bridge.handlers.get("session_tree")!(event, gatewayContext);
191
+ assert.deepEqual(bridge.getActiveTools(), [
192
+ "read",
193
+ "mcp__t3-code__task_status",
194
+ "tool_search",
195
+ "mcp__t3-code__orchestrator_capabilities",
196
+ "mcp__t3-code__preview_snapshot",
197
+ ]);
198
+ await bridge.handlers.get("model_select")!(event, discoveryContext);
199
+ assert.equal(
200
+ bridge.tools.find((tool) => tool.name === "mcp__t3-code__preview_snapshot")?.exposure,
201
+ "deferred",
202
+ );
203
+ assert.include(bridge.getActiveTools(), "mcp__t3-code__preview_snapshot");
204
+ });
205
+
206
it.each(["legacy Pi", "disabled tool search"])(
207
"keeps tool execution available with %s",
208
async (mode) => {
@@ -164,7 +213,7 @@ describe("Pi MCP tool exposure", () => {
213
});
214
if (mode !== "legacy Pi") {
215
const start = bridge.handlers.get("session_start");
167
- await start!({ systemPrompt: "Pi system prompt" }, { ui: { notify: () => undefined } });
216
+ await start!({ systemPrompt: "Pi system prompt" }, discoveryContext);
217
}
218
assert.equal(bridge.tools.filter((tool) => tool.exposure !== "hidden").length, 4);
219
assert.isTrue(
@@ -198,10 +247,7 @@ describe("Pi MCP tool exposure", () => {
247
allowsTool: (name) =>
248
name === "read" || name === "tool_search" || name.startsWith("mcp__t3-code__"),
249
});
201
- await bridge.handlers.get("session_start")!(
202
- { systemPrompt: "" },
203
- { ui: { notify: () => undefined } },
204
- );
250
+ await bridge.handlers.get("session_start")!({ systemPrompt: "" }, discoveryContext);
251
assert.equal(bridge.tools.length, 4);
252
assert.equal(bridge.tools.filter((tool) => tool.exposure === "direct").length, 3);
253
const tool = bridge.tools.find((tool) => tool.name === "mcp__t3-code__preview_snapshot");
@@ -218,10 +264,7 @@ describe("Pi MCP tool exposure", () => {
264
allowsTool: (name) =>
265
name !== "mcp__t3-code__delegate_task" && (!excludeSearch || name !== "tool_search"),
266
});
221
- await bridge.handlers.get("session_start")!(
222
- { systemPrompt: "" },
223
- { ui: { notify: () => undefined } },
224
- );
267
+ await bridge.handlers.get("session_start")!({ systemPrompt: "" }, discoveryContext);
268
bridge.restoreActiveTools([
269
"read",
270
"mcp__t3-code__task_status",
@@ -229,7 +272,7 @@ describe("Pi MCP tool exposure", () => {
272
]);
273
const tree = bridge.handlers.get("session_tree");
274
assert.isDefined(tree);
232
- await tree!({ systemPrompt: "" }, { ui: { notify: () => undefined } });
275
+ await tree!({ systemPrompt: "" }, discoveryContext);
276
assert.deepEqual(bridge.getActiveTools(), [
277
"read",
278
"mcp__t3-code__task_status",
packages/provider-pi/src/server/mcpExtensionSource.ts
+23
-13
@@ -1,8 +1,9 @@
1
/**
2
* Source for the T3-owned Pi extension that consumes T3's HTTP MCP server.
3
*
4
- * Pi 0.99+ discovers optional HTTP bridge tools on demand. Older versions
5
- * keep them directly available. Pi loads this TypeScript via `--extension`; the
4
+ * Pi 0.99+ discovers optional HTTP bridge tools on demand. OpenAI completion
5
+ * gateways need their schemas loaded up front. Older versions keep tools
6
+ * directly available. Pi loads this TypeScript via `--extension`; the
7
* server writes it to a cache so packaged builds need no sibling .ts file.
8
*
9
* Do not import t3code modules from the string body. The Pi process resolves
@@ -24,7 +25,7 @@ export const T3_PI_MCP_EXTENSION_PATH_ENV = "T3_PI_MCP_EXTENSION_PATH";
25
export const PI_FILE_CHANGE_TOOLS = ["edit", "write"] as const;
26
27
export const PI_T3_MCP_EXTENSION_SOURCE = `\
27
-import { stripFrontmatter, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
28
+import { stripFrontmatter, type ExtensionAPI, type ExtensionContext } from "@earendil-works/pi-coding-agent";
29
import * as NodeFSP from "node:fs/promises";
30
import * as NodePath from "node:path";
31
import { Type } from "typebox";
@@ -421,23 +422,31 @@ export default async function t3McpExtension(pi: ExtensionAPI) {
422
// mcp.json configures it; retry a failed connection at session_start.
423
await ensureStarted().catch(() => undefined);
424
424
- const reconcileDiscovery = () => {
425
+ const reconcileDiscovery = (ctx: Pick<ExtensionContext, "model">) => {
426
if (!supportsExposure) return;
427
// A disabled or replaced search builtin cannot discover deferred tools.
428
const hasToolSearch = hasBuiltinToolSearch();
429
const exposureChanged = deferOptionalTools !== hasToolSearch;
430
deferOptionalTools = hasToolSearch;
431
if (exposureChanged) registerTools();
431
- if (hasToolSearch) {
432
- const active = pi.getActiveTools();
433
- if (!active.includes("tool_search")) pi.setActiveTools([...active, "tool_search"]);
434
- }
432
+ const active = pi.getActiveTools();
433
+ // Completion gateways can silently drop calls to undeclared tools named
434
+ // in history or guidance. Eagerly load T3 schemas, including after tree
435
+ // restores, while retaining deferred exposure for Pi's allowlist rules.
436
+ // Pi still applies --tools/--exclude-tools to this active loadout.
437
+ const required = [
438
+ ...(hasToolSearch ? ["tool_search"] : []),
439
+ ...(ctx.model?.api === "openai-completions" ? catalog.map((tool) => "mcp__t3-code__" + tool.name) : []),
440
+ ];
441
+ const missing = required.filter((name) => !active.includes(name));
442
+ if (missing.length > 0) pi.setActiveTools([...active, ...missing]);
443
};
444
445
// Tree navigation restores its saved loadout after session_start.
438
- pi.on("session_tree", reconcileDiscovery);
446
+ pi.on("session_tree", (_event, ctx) => reconcileDiscovery(ctx));
447
+ pi.on("model_select", (_event, ctx) => reconcileDiscovery(ctx));
448
pi.on("session_start", async (_event, ctx) => {
440
- reconcileDiscovery();
449
+ reconcileDiscovery(ctx);
450
try {
451
await ensureStarted();
452
} catch (error) {
@@ -449,8 +458,9 @@ export default async function t3McpExtension(pi: ExtensionAPI) {
458
// Deliver orchestration guidance through pi's real system-prompt channel.
459
// Wrapping the first user message instead would stop it from starting
460
// with "/" and silently break slash-command expansion.
452
- pi.on("before_agent_start", (event) => ({
453
- systemPrompt: event.systemPrompt + "\\n\\n" + ORCHESTRATION_INSTRUCTIONS,
454
- }));
461
+ pi.on("before_agent_start", (event, ctx) => {
462
+ reconcileDiscovery(ctx);
463
+ return { systemPrompt: event.systemPrompt + "\\n\\n" + ORCHESTRATION_INSTRUCTIONS };
464
+ });
465
}
466
`;