mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-28 15:40:04 +00:00
mcp: optimize dispatching and simplify test harness
- Use StaticStringMap and enums for method, tool, and resource lookups. - Implement comptime JSON minification for tool schemas. - Refactor router and harness to use more efficient buffered polling. - Consolidate integration tests and add synchronous unit tests.
This commit is contained in:
@@ -64,6 +64,16 @@ const ResourceStreamingResult = struct {
|
||||
};
|
||||
};
|
||||
|
||||
const ResourceUri = enum {
|
||||
@"mcp://page/html",
|
||||
@"mcp://page/markdown",
|
||||
};
|
||||
|
||||
const resource_map = std.StaticStringMap(ResourceUri).initComptime(.{
|
||||
.{ "mcp://page/html", .@"mcp://page/html" },
|
||||
.{ "mcp://page/markdown", .@"mcp://page/markdown" },
|
||||
});
|
||||
|
||||
pub fn handleRead(server: *Server, arena: std.mem.Allocator, req: protocol.Request) !void {
|
||||
if (req.params == null) {
|
||||
return server.sendError(req.id.?, .InvalidParams, "Missing params");
|
||||
@@ -73,26 +83,31 @@ pub fn handleRead(server: *Server, arena: std.mem.Allocator, req: protocol.Reque
|
||||
return server.sendError(req.id.?, .InvalidParams, "Invalid params");
|
||||
};
|
||||
|
||||
if (std.mem.eql(u8, params.uri, "mcp://page/html")) {
|
||||
const result: ResourceStreamingResult = .{
|
||||
.contents = &.{.{
|
||||
.uri = params.uri,
|
||||
.mimeType = "text/html",
|
||||
.text = .{ .server = server, .format = .html },
|
||||
}},
|
||||
};
|
||||
try server.sendResult(req.id.?, result);
|
||||
} else if (std.mem.eql(u8, params.uri, "mcp://page/markdown")) {
|
||||
const result: ResourceStreamingResult = .{
|
||||
.contents = &.{.{
|
||||
.uri = params.uri,
|
||||
.mimeType = "text/markdown",
|
||||
.text = .{ .server = server, .format = .markdown },
|
||||
}},
|
||||
};
|
||||
try server.sendResult(req.id.?, result);
|
||||
} else {
|
||||
const uri = resource_map.get(params.uri) orelse {
|
||||
return server.sendError(req.id.?, .InvalidRequest, "Resource not found");
|
||||
};
|
||||
|
||||
switch (uri) {
|
||||
.@"mcp://page/html" => {
|
||||
const result: ResourceStreamingResult = .{
|
||||
.contents = &.{.{
|
||||
.uri = params.uri,
|
||||
.mimeType = "text/html",
|
||||
.text = .{ .server = server, .format = .html },
|
||||
}},
|
||||
};
|
||||
try server.sendResult(req.id.?, result);
|
||||
},
|
||||
.@"mcp://page/markdown" => {
|
||||
const result: ResourceStreamingResult = .{
|
||||
.contents = &.{.{
|
||||
.uri = params.uri,
|
||||
.mimeType = "text/markdown",
|
||||
.text = .{ .server = server, .format = .markdown },
|
||||
}},
|
||||
};
|
||||
try server.sendResult(req.id.?, result);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user