mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
feat(coding-agent): codemode and MCP
This adds codemode and MCP support to pi. Codemode (packages/codemode) runs model-written JavaScript in a QuickJS wasm VM inside a worker. From there the script calls pi's tools as async functions, uses a per-session store, and can read the model catalog and run classifiers. The codemode tool in coding-agent is compatible with Codex's exec tool, and codemode.mode works like Codex's tool modes (on or only). Large results are written to a JSON temp file. MCP (packages/mcp) is a standalone client for stdio and streamable HTTP servers, with OAuth. The core only gets general mechanisms; codemode, tool_search and MCP are built-in extensions that use them. Other extensions can replace these built-ins. Tools declare an exposure: direct, model-only, codemode, deferred or hidden. prepareLoadout() lets an orchestrating tool change which tool declarations the model sees. ctx.executeTool() runs nested calls through validation and the tool_call/tool_result hooks. Those calls emit events with parentToolCallId and are recorded as bounded nestedCalls on the parent result, which compaction and HTML export use. Tools can also return structuredContent with an outputSchema, and can report errors with isError instead of throwing. MCP servers come from mcp.json (global, or per project after the project is trusted) or from pi.registerMcpServer(). They are managed with /mcp and pi mcp list|login|logout, and their runtimes load on first use. Closes #10040
This commit is contained in:
parent
b485fa3128
commit
8562bcf66a
143 changed files with 17620 additions and 225 deletions
60
packages/coding-agent/examples/sdk/14-codemode-mcp.ts
Normal file
60
packages/coding-agent/examples/sdk/14-codemode-mcp.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/**
|
||||
* Codemode, Tool Search, and MCP
|
||||
*
|
||||
* The CLI loads `codemode`, `tool_search`, and MCP as built-in extensions. SDK sessions add
|
||||
* them to the resource loader's extension factories.
|
||||
*
|
||||
* `codemode` and `tool_search` are registered inactive. Enable them through the `defaultTools` setting
|
||||
* (`tools` would also restrict the session to the named tools, which hides MCP tools), or let the
|
||||
* MCP extension activate them: `codemode` for servers with `codemode` or `codemode-deferred` exposure,
|
||||
* `tool_search` for servers with `deferred` exposure.
|
||||
*/
|
||||
|
||||
import {
|
||||
createAgentSession,
|
||||
createCodemodeExtension,
|
||||
createMcpExtension,
|
||||
createToolSearchExtension,
|
||||
DefaultResourceLoader,
|
||||
getAgentDir,
|
||||
SessionManager,
|
||||
SettingsManager,
|
||||
} from "@earendil-works/pi-coding-agent";
|
||||
|
||||
const cwd = process.cwd();
|
||||
|
||||
const resourceLoader = new DefaultResourceLoader({
|
||||
cwd,
|
||||
agentDir: getAgentDir(),
|
||||
extensionFactories: [
|
||||
createCodemodeExtension({ mode: "on" }),
|
||||
createToolSearchExtension(),
|
||||
// Reads mcp.json from the agent directory and the trusted project, like the CLI.
|
||||
createMcpExtension(),
|
||||
],
|
||||
});
|
||||
await resourceLoader.reload();
|
||||
|
||||
const settingsManager = SettingsManager.create(cwd);
|
||||
settingsManager.applyOverrides({ defaultTools: ["read", "bash", "edit", "write", "codemode", "tool_search"] });
|
||||
|
||||
const { session } = await createAgentSession({
|
||||
resourceLoader,
|
||||
settingsManager,
|
||||
sessionManager: SessionManager.inMemory(),
|
||||
});
|
||||
|
||||
try {
|
||||
// Emits session_start, which connects the MCP servers in the background.
|
||||
await session.bindExtensions({});
|
||||
console.log("Active tools:", session.getActiveToolNames().join(", "));
|
||||
session.subscribe((event) => {
|
||||
if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
|
||||
process.stdout.write(event.assistantMessageEvent.delta);
|
||||
}
|
||||
});
|
||||
await session.prompt("Use codemode to count the TypeScript files in src/ and list the three largest.");
|
||||
console.log();
|
||||
} finally {
|
||||
session.dispose();
|
||||
}
|
||||
|
|
@ -21,6 +21,7 @@ The runtime example shows how to build a recreate function that closes over proc
|
|||
| `11-sessions.ts` | In-memory, persistent, continue, list sessions |
|
||||
| `12-full-control.ts` | Replace everything, no discovery |
|
||||
| `13-session-runtime.ts` | Manage runtime-backed session replacement |
|
||||
| `14-codemode-mcp.ts` | Add the `codemode`, `tool_search`, and MCP extensions |
|
||||
|
||||
## Running
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue