Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

feat(lean): fold MCP behind the load_tool_schemas meta tool instead of skipping it

Rach Pradhan596681b58447
diff --git a/src/cli.zig b/src/cli.zig
index 2e3c0a49..8c9130f6 100644
--- a/src/cli.zig
+++ b/src/cli.zig
@@ -124,7 +124,7 @@ pub const usage_text =
\\ -w, --worktree <name> isolate this session in a git worktree (.graff/worktrees/<name>) so parallel agents don't collide on files
\\ --no-autocommit with -w, don't auto-commit each turn (default on; land work with `graff worktree merge`)
\\ --yolo skip all permission prompts for the session
- \\ --lean do not connect MCP servers at all — a smaller per-turn context prefix for one-shot/CI runs (GRAFF_LEAN=1)
+ \\ --lean one-shot/CI mode: slim tool surface (8 core tools), and MCP servers connect DEFERRED behind load_tool_schemas instead of paying full schemas every turn (GRAFF_LEAN=1)
\\ --no-local-tools embedder mode: hard-disable the built-in bash/bash_output/bash_kill/read_file/edit_file/write_file/codedb tools for the whole process (subagents included), so graff can run outside the sandbox and get its coding tools from an MCP server instead; webfetch, orchestration and MCP tools still work (GRAFF_NO_LOCAL_TOOLS=1)
\\ -p, --print one-shot print mode (answer on stdout, progress on stderr)
\\ --timing show per-tool wall-clock on result lines
diff --git a/src/main.zig b/src/main.zig
index f5a5991d..78f234ec 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -392,7 +392,7 @@ pub fn main(init: std.process.Init) !void {
try session_start.connectCompanion(io, arena, &registry_storage, flags, out, json_mode, init.environ_map);
const mcp_tools: []const mcp.Tool = registry_storage.tools;
// If the metered companion connected, probe its license once so the note below can lean into paid tools (vs the conservative free-codedb note).
- if (mcpServerConnected(mcp_tools, "codedbpro")) g_codedbpro_licensed = probeLicensedPinEager(gpa, arena, io);
+ if (!session_start.leanMode(flags.lean_flag, init.environ_map) and mcpServerConnected(mcp_tools, "codedbpro")) g_codedbpro_licensed = probeLicensedPinEager(gpa, arena, io);
boot.mark(io, "companion");
var approvals: Approvals = undefined;
diff --git a/src/mcp_schema_gate.zig b/src/mcp_schema_gate.zig
index 12d34012..2d371a73 100644
--- a/src/mcp_schema_gate.zig
+++ b/src/mcp_schema_gate.zig
@@ -178,6 +178,30 @@ pub fn configure(arena: Allocator, merged: mcp_config.Merged, environ_map: anyty
g_policy = policy;
}
+/// Startup-only promotion after configure(): a post-connect capability probe
+/// (the codedb-pro license check) lands AFTER the registry starts, so it
+/// appends here rather than arriving through env/config. Single-threaded
+/// startup contract — call it from main() before the first catalog render,
+/// never at request time (pool threads read g_policy concurrently).
+pub fn pinEagerRuntime(arena: Allocator, server: []const u8) void {
+ if (pinnedEager(server)) return;
+ var list: std.ArrayList([]const u8) = .empty;
+ list.appendSlice(arena, g_policy.eager) catch return;
+ list.append(arena, arena.dupe(u8, server) catch return) catch return;
+ g_policy.eager = list.items;
+}
+
+/// --lean: fold EVERY MCP server behind load_tool_schemas rather than skip
+/// MCP entirely — capability survives (a load call away), prefix cost drops
+/// to names + shortened descriptions. Clears eager pins too: a licensed
+/// companion's runtime pin is worth its bytes in an interactive session, not
+/// in a one-shot counting every token. GRAFF_MCP_SCHEMA_BUDGET=0 is the same
+/// policy reached by env; this is the flag's path to it.
+pub fn deferAllRuntime() void {
+ g_policy.budget = 0;
+ g_policy.eager = &.{};
+}
+
/// The server half of `mcp__<server>__<tool>`; "" for a name that is not one.
pub fn serverOf(qualified: []const u8) []const u8 {
const prefix = "mcp__";
diff --git a/src/no_local_tools.zig b/src/no_local_tools.zig
index 3c9febf6..d3856e71 100644
--- a/src/no_local_tools.zig
+++ b/src/no_local_tools.zig
@@ -79,7 +79,9 @@ pub var lean: bool = false;
/// (5.1k bytes): in an unattended non-yolo one-shot the root is
/// approval-gated out of bash/edit_file and delegation is the sanctioned
/// path (prompts.unattended_note) — dropping it would strand exactly the
-/// runs lean is built for.
+/// runs lean is built for. `load_tool_schemas` stays because lean folds MCP
+/// behind that meta tool (mcp_schema_gate.deferAllRuntime) instead of
+/// skipping it: the one-shot keeps full MCP capability a load call away.
pub const lean_tools = [_][]const u8{
"bash",
"read_file",
@@ -88,6 +90,7 @@ pub const lean_tools = [_][]const u8{
"codedb",
"subagent",
"attempt_completion",
+ "load_tool_schemas",
};
/// A name test only, so the catalog filter and tests share one list.
@@ -226,9 +229,10 @@ test "lean: the filter keeps exactly the seven one-shot tools, and allocates not
try std.testing.expectEqualStrings("read_file", kept[1].name);
try std.testing.expectEqualStrings("subagent", kept[2].name);
try std.testing.expectEqualStrings("attempt_completion", kept[3].name);
- // The keep-list is exactly the seven documented tools — subagent stays
- // (the unattended delegation path), everything orchestration/meta goes.
- try std.testing.expectEqual(@as(usize, 7), lean_tools.len);
+ // The keep-list is exactly the eight documented tools — subagent stays
+ // (the unattended delegation path), load_tool_schemas stays (lean folds
+ // MCP behind it), everything orchestration/meta goes.
+ try std.testing.expectEqual(@as(usize, 8), lean_tools.len);
for (lean_tools) |tool| try std.testing.expect(leanKeeps(tool));
try std.testing.expect(!leanKeeps("workflow"));
try std.testing.expect(!leanKeeps("todo_write"));
diff --git a/src/schema_protocol.zig b/src/schema_protocol.zig
index 076df334..68ef4e57 100644
--- a/src/schema_protocol.zig
+++ b/src/schema_protocol.zig
@@ -15,7 +15,7 @@ pub const flags =
\\ {"flag": "--allow-cross-provider-subagents", "arg": null, "description": "explicitly consent to sending worker prompts, code, and tool results to a provider different from the root"},
\\ {"flag": "--no-subagent-tier", "arg": null, "description": "opt out of the default worker tier ladder; workers inherit the root model unless an explicit --subagent-model is given"},
\\ {"flag": "--yolo", "arg": null, "description": "skip all permission prompts for the session"},
- \\ {"flag": "--lean", "arg": null, "description": "do not connect MCP servers at all: a smaller per-turn context prefix for one-shot/CI runs (connected servers pay their tool schemas into every model turn). GRAFF_LEAN=1 is the equivalent"},
+ \\ {"flag": "--lean", "arg": null, "description": "one-shot/CI mode: slim the tool surface to 8 core tools and connect MCP servers DEFERRED behind the load_tool_schemas meta tool (names + one-line descriptions up front, full schemas on demand) instead of paying every server's schemas in every model turn. GRAFF_LEAN=1 is the equivalent"},
\\ {"flag": "--no-local-tools", "arg": null, "description": "embedder mode: hard-disable the built-in bash/bash_output/bash_kill/read_file/edit_file/write_file/codedb tools for the whole process, so the harness can run outside the sandbox and source its coding tools from an MCP server instead; webfetch, orchestration and MCP tools are unaffected, and subagents inherit the gate. GRAFF_NO_LOCAL_TOOLS=1 is the equivalent"},
\\ {"flag": "--system-prompt", "arg": "text", "description": "replace the built-in system prompt (cwd project-instructions file is still appended)"},
\\ {"flag": "--append-system-prompt", "arg": "text", "description": "append extra text to the end of the system prompt"},
diff --git a/src/session_start.zig b/src/session_start.zig
index 383e5bbc..036e8c05 100644
--- a/src/session_start.zig
+++ b/src/session_start.zig
@@ -362,12 +362,14 @@ pub fn initTelemetry(io: Io, gpa: Allocator, client: *std.http.Client, environ_m
/// out of main() (600-line goal). Returns the Registry by value — mcp.Registry
/// holds no self-references (its storage is ArrayList/HashMap-backed), so
/// returning it is safe.
-/// --lean / GRAFF_LEAN=1: skip MCP connection entirely. Connected servers pay
-/// their tool schemas into the prefix of EVERY model turn — measured ~6k
-/// tokens for a licensed codedbpro alone against a ~7.4k native baseline, so
-/// an MCP-heavy config roughly doubles the context bill of a one-shot. Lean
-/// is the one-shot/CI answer; interactive sessions keep the consent flow.
-pub fn leanSkipsMcp(lean_flag: bool, environ_map: anytype) bool {
+/// --lean / GRAFF_LEAN=1. Lean no longer SKIPS MCP — it folds every server
+/// behind the load_tool_schemas meta tool (mcp_schema_gate.deferAllRuntime):
+/// connected servers pay names + one-line descriptions instead of full
+/// schemas, and capability survives a load call away. Measured prefix cost
+/// of eager MCP: ~3.3k tokens for a licensed codedbpro alone, on EVERY
+/// model turn. One-shot/CI runs count those; interactive keeps the consent
+/// flow and eager pins.
+pub fn leanMode(lean_flag: bool, environ_map: anytype) bool {
return lean_flag or environ_map.get("GRAFF_LEAN") != null;
}
@@ -382,6 +384,9 @@ pub fn initRegistryConsent(io: Io, gpa: Allocator, arena: Allocator, out: *Io.Wr
// #416: resolve eager-vs-deferred BEFORE anything connects, so the first
// catalog render already knows which servers pay their schemas up front.
mcp_schema_gate.configure(arena, merged, environ_map);
+ // --lean folds every server behind the meta tool (deferAllRuntime) rather
+ // than skipping MCP: capability a load call away, ~zero schema prefix.
+ if (leanMode(flags.lean_flag, environ_map)) mcp_schema_gate.deferAllRuntime();
if (!quiet) {
// Reported here rather than in `Registry.init`, which never runs when
// consent is declined — a config that does not parse must be named
@@ -393,9 +398,8 @@ pub fn initRegistryConsent(io: Io, gpa: Allocator, arena: Allocator, out: *Io.Wr
sink.emit(io, dimNotice(try std.fmt.allocPrint(arena, "ignoring ~/" ++ mcp_config.unsupported_rel_path ++ ": unsupported path — use ~/" ++ mcp_config.global_rel_path ++ " (global) or {s} (project)", .{mcp_config_path})));
}
const mcp_count = mcp_cli.countMcpServers(merged);
- const lean = leanSkipsMcp(flags.lean_flag, environ_map);
- var connect_mcp = !lean and (flags.yolo_flag or mcp_count == 0);
- if (mcp_count > 0 and !lean and !flags.yolo_flag and !json_mode and use_color) {
+ var connect_mcp = flags.yolo_flag or mcp_count == 0;
+ if (mcp_count > 0 and !flags.yolo_flag and !json_mode and use_color) {
sink.emit(io, .{ .mcp_consent_prompt = .{ .count = mcp_count } });
// Still an inline read: only the QUESTION is inverted here, and the
// answer becomes a typed command in #430 (input inversion).
@@ -439,9 +443,6 @@ pub fn probeLicensedPinEager(gpa: Allocator, arena: Allocator, io: Io) bool {
/// already main()-owned and stable by the time this is called, so a pointer
/// is all that's needed — no return-by-value trickery here).
pub fn connectCompanion(io: Io, arena: Allocator, registry: *mcp.Registry, flags: args.Flags, out: *Io.Writer, json_mode: bool, environ_map: anytype) !void {
- // --lean / GRAFF_LEAN=1 skips ALL MCP, the auto-companion and the bundled
- // Smolify registration included — lean means no MCP tool schemas at all.
- if (leanSkipsMcp(flags.lean_flag, environ_map)) return;
const sink = engine_sink.writerSink(out);
const speak = !json_mode and flags.oneshot_prompt == null;
connect: {
@@ -476,14 +477,14 @@ fn dimNotice(text: []const u8) engine_events.EngineEvent {
return .{ .session_notice = .{ .text = text, .tone = .dim } };
}
-test "lean skips MCP: flag or GRAFF_LEAN env, never the default" {
+test "lean mode: flag or GRAFF_LEAN env, never the default" {
var env = std.process.Environ.Map.init(std.testing.allocator);
defer env.deinit();
- try std.testing.expect(!leanSkipsMcp(false, env)); // default: consent flow decides
- try std.testing.expect(leanSkipsMcp(true, env)); // --lean wins alone
+ try std.testing.expect(!leanMode(false, env)); // default: consent flow decides
+ try std.testing.expect(leanMode(true, env)); // --lean wins alone
try env.put("GRAFF_LEAN", "1");
- try std.testing.expect(leanSkipsMcp(false, env)); // env wins alone
- try std.testing.expect(leanSkipsMcp(true, env));
+ try std.testing.expect(leanMode(false, env)); // env wins alone
+ try std.testing.expect(leanMode(true, env));
}
test "core Smolify registration is offline and lazy" {