Skip to content

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

Python

Let rlm call MCP tools as tools.server.tool

justrach47a71cd08a05
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3d18eecd..272cd842 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@ current is part of cutting a release.
- Xiaomi's model list follows `https://api.xiaomimimo.com/v1/models` (same 6h cache and `graff models refresh` as the other live catalogs). Offline fallback and the default are MiMo-V2.6.
- `graff refresh` is the short form of `graff models refresh`.
+- `rlm` accepts `tools.server.tool(...)` for a loaded MCP tool. Same Zig caller, not a JavaScript heap.
- A background command that exits resumes the same session with its exit status and output, instead of waiting for another message (#1154).
- Desktop interface sounds are optional and off until turned on. Cuelume notices ship with the app bundle.
- Desktop task workspaces get their own Git checkouts, with a keep gate so dirty or unique trees are not dropped.
diff --git a/src/rlm.zig b/src/rlm.zig
index 34f6b2f9..2e2432dc 100644
--- a/src/rlm.zig
+++ b/src/rlm.zig
@@ -25,12 +25,12 @@ const mcp_shapes = @import("mcp_shapes.zig");
const read_miss = @import("read_miss.zig");
pub const tool_name = "rlm";
-pub const tool_desc = "Programmatic tool calling (RLM + sPTC). Functions ARE this session's tools. Literal read_file/codedb/bash/webfetch/sleep_ms/llm_query/subagent start as the script streams. Binds persist. subagent(\"task\") is sidecar-only (keep the critical-path next step local). Loaded MCP names are host functions after load_tool_schemas; each(arr, tool, field) maps a JSON array; len(x)/project(x, field) slim it. print() is the answer. Prefer one rlm over N tool calls.";
+pub const tool_desc = "Programmatic tool calling (RLM + sPTC). Functions ARE this session's tools. Literal read_file/codedb/bash/webfetch/sleep_ms/llm_query/subagent start as the script streams. Binds persist. subagent(\"task\") is sidecar-only (keep the critical-path next step local). Loaded MCP names are host functions after load_tool_schemas; tools.server.tool(...) is that name; each(arr, tool, field) maps a JSON array; len(x)/project(x, field) slim it. print() is the answer. Prefer one rlm over N tool calls.";
/// --lean catalog desc: same contract, no REPL essay. maybeAppend is after
/// compactLeanSpecs, so this is the one-shot wire text.
pub const lean_tool_desc = "Batch independent read_file/codedb/bash here. print(read_file(\"p\")) returns the file — do not catalog-read it again. Then edit_file/write_file/bash as catalog tools. Literal calls start as it streams. Binds persist. Loaded MCP names are host functions after load_tool_schemas.";
pub const tool_schema =
- \\{"type": "object", "properties": {"code": {"type": "string", "description": "Python-like script: name = read_file(\"path\") / codedb(\"command\") / bash(\"cmd\") / sleep_ms(ms) / llm_query(\"prompt\") / subagent(\"task\") / loaded mcp__server__tool(); each(arr, tool, field) maps a JSON array; len(x) and project(x, field) slim it; print(...) is the result. Assignments persist across rlm calls."}}, "required": ["code"]}
+ \\{"type": "object", "properties": {"code": {"type": "string", "description": "Python-like script: name = read_file(\"path\") / codedb(\"command\") / bash(\"cmd\") / sleep_ms(ms) / llm_query(\"prompt\") / subagent(\"task\") / loaded mcp__server__tool() or tools.server.tool(); each(arr, tool, field) maps a JSON array; len(x) and project(x, field) slim it; print(...) is the result. Assignments persist across rlm calls."}}, "required": ["code"]}
;
/// Process-global: on by default. `--old` / `--no-rlm` turn it off; `--rlm`
diff --git a/src/rlm_mcp.zig b/src/rlm_mcp.zig
index 432c38df..3ab16da0 100644
--- a/src/rlm_mcp.zig
+++ b/src/rlm_mcp.zig
@@ -24,7 +24,19 @@ pub const Prep = union(enum) {
};
pub fn looksMcp(name: []const u8) bool {
- return mcp.Registry.isMcp(name);
+ return mcp.Registry.isMcp(name) or codeModeParts(name) != null;
+}
+
+/// `tools.server.tool` or `server.tool` — OpenCode's call shape, no JS heap.
+pub fn codeModeParts(name: []const u8) ?struct { server: []const u8, tool: []const u8 } {
+ var rest = name;
+ if (std.mem.startsWith(u8, rest, "tools.")) rest = rest["tools.".len..];
+ const dot = std.mem.indexOfScalar(u8, rest, '.') orelse return null;
+ if (dot == 0 or std.mem.indexOfScalar(u8, rest[dot + 1 ..], '.') != null) return null;
+ const server = rest[0..dot];
+ const tool = rest[dot + 1 ..];
+ if (server.len == 0 or tool.len == 0) return null;
+ return .{ .server = server, .tool = tool };
}
/// Catalog natives stay native even when an MCP short name collides (#1016).
@@ -52,6 +64,14 @@ pub fn resolve(ctx: ToolCtx, name: []const u8) ?[]const u8 {
if (isNativeHost(name)) return null;
if (mcp.Registry.isMcp(name)) return name;
const reg = ctx.registry orelse return null;
+ if (codeModeParts(name)) |parts| {
+ for (reg.tools) |t| {
+ if (!std.mem.eql(u8, t.original_name, parts.tool)) continue;
+ if (!serverOf(t.qualified_name, parts.server)) continue;
+ return t.qualified_name;
+ }
+ return null;
+ }
var hit: ?[]const u8 = null;
for (reg.tools) |t| {
if (!std.mem.eql(u8, shortName(t.qualified_name), name)) continue;
@@ -61,6 +81,13 @@ pub fn resolve(ctx: ToolCtx, name: []const u8) ?[]const u8 {
return hit;
}
+fn serverOf(qualified: []const u8, server: []const u8) bool {
+ if (!std.mem.startsWith(u8, qualified, "mcp__")) return false;
+ const rest = qualified["mcp__".len..];
+ const sep = std.mem.indexOf(u8, rest, "__") orelse return false;
+ return std.mem.eql(u8, rest[0..sep], server);
+}
+
fn shortName(qualified: []const u8) []const u8 {
if (!std.mem.startsWith(u8, qualified, "mcp__")) return qualified;
const rest = qualified["mcp__".len..];
@@ -292,6 +319,44 @@ test "unloaded MCP name is refused; loaded name is prepared for exec" {
}
}
+test "tools.server.tool resolves to the loaded MCP name" {
+ const gpa = std.testing.allocator;
+ var arena_state = std.heap.ArenaAllocator.init(gpa);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ const io = std.testing.io;
+ mcp_schema_gate.reset();
+ defer mcp_schema_gate.reset();
+ var schema: std.json.ObjectMap = .empty;
+ try schema.put(arena, "type", .{ .string = "object" });
+ var tool = [_]mcp.Tool{.{
+ .server_index = 0,
+ .original_name = "ask_wiki_question",
+ .qualified_name = "mcp__deepwiki__ask_wiki_question",
+ .description = "ask",
+ .input_schema = .{ .object = schema },
+ }};
+ var registry = mcp.Registry.empty(gpa, io);
+ defer registry.deinit();
+ registry.tools = tool[0..];
+ var dummy_client: std.http.Client = .{ .allocator = gpa, .io = io };
+ defer dummy_client.deinit();
+ const ctx: ToolCtx = .{
+ .gpa = gpa,
+ .io = io,
+ .client = &dummy_client,
+ .provider = undefined,
+ .registry = &registry,
+ .from_sub = false,
+ .approvals = null,
+ .tracer = null,
+ };
+ try std.testing.expectEqualStrings("mcp__deepwiki__ask_wiki_question", resolve(ctx, "tools.deepwiki.ask_wiki_question").?);
+ try std.testing.expectEqualStrings("mcp__deepwiki__ask_wiki_question", resolve(ctx, "deepwiki.ask_wiki_question").?);
+ try std.testing.expect(looksMcp("tools.missing.nope"));
+ try std.testing.expect(resolve(ctx, "tools.missing.nope") == null);
+}
+
test "MCP-inside-rlm host functions are on by default" {
try std.testing.expect(host_enabled);
}
diff --git a/src/rlm_spec.zig b/src/rlm_spec.zig
index f6d21a87..85a2a638 100644
--- a/src/rlm_spec.zig
+++ b/src/rlm_spec.zig
@@ -22,7 +22,7 @@ pub var run_host: ?*const fn (ToolCtx, spec_ptc.Call) ToolOutput = null;
/// `subagent("task")` is Prime-style recursion via graff's existing subagent
/// tool. v1 is synchronous; speculate() still overlaps independent calls.
/// Keyword `run_in_background=true` returns an agent id (a handle).
-pub const system_note = "\n\nrlm(code): session tools as a script. Literal read_file/codedb/bash/llm_query/subagent start as it streams. Binds persist until /new or /clear. subagent(\"task\") is sidecar-only; keep the critical-path next step local. Loaded MCP names are host functions; each() maps a JSON array; len/project slim it. print(...) is the answer.";
+pub const system_note = "\n\nrlm(code): session tools as a script. Literal read_file/codedb/bash/llm_query/subagent start as it streams. Binds persist until /new or /clear. subagent(\"task\") is sidecar-only; keep the critical-path next step local. Loaded MCP names are host functions (tools.server.tool(...) too); each() maps a JSON array; len/project slim it. print(...) is the answer.";
/// One REPL assignment. `runScript` seeds these from the process-local store
/// so a later `rlm` call can `print(prev)` without re-reading. Owned by the
diff --git a/src/spec_ptc.zig b/src/spec_ptc.zig
index e2d81656..3227b090 100644
--- a/src/spec_ptc.zig
+++ b/src/spec_ptc.zig
@@ -80,7 +80,7 @@ pub fn extractCall(arena: Allocator, stmt: []const u8) !?Call {
const call_src = afterOptionalAssign(trimmed) orelse return null;
const open = std.mem.indexOfScalar(u8, call_src, '(') orelse return null;
const name = std.mem.trim(u8, call_src[0..open], " \t");
- if (!isIdent(name)) return null;
+ if (!isCallName(name)) return null;
if (std.mem.eql(u8, name, "print")) return null;
const close = lastParen(call_src) orelse return null;
const inner = call_src[open + 1 .. close];
@@ -163,6 +163,17 @@ fn isIdent(s: []const u8) bool {
return true;
}
+/// `name(...)` or Code Mode `tools.server.tool(...)` / `server.tool(...)`.
+/// Not a general dotted expression — two idents, optional `tools.` prefix.
+fn isCallName(s: []const u8) bool {
+ if (isIdent(s)) return true;
+ var rest = s;
+ if (std.mem.startsWith(u8, rest, "tools.")) rest = rest["tools.".len..];
+ const dot = std.mem.indexOfScalar(u8, rest, '.') orelse return false;
+ if (std.mem.indexOfScalar(u8, rest[dot + 1 ..], '.') != null) return false;
+ return isIdent(rest[0..dot]) and isIdent(rest[dot + 1 ..]);
+}
+
fn identStart(c: u8) bool {
return (c >= 'A' and c <= 'Z') or (c >= 'a' and c <= 'z') or c == '_';
}
@@ -450,6 +461,9 @@ test "extractCall reads positional and keyword literals; print and names are ski
const q = (try extractCall(a, "n = llm_query(\"summarize this chunk\")")).?;
try std.testing.expectEqualStrings("llm_query", q.name);
try std.testing.expectEqualStrings("{\"prompt\":\"summarize this chunk\"}", q.args_json);
+ const dotted = (try extractCall(a, "tools.deepwiki.ask_wiki_question(repoName=\"anomalyco/opencode\", question=\"how\")")).?;
+ try std.testing.expectEqualStrings("tools.deepwiki.ask_wiki_question", dotted.name);
+ try std.testing.expect(std.mem.indexOf(u8, dotted.args_json, "anomalyco/opencode") != null);
try std.testing.expect(try extractCall(a, "print(x)") == null);
try std.testing.expect(try extractCall(a, "read_file(path)") == null);
try std.testing.expect(try extractCall(a, "# just a comment") == null);