feat(mcp): add ping request handling

This commit is contained in:
evalstate
2026-03-14 17:48:29 +00:00
parent 42bb2f3c58
commit 93ea95af24
3 changed files with 64 additions and 7 deletions

View File

@@ -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();