mcp: refactor for testability and add comprehensive test suite

- Refactor mcp.Server and router to accept injected I/O streams.
- Implement McpHarness for high-fidelity MCP integration testing.
- Add unit tests for protocol, tools, and resources modules.
- Add integration tests covering initialization, tool/resource execution, and error handling.
- Improve error reporting for malformed JSON requests.
This commit is contained in:
Adrià Arrufat
2026-03-02 15:50:29 +09:00
parent 8a1795d56f
commit 64107f5957
8 changed files with 371 additions and 6 deletions

View File

@@ -105,3 +105,21 @@ pub fn handleRead(server: *Server, arena: std.mem.Allocator, req: protocol.Reque
return server.sendError(req.id.?, .InvalidRequest, "Resource not found");
}
}
const testing = @import("../testing.zig");
test "resource_list contains expected resources" {
try testing.expect(resource_list.len >= 2);
try testing.expectString("mcp://page/html", resource_list[0].uri);
try testing.expectString("mcp://page/markdown", resource_list[1].uri);
}
test "ReadParams parsing" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const aa = arena.allocator();
const raw = "{\"uri\": \"mcp://page/html\"}";
const parsed = try std.json.parseFromSlice(ReadParams, aa, raw, .{});
try testing.expectString("mcp://page/html", parsed.value.uri);
}