graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
mcp: an oversized stdio line no longer wedges the server (#1504)
justrach9fba8edfce9f
diff --git a/src/mcp_stdio.zig b/src/mcp_stdio.zigindex e4b21fc2..69e69f9f 100644--- a/src/mcp_stdio.zig+++ b/src/mcp_stdio.zig@@ -7,12 +7,42 @@ const Io = std.Io;/// One newline-delimited JSON-RPC line. A payload that fills graff's 1 MiB/// stdio reader used to surface as the opaque `WriteFailed` that auto-filed/// #527/#528/#552; map the cap to a diagnosable error instead.+///+/// #1504: `takeDelimiter` leaves the stream unchanged on `StreamTooLong`, so+/// the oversized line stayed in the full buffer and every later call on the+/// server failed the same way. The rest of the line is now discarded. An+/// oversized notification is skipped (the reply being awaited follows it);+/// anything else fails only the call that was reading it.pub fn takeLine(r: *Io.Reader) ![]u8 {- const line = r.takeDelimiter('\n') catch |err| switch (err) {- error.StreamTooLong => return error.McpResponseTooLarge,- else => return err,- };- return line orelse error.McpClosed;+ while (true) {+ const line = r.takeDelimiter('\n') catch |err| switch (err) {+ error.StreamTooLong => {+ const notification = isNotification(r.buffered());+ _ = r.discardDelimiterInclusive('\n') catch |e| switch (e) {+ error.EndOfStream => return error.McpClosed,+ else => return e,+ };+ if (notification) continue;+ return error.McpResponseTooLarge;+ },+ else => return err,+ };+ return line orelse error.McpClosed;+ }+}++/// Whether the start of a JSON-RPC line is a server notification: a+/// `"method"` naming `notifications/…` with no `"result"` or `"error"` ahead of+/// it. Only the line's prefix is available once it overflows the buffer.+fn isNotification(prefix: []const u8) bool {+ const head = prefix[0..@min(prefix.len, 4096)];+ const key = std.mem.indexOf(u8, head, "\"method\"") orelse return false;+ if (std.mem.indexOf(u8, head[0..key], "\"result\"") != null) return false;+ if (std.mem.indexOf(u8, head[0..key], "\"error\"") != null) return false;+ var rest = std.mem.trimStart(u8, head[key + "\"method\"".len ..], " \t");+ if (rest.len == 0 or rest[0] != ':') return false;+ rest = std.mem.trimStart(u8, rest[1..], " \t");+ return std.mem.startsWith(u8, rest, "\"notifications/");}/// Write one newline-delimited request. A dead child (binary replaced@@ -86,3 +116,31 @@ test "takeLine: one JSON-RPC line" {const line = try takeLine(&reader);try std.testing.expectEqualStrings("{\"ok\":true}", line);}++/// A stream whose first line overflows a 64-byte reader buffer.+fn oversizedStream(comptime head: []const u8) []const u8 {+ const pad: [200]u8 = @splat('x');+ return head ++ pad ++ "\"}\n{\"jsonrpc\":\"2.0\",\"id\":7,\"result\":{}}\n";+}++test "takeLine #1504: an oversized notification is skipped and the reply still arrives" {+ var buf: [64]u8 = undefined;+ var tr: std.testing.Reader = .init(&buf, &.{.{ .buffer = oversizedStream("{\"jsonrpc\":\"2.0\",\"method\":\"notifications/progress\",\"params\":{\"m\":\"") }});+ tr.artificial_limit = .limited(16);+ try std.testing.expectEqualStrings("{\"jsonrpc\":\"2.0\",\"id\":7,\"result\":{}}", try takeLine(&tr.interface));+}++test "takeLine #1504: an oversized reply fails its own call and the stream stays usable" {+ var buf: [64]u8 = undefined;+ var tr: std.testing.Reader = .init(&buf, &.{.{ .buffer = oversizedStream("{\"jsonrpc\":\"2.0\",\"id\":6,\"result\":{\"text\":\"") }});+ tr.artificial_limit = .limited(16);+ try std.testing.expectError(error.McpResponseTooLarge, takeLine(&tr.interface));+ try std.testing.expectEqualStrings("{\"jsonrpc\":\"2.0\",\"id\":7,\"result\":{}}", try takeLine(&tr.interface));+}++test "isNotification reads only the JSON-RPC envelope" {+ try std.testing.expect(isNotification("{\"jsonrpc\":\"2.0\", \"method\" : \"notifications/message\",\"params\":{"));+ try std.testing.expect(!isNotification("{\"jsonrpc\":\"2.0\",\"id\":3,\"method\":\"sampling/createMessage\""));+ try std.testing.expect(!isNotification("{\"jsonrpc\":\"2.0\",\"id\":3,\"result\":{\"method\":\"notifications/x\""));+ try std.testing.expect(!isNotification("{\"jsonrpc\":\"2.0\",\"id\":3,\"result\":{\"text\":\"\\\"method\\\":\\\"notifications/x"));+}