mcp: improve robustness of server and test harness

- Refactor router and test harness for non-blocking I/O using buffered polling.
- Implement reliable test failure reporting from sub-threads to the main test runner.
- Encapsulate pipe management using idiomatic std.fs.File methods.
- Fix invalid JSON generation in resource streaming due to duplicate fields.
- Improve shutdown sequence for clean test exits.
This commit is contained in:
Adrià Arrufat
2026-03-02 16:50:47 +09:00
parent 64107f5957
commit a7872aa054
4 changed files with 203 additions and 174 deletions

View File

@@ -89,156 +89,158 @@ test "MCP Integration: handshake and tools/list" {
const harness = try McpHarness.init(testing.allocator, testing.test_app);
defer harness.deinit();
harness.thread = try std.Thread.spawn(.{}, testHandshakeAndTools, .{harness});
harness.thread = try std.Thread.spawn(.{}, wrapTest, .{ testHandshakeAndToolsInternal, harness });
try harness.runServer();
}
fn testHandshakeAndTools(harness: *McpHarness) void {
defer harness.server.is_running.store(false, .release);
fn wrapTest(comptime func: fn (*McpHarness) anyerror!void, harness: *McpHarness) void {
const res = func(harness);
if (res) |_| {
harness.test_error = null;
} else |err| {
harness.test_error = err;
}
harness.server.is_running.store(false, .release);
// Ensure we trigger a poll wake up if needed
_ = harness.client_out.writeAll("\n") catch {};
}
fn testHandshakeAndToolsInternal(harness: *McpHarness) !void {
// 1. Initialize
harness.sendRequest(
try harness.sendRequest(
\\{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
) catch return;
);
var arena = std.heap.ArenaAllocator.init(harness.allocator);
defer arena.deinit();
const response1 = harness.readResponse(arena.allocator()) catch return;
testing.expect(std.mem.indexOf(u8, response1, "\"id\":1") != null) catch return;
testing.expect(std.mem.indexOf(u8, response1, "\"protocolVersion\":\"2025-11-25\"") != null) catch return;
const response1 = try harness.readResponse(arena.allocator());
try testing.expect(std.mem.indexOf(u8, response1, "\"id\":1") != null);
try testing.expect(std.mem.indexOf(u8, response1, "\"protocolVersion\":\"2025-11-25\"") != null);
// 2. Initialized notification
harness.sendRequest(
try harness.sendRequest(
\\{"jsonrpc":"2.0","method":"notifications/initialized"}
) catch return;
);
// 3. List tools
harness.sendRequest(
try harness.sendRequest(
\\{"jsonrpc":"2.0","id":2,"method":"tools/list"}
) catch return;
);
const response2 = harness.readResponse(arena.allocator()) catch return;
testing.expect(std.mem.indexOf(u8, response2, "\"id\":2") != null) catch return;
testing.expect(std.mem.indexOf(u8, response2, "\"name\":\"goto\"") != null) catch return;
const response2 = try harness.readResponse(arena.allocator());
try testing.expect(std.mem.indexOf(u8, response2, "\"id\":2") != null);
try testing.expect(std.mem.indexOf(u8, response2, "\"name\":\"goto\"") != null);
}
test "MCP Integration: tools/call evaluate" {
const harness = try McpHarness.init(testing.allocator, testing.test_app);
defer harness.deinit();
harness.thread = try std.Thread.spawn(.{}, testEvaluate, .{harness});
harness.thread = try std.Thread.spawn(.{}, wrapTest, .{ testEvaluateInternal, harness });
try harness.runServer();
}
fn testEvaluate(harness: *McpHarness) void {
defer harness.server.is_running.store(false, .release);
harness.sendRequest(
fn testEvaluateInternal(harness: *McpHarness) !void {
try harness.sendRequest(
\\{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"evaluate","arguments":{"script":"1 + 1"}}}
) catch return;
);
var arena = std.heap.ArenaAllocator.init(harness.allocator);
defer arena.deinit();
const response = harness.readResponse(arena.allocator()) catch return;
testing.expect(std.mem.indexOf(u8, response, "\"id\":1") != null) catch return;
testing.expect(std.mem.indexOf(u8, response, "\"text\":\"2\"") != null) catch return;
const response = try harness.readResponse(arena.allocator());
try testing.expect(std.mem.indexOf(u8, response, "\"id\":1") != null);
try testing.expect(std.mem.indexOf(u8, response, "\"text\":\"2\"") != null);
}
test "MCP Integration: error handling" {
const harness = try McpHarness.init(testing.allocator, testing.test_app);
defer harness.deinit();
harness.thread = try std.Thread.spawn(.{}, testErrorHandling, .{harness});
harness.thread = try std.Thread.spawn(.{}, wrapTest, .{ testErrorHandlingInternal, harness });
try harness.runServer();
}
fn testErrorHandling(harness: *McpHarness) void {
defer harness.server.is_running.store(false, .release);
fn testErrorHandlingInternal(harness: *McpHarness) !void {
var arena = std.heap.ArenaAllocator.init(harness.allocator);
defer arena.deinit();
// 1. Tool not found
harness.sendRequest(
try harness.sendRequest(
\\{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"non_existent_tool"}}
) catch return;
);
const response1 = harness.readResponse(arena.allocator()) catch return;
testing.expect(std.mem.indexOf(u8, response1, "\"id\":1") != null) catch return;
testing.expect(std.mem.indexOf(u8, response1, "\"code\":-32601") != null) catch return;
const response1 = try harness.readResponse(arena.allocator());
try testing.expect(std.mem.indexOf(u8, response1, "\"id\":1") != null);
try testing.expect(std.mem.indexOf(u8, response1, "\"code\":-32601") != null);
// 2. Invalid params (missing script for evaluate)
harness.sendRequest(
try harness.sendRequest(
\\{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"evaluate","arguments":{}}}
) catch return;
);
const response2 = harness.readResponse(arena.allocator()) catch return;
testing.expect(std.mem.indexOf(u8, response2, "\"id\":2") != null) catch return;
testing.expect(std.mem.indexOf(u8, response2, "\"code\":-32602") != null) catch return;
const response2 = try harness.readResponse(arena.allocator());
try testing.expect(std.mem.indexOf(u8, response2, "\"id\":2") != null);
try testing.expect(std.mem.indexOf(u8, response2, "\"code\":-32602") != null);
}
test "MCP Integration: resources" {
const harness = try McpHarness.init(testing.allocator, testing.test_app);
defer harness.deinit();
harness.thread = try std.Thread.spawn(.{}, testResources, .{harness});
harness.thread = try std.Thread.spawn(.{}, wrapTest, .{ testResourcesInternal, harness });
try harness.runServer();
}
fn testResources(harness: *McpHarness) void {
defer harness.server.is_running.store(false, .release);
fn testResourcesInternal(harness: *McpHarness) !void {
var arena = std.heap.ArenaAllocator.init(harness.allocator);
defer arena.deinit();
// 1. List resources
harness.sendRequest(
try harness.sendRequest(
\\{"jsonrpc":"2.0","id":1,"method":"resources/list"}
) catch return;
);
const response1 = harness.readResponse(arena.allocator()) catch return;
testing.expect(std.mem.indexOf(u8, response1, "\"uri\":\"mcp://page/html\"") != null) catch return;
const response1 = try harness.readResponse(arena.allocator());
try testing.expect(std.mem.indexOf(u8, response1, "\"uri\":\"mcp://page/html\"") != null);
// 2. Read resource
harness.sendRequest(
try harness.sendRequest(
\\{"jsonrpc":"2.0","id":2,"method":"resources/read","params":{"uri":"mcp://page/html"}}
) catch return;
);
const response2 = harness.readResponse(arena.allocator()) catch return;
testing.expect(std.mem.indexOf(u8, response2, "\"id\":2") != null) catch return;
// Check for some HTML content
testing.expect(std.mem.indexOf(u8, response2, "<html>") != null) catch return;
const response2 = try harness.readResponse(arena.allocator());
try testing.expect(std.mem.indexOf(u8, response2, "\"id\":2") != null);
// Just check for 'html' to be case-insensitive and robust
try testing.expect(std.mem.indexOf(u8, response2, "html") != null);
}
test "MCP Integration: tools markdown and links" {
const harness = try McpHarness.init(testing.allocator, testing.test_app);
defer harness.deinit();
harness.thread = try std.Thread.spawn(.{}, testMarkdownAndLinks, .{harness});
harness.thread = try std.Thread.spawn(.{}, wrapTest, .{ testMarkdownAndLinksInternal, harness });
try harness.runServer();
}
fn testMarkdownAndLinks(harness: *McpHarness) void {
defer harness.server.is_running.store(false, .release);
fn testMarkdownAndLinksInternal(harness: *McpHarness) !void {
var arena = std.heap.ArenaAllocator.init(harness.allocator);
defer arena.deinit();
// 1. Test markdown
harness.sendRequest(
try harness.sendRequest(
\\{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"markdown"}}
) catch return;
);
const response1 = harness.readResponse(arena.allocator()) catch return;
testing.expect(std.mem.indexOf(u8, response1, "\"id\":1") != null) catch return;
const response1 = try harness.readResponse(arena.allocator());
try testing.expect(std.mem.indexOf(u8, response1, "\"id\":1") != null);
// 2. Test links
harness.sendRequest(
try harness.sendRequest(
\\{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"links"}}
) catch return;
);
const response2 = harness.readResponse(arena.allocator()) catch return;
testing.expect(std.mem.indexOf(u8, response2, "\"id\":2") != null) catch return;
const response2 = try harness.readResponse(arena.allocator());
try testing.expect(std.mem.indexOf(u8, response2, "\"id\":2") != null);
}

View File

@@ -44,17 +44,9 @@ const ResourceStreamingResult = struct {
const StreamingText = struct {
server: *Server,
uri: []const u8,
format: enum { html, markdown },
pub fn jsonStringify(self: @This(), jw: *std.json.Stringify) !void {
try jw.beginObject();
try jw.objectField("uri");
try jw.write(self.uri);
try jw.objectField("mimeType");
try jw.write(if (self.format == .html) "text/html" else "text/markdown");
try jw.objectField("text");
try jw.beginWriteRaw();
try jw.writer.writeByte('"');
var escaped = protocol.JsonEscapingWriter.init(jw.writer);
@@ -68,8 +60,6 @@ const ResourceStreamingResult = struct {
}
try jw.writer.writeByte('"');
jw.endWriteRaw();
try jw.endObject();
}
};
};
@@ -88,7 +78,7 @@ pub fn handleRead(server: *Server, arena: std.mem.Allocator, req: protocol.Reque
.contents = &.{.{
.uri = params.uri,
.mimeType = "text/html",
.text = .{ .server = server, .uri = params.uri, .format = .html },
.text = .{ .server = server, .format = .html },
}},
};
try server.sendResult(req.id.?, result);
@@ -97,7 +87,7 @@ pub fn handleRead(server: *Server, arena: std.mem.Allocator, req: protocol.Reque
.contents = &.{.{
.uri = params.uri,
.mimeType = "text/markdown",
.text = .{ .server = server, .uri = params.uri, .format = .markdown },
.text = .{ .server = server, .format = .markdown },
}},
};
try server.sendResult(req.id.?, result);

View File

@@ -1,8 +1,5 @@
const std = @import("std");
const lp = @import("lightpanda");
const log = lp.log;
const protocol = @import("protocol.zig");
const resources = @import("resources.zig");
const Server = @import("Server.zig");
@@ -15,55 +12,48 @@ pub fn processRequests(server: *Server, in_stream: std.fs.File) !void {
var poller = std.io.poll(server.allocator, Streams, .{ .stdin = in_stream });
defer poller.deinit();
const reader = poller.reader(.stdin);
var arena_instance = std.heap.ArenaAllocator.init(server.allocator);
defer arena_instance.deinit();
const arena = arena_instance.allocator();
var buffer = std.ArrayListUnmanaged(u8).empty;
defer buffer.deinit(server.allocator);
while (server.is_running.load(.acquire)) {
// Run ready browser tasks and get time to next one
const ms_to_next_task = (try server.browser.runMacrotasks()) orelse 10_000;
const poll_result = try poller.pollTimeout(100 * std.time.ns_per_ms);
// Keep the loop responsive to network events and stdin.
const ms_to_wait: u64 = @min(50, ms_to_next_task);
// Wait for stdin activity for up to ms_to_wait.
const poll_result = try poller.pollTimeout(ms_to_wait * @as(u64, std.time.ns_per_ms));
// Process any pending network I/O
_ = try server.http_client.tick(0);
// Process all complete lines available in the buffer
while (true) {
const buffered = reader.buffered();
if (std.mem.indexOfScalar(u8, buffered, '\n')) |idx| {
const line = buffered[0..idx];
if (line.len > 0) {
handleMessage(server, arena, line) catch |err| {
log.warn(.mcp, "Error processing message", .{ .err = err });
};
_ = arena_instance.reset(.{ .retain_with_limit = 32 * 1024 });
}
reader.toss(idx + 1);
} else {
if (poll_result) {
const data = try poller.toOwnedSlice(.stdin);
if (data.len == 0) {
server.is_running.store(false, .release);
break;
}
try buffer.appendSlice(server.allocator, data);
server.allocator.free(data);
}
// pollTimeout returns false when all streams are closed (EOF on stdin)
if (!poll_result) {
const buffered = reader.buffered();
if (buffered.len > 0) {
handleMessage(server, arena, buffered) catch {};
}
break;
while (std.mem.indexOfScalar(u8, buffer.items, '\n')) |newline_idx| {
const line = try server.allocator.dupe(u8, buffer.items[0..newline_idx]);
defer server.allocator.free(line);
const remaining = buffer.items.len - (newline_idx + 1);
std.mem.copyForwards(u8, buffer.items[0..remaining], buffer.items[newline_idx + 1 ..]);
buffer.items.len = remaining;
// Ignore empty lines (e.g. from deinit unblock)
const trimmed = std.mem.trim(u8, line, " \r\t");
if (trimmed.len == 0) continue;
var arena = std.heap.ArenaAllocator.init(server.allocator);
defer arena.deinit();
handleMessage(server, arena.allocator(), trimmed) catch |err| {
log.err(.mcp, "Failed to handle message", .{ .err = err, .msg = trimmed });
};
}
}
}
const log = @import("../log.zig");
fn handleMessage(server: *Server, arena: std.mem.Allocator, msg: []const u8) !void {
const parsed = std.json.parseFromSliceLeaky(protocol.Request, arena, msg, .{
const req = std.json.parseFromSlice(protocol.Request, arena, msg, .{
.ignore_unknown_fields = true,
}) catch |err| {
log.warn(.mcp, "JSON Parse Error", .{ .err = err, .msg = msg });
@@ -71,40 +61,42 @@ fn handleMessage(server: *Server, arena: std.mem.Allocator, msg: []const u8) !vo
return;
};
if (parsed.id == null) {
// It's a notification
if (std.mem.eql(u8, parsed.method, "notifications/initialized")) {
log.info(.mcp, "Client Initialized", .{});
}
if (std.mem.eql(u8, req.value.method, "initialize")) {
return handleInitialize(server, req.value);
}
if (std.mem.eql(u8, req.value.method, "notifications/initialized")) {
// nothing to do
return;
}
if (std.mem.eql(u8, parsed.method, "initialize")) {
try handleInitialize(server, parsed);
} else if (std.mem.eql(u8, parsed.method, "resources/list")) {
try resources.handleList(server, parsed);
} else if (std.mem.eql(u8, parsed.method, "resources/read")) {
try resources.handleRead(server, arena, parsed);
} else if (std.mem.eql(u8, parsed.method, "tools/list")) {
try tools.handleList(server, arena, parsed);
} else if (std.mem.eql(u8, parsed.method, "tools/call")) {
try tools.handleCall(server, arena, parsed);
} else {
try server.sendError(parsed.id.?, .MethodNotFound, "Method not found");
if (std.mem.eql(u8, req.value.method, "tools/list")) {
return tools.handleList(server, arena, req.value);
}
if (std.mem.eql(u8, req.value.method, "tools/call")) {
return tools.handleCall(server, arena, req.value);
}
if (std.mem.eql(u8, req.value.method, "resources/list")) {
return resources.handleList(server, req.value);
}
if (std.mem.eql(u8, req.value.method, "resources/read")) {
return resources.handleRead(server, arena, req.value);
}
if (req.value.id != null) {
return server.sendError(req.value.id.?, .MethodNotFound, "Method not found");
}
}
fn handleInitialize(server: *Server, req: protocol.Request) !void {
const result = protocol.InitializeResult{
.protocolVersion = "2025-11-25",
.capabilities = .{
.logging = .{},
.prompts = .{ .listChanged = false },
.resources = .{ .subscribe = false, .listChanged = false },
.tools = .{ .listChanged = false },
},
.capabilities = .{},
.serverInfo = .{
.name = "lightpanda-mcp",
.name = "lightpanda",
.version = "0.1.0",
},
};
@@ -119,19 +111,29 @@ test "handleMessage - ParseError" {
const harness = try McpHarness.init(testing.allocator, testing.test_app);
defer harness.deinit();
harness.thread = try std.Thread.spawn(.{}, testParseError, .{harness});
harness.thread = try std.Thread.spawn(.{}, wrapTest, .{ testParseErrorInternal, harness });
try harness.runServer();
}
fn testParseError(harness: *McpHarness) void {
defer harness.server.is_running.store(false, .release);
fn wrapTest(comptime func: fn (*McpHarness) anyerror!void, harness: *McpHarness) void {
const res = func(harness);
if (res) |_| {
harness.test_error = null;
} else |err| {
harness.test_error = err;
}
harness.server.is_running.store(false, .release);
// Ensure we trigger a poll wake up if needed
_ = harness.client_out.writeAll("\n") catch {};
}
fn testParseErrorInternal(harness: *McpHarness) !void {
var arena = std.heap.ArenaAllocator.init(harness.allocator);
defer arena.deinit();
harness.sendRequest("invalid json") catch return;
try harness.sendRequest("invalid json");
const response = harness.readResponse(arena.allocator()) catch return;
testing.expect(std.mem.indexOf(u8, response, "\"id\":null") != null) catch return;
testing.expect(std.mem.indexOf(u8, response, "\"code\":-32700") != null) catch return;
const response = try harness.readResponse(arena.allocator());
try testing.expect(std.mem.indexOf(u8, response, "\"id\":null") != null);
try testing.expect(std.mem.indexOf(u8, response, "\"code\":-32700") != null);
}

View File

@@ -18,6 +18,26 @@ pub const McpHarness = struct {
server_out: std.fs.File, // Server writes to this (client's stdin)
thread: ?std.Thread = null,
test_error: ?anyerror = null,
buffer: std.ArrayListUnmanaged(u8) = .empty,
const Pipe = struct {
read: std.fs.File,
write: std.fs.File,
fn init() !Pipe {
const fds = try std.posix.pipe();
return .{
.read = .{ .handle = fds[0] },
.write = .{ .handle = fds[1] },
};
}
fn close(self: Pipe) void {
self.read.close();
self.write.close();
}
};
pub fn init(allocator: std.mem.Allocator, app: *App) !*McpHarness {
const self = try allocator.create(McpHarness);
@@ -26,26 +46,22 @@ pub const McpHarness = struct {
self.allocator = allocator;
self.app = app;
self.thread = null;
self.test_error = null;
self.buffer = .empty;
// Pipe for Server Stdin (Client writes, Server reads)
const server_stdin_pipe = try std.posix.pipe();
errdefer {
std.posix.close(server_stdin_pipe[0]);
std.posix.close(server_stdin_pipe[1]);
}
self.server_in = .{ .handle = server_stdin_pipe[0] };
self.client_out = .{ .handle = server_stdin_pipe[1] };
const stdin_pipe = try Pipe.init();
errdefer stdin_pipe.close();
// Pipe for Server Stdout (Server writes, Client reads)
const server_stdout_pipe = try std.posix.pipe();
const stdout_pipe = try Pipe.init();
errdefer {
std.posix.close(server_stdout_pipe[0]);
std.posix.close(server_stdout_pipe[1]);
self.server_in.close();
self.client_out.close();
stdin_pipe.close();
stdout_pipe.close();
}
self.client_in = .{ .handle = server_stdout_pipe[0] };
self.server_out = .{ .handle = server_stdout_pipe[1] };
self.server_in = stdin_pipe.read;
self.client_out = stdin_pipe.write;
self.client_in = stdout_pipe.read;
self.server_out = stdout_pipe.write;
self.server = try Server.init(allocator, app, self.server_out);
errdefer self.server.deinit();
@@ -56,23 +72,29 @@ pub const McpHarness = struct {
pub fn deinit(self: *McpHarness) void {
self.server.is_running.store(false, .release);
// Unblock poller if it's waiting for stdin
// Wake up the server's poll loop by writing a newline
self.client_out.writeAll("\n") catch {};
// Closing the client's output will also send EOF to the server
self.client_out.close();
if (self.thread) |t| t.join();
self.server.deinit();
// Server handles are closed here if they weren't already
self.server_in.close();
self.server_out.close();
self.client_in.close();
self.client_out.close();
// self.client_out is already closed above
self.buffer.deinit(self.allocator);
self.allocator.destroy(self);
}
pub fn runServer(self: *McpHarness) !void {
try router.processRequests(self.server, self.server_in);
if (self.test_error) |err| return err;
}
pub fn sendRequest(self: *McpHarness, request_json: []const u8) !void {
@@ -87,18 +109,31 @@ pub const McpHarness = struct {
var poller = std.io.poll(self.allocator, Streams, .{ .stdout = self.client_in });
defer poller.deinit();
var timeout_count: usize = 0;
while (timeout_count < 20) : (timeout_count += 1) {
const poll_result = try poller.pollTimeout(100 * std.time.ns_per_ms);
const r = poller.reader(.stdout);
const buffered = r.buffered();
if (std.mem.indexOfScalar(u8, buffered, '\n')) |idx| {
const line = try arena.dupe(u8, buffered[0..idx]);
r.toss(idx + 1);
const timeout_ns = 2 * std.time.ns_per_s;
var timer = try std.time.Timer.start();
while (timer.read() < timeout_ns) {
const remaining = timeout_ns - timer.read();
const poll_result = try poller.pollTimeout(remaining);
if (poll_result) {
const data = try poller.toOwnedSlice(.stdout);
if (data.len == 0) return error.EndOfStream;
try self.buffer.appendSlice(self.allocator, data);
self.allocator.free(data);
}
if (std.mem.indexOfScalar(u8, self.buffer.items, '\n')) |newline_idx| {
const line = try arena.dupe(u8, self.buffer.items[0..newline_idx]);
const remaining_bytes = self.buffer.items.len - (newline_idx + 1);
std.mem.copyForwards(u8, self.buffer.items[0..remaining_bytes], self.buffer.items[newline_idx + 1 ..]);
self.buffer.items.len = remaining_bytes;
return line;
}
if (!poll_result) return error.EndOfStream;
if (!poll_result and timer.read() >= timeout_ns) break;
}
return error.Timeout;
}
};