mcp: handle missing request IDs safely

This commit is contained in:
Adrià Arrufat
2026-03-27 11:34:06 +09:00
parent c8d8ca5e94
commit de32e5cf34
3 changed files with 6 additions and 3 deletions

View File

@@ -22,7 +22,8 @@ pub const resource_list = [_]protocol.Resource{
};
pub fn handleList(server: *Server, req: protocol.Request) !void {
try server.sendResult(req.id.?, .{ .resources = &resource_list });
const id = req.id orelse return;
try server.sendResult(id, .{ .resources = &resource_list });
}
const ReadParams = struct {

View File

@@ -80,6 +80,7 @@ pub fn handleMessage(server: *Server, arena: std.mem.Allocator, msg: []const u8)
}
fn handleInitialize(server: *Server, req: protocol.Request) !void {
const id = req.id orelse return;
const result = protocol.InitializeResult{
.protocolVersion = "2025-11-25",
.capabilities = .{
@@ -92,7 +93,7 @@ fn handleInitialize(server: *Server, req: protocol.Request) !void {
},
};
try server.sendResult(req.id.?, result);
try server.sendResult(id, result);
}
fn handlePing(server: *Server, req: protocol.Request) !void {

View File

@@ -172,7 +172,8 @@ pub const tool_list = [_]protocol.Tool{
pub fn handleList(server: *Server, arena: std.mem.Allocator, req: protocol.Request) !void {
_ = arena;
try server.sendResult(req.id.?, .{ .tools = &tool_list });
const id = req.id orelse return;
try server.sendResult(id, .{ .tools = &tool_list });
}
const GotoParams = struct {