mcp: support notifications and improve error handling

Make Request id optional for JSON-RPC notifications and handle the
initialized event. Improve thread safety, logging, and error paths.
This commit is contained in:
Adrià Arrufat
2026-02-25 23:14:06 +09:00
parent 9b3fa809bf
commit 34d2fc1503
6 changed files with 76 additions and 50 deletions

View File

@@ -31,6 +31,14 @@ fn handleMessage(server: *McpServer, arena: std.mem.Allocator, msg: []const u8)
return;
};
if (parsed.id == null) {
// It's a notification
if (std.mem.eql(u8, parsed.method, "notifications/initialized")) {
log.info(.app, "MCP Client Initialized", .{});
}
return;
}
if (std.mem.eql(u8, parsed.method, "initialize")) {
try handleInitialize(server, parsed);
} else if (std.mem.eql(u8, parsed.method, "resources/list")) {
@@ -38,12 +46,12 @@ fn handleMessage(server: *McpServer, arena: std.mem.Allocator, msg: []const u8)
} else if (std.mem.eql(u8, parsed.method, "resources/read")) {
try resources.handleRead(server, arena, parsed);
} else if (std.mem.eql(u8, parsed.method, "tools/list")) {
try tools.handleList(server, parsed);
try tools.handleList(server, arena, parsed);
} else if (std.mem.eql(u8, parsed.method, "tools/call")) {
try tools.handleCall(server, arena, parsed);
} else {
try server.sendResponse(protocol.Response{
.id = parsed.id,
.id = parsed.id.?,
.@"error" = protocol.Error{
.code = -32601,
.message = "Method not found",
@@ -78,5 +86,5 @@ fn handleInitialize(server: *McpServer, req: protocol.Request) !void {
},
};
try sendResponseGeneric(server, req.id, result);
try sendResponseGeneric(server, req.id.?, result);
}