Files
fil/docs/snippets/zig/mcp/mcp_custom_client.md
Henrik Jess Nielsen b4c07d3693
All checks were successful
Deploy fil (kreuzberg) / deploy (push) Successful in 49s
Nomad changes
2026-06-01 23:40:55 +02:00

1.1 KiB

const std = @import("std");

// The Zig binding does not expose an MCP client. To talk to the bundled
// `kreuzberg mcp` server, spawn the CLI as a subprocess and exchange
// JSON-RPC messages over stdin/stdout.
pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    var child = std.process.Child.init(&.{ "kreuzberg", "mcp" }, allocator);
    child.stdin_behavior = .Pipe;
    child.stdout_behavior = .Pipe;
    child.stderr_behavior = .Inherit;
    try child.spawn();

    const request =
        \\{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"extract_file","arguments":{"path":"document.pdf"}}}
        ++ "\n";

    if (child.stdin) |stdin| {
        try stdin.writeAll(request);
        stdin.close();
        child.stdin = null;
    }

    if (child.stdout) |stdout| {
        const response = try stdout.reader().readAllAlloc(allocator, 16 * 1024 * 1024);
        defer allocator.free(response);
        try std.io.getStdOut().writer().print("{s}\n", .{response});
    }

    _ = try child.wait();
}