mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-30 09:08:55 +00:00
feat(mcp): add ping request handling
This commit is contained in:
@@ -34,6 +34,7 @@ const log = @import("../log.zig");
|
||||
|
||||
const Method = enum {
|
||||
initialize,
|
||||
ping,
|
||||
@"notifications/initialized",
|
||||
@"tools/list",
|
||||
@"tools/call",
|
||||
@@ -43,6 +44,7 @@ const Method = enum {
|
||||
|
||||
const method_map = std.StaticStringMap(Method).initComptime(.{
|
||||
.{ "initialize", .initialize },
|
||||
.{ "ping", .ping },
|
||||
.{ "notifications/initialized", .@"notifications/initialized" },
|
||||
.{ "tools/list", .@"tools/list" },
|
||||
.{ "tools/call", .@"tools/call" },
|
||||
@@ -68,6 +70,7 @@ pub fn handleMessage(server: *Server, arena: std.mem.Allocator, msg: []const u8)
|
||||
|
||||
switch (method) {
|
||||
.initialize => try handleInitialize(server, req),
|
||||
.ping => try handlePing(server, req),
|
||||
.@"notifications/initialized" => {},
|
||||
.@"tools/list" => try tools.handleList(server, arena, req),
|
||||
.@"tools/call" => try tools.handleCall(server, arena, req),
|
||||
@@ -92,6 +95,11 @@ fn handleInitialize(server: *Server, req: protocol.Request) !void {
|
||||
try server.sendResult(req.id.?, result);
|
||||
}
|
||||
|
||||
fn handlePing(server: *Server, req: protocol.Request) !void {
|
||||
const id = req.id orelse return;
|
||||
try server.sendResult(id, .{});
|
||||
}
|
||||
|
||||
const testing = @import("../testing.zig");
|
||||
|
||||
test "MCP.router - handleMessage - synchronous unit tests" {
|
||||
@@ -116,22 +124,29 @@ test "MCP.router - handleMessage - synchronous unit tests" {
|
||||
, out_alloc.writer.buffered());
|
||||
out_alloc.writer.end = 0;
|
||||
|
||||
// 2. Tools list
|
||||
// 2. Ping
|
||||
try handleMessage(server, aa,
|
||||
\\{"jsonrpc":"2.0","id":2,"method":"tools/list"}
|
||||
\\{"jsonrpc":"2.0","id":2,"method":"ping"}
|
||||
);
|
||||
try testing.expectJson(.{ .id = 2 }, out_alloc.writer.buffered());
|
||||
try testing.expectJson(.{ .id = 2, .result = .{} }, out_alloc.writer.buffered());
|
||||
out_alloc.writer.end = 0;
|
||||
|
||||
// 3. Tools list
|
||||
try handleMessage(server, aa,
|
||||
\\{"jsonrpc":"2.0","id":3,"method":"tools/list"}
|
||||
);
|
||||
try testing.expectJson(.{ .id = 3 }, out_alloc.writer.buffered());
|
||||
try testing.expect(std.mem.indexOf(u8, out_alloc.writer.buffered(), "\"name\":\"goto\"") != null);
|
||||
out_alloc.writer.end = 0;
|
||||
|
||||
// 3. Method not found
|
||||
// 4. Method not found
|
||||
try handleMessage(server, aa,
|
||||
\\{"jsonrpc":"2.0","id":3,"method":"unknown_method"}
|
||||
\\{"jsonrpc":"2.0","id":4,"method":"unknown_method"}
|
||||
);
|
||||
try testing.expectJson(.{ .id = 3, .@"error" = .{ .code = -32601 } }, out_alloc.writer.buffered());
|
||||
try testing.expectJson(.{ .id = 4, .@"error" = .{ .code = -32601 } }, out_alloc.writer.buffered());
|
||||
out_alloc.writer.end = 0;
|
||||
|
||||
// 4. Parse error
|
||||
// 5. Parse error
|
||||
{
|
||||
const filter: testing.LogFilter = .init(.mcp);
|
||||
defer filter.deinit();
|
||||
|
||||
Reference in New Issue
Block a user