Lightpanda: the headless browser designed for AI and automation
tools: name the missing required argument instead of bare InvalidParams
coolswoodaad24c1e8556
diff --git a/src/browser/tools.zig b/src/browser/tools.zigindex f909f8659..6c3cfd775 100644--- a/src/browser/tools.zig+++ b/src/browser/tools.zig@@ -1039,6 +1039,9 @@ fn callInner(error.OutOfMemory => return error.OutOfMemory,error.DuplicateField => return error.InvalidParams,};+ // After normalizeArgKeys so a PascalCase `Script` isn't reported missing.+ if (diagnoseMissingRequired(arena, tool, normalized)) |msg|+ return .{ .text = msg, .is_error = true };const substituted = try substituteStringArgs(arena, tool, normalized);// Before dispatch, because a navigation takes the node with it. Gated on@@ -2767,6 +2770,33 @@ fn resolveBySelector(session: *lp.Session, selector: []const u8) ToolError!NodeAconst ParseArgsError = error{ OutOfMemory, InvalidParams };+/// Names the first required argument absent from the call, so a bare+/// `InvalidParams` from `parseValue` doesn't hide which field to add — a+/// caller sending `{"expression": ...}` to evaluate learns about `script`.+/// Default-true booleans (setChecked.checked) parse fine when omitted, so+/// they don't count as missing. Runs on key-normalized arguments.+fn diagnoseMissingRequired(arena: std.mem.Allocator, tool: Tool, arguments: ?std.json.Value) ?[]const u8 {+ const schemas = lp.Schema.all();+ const tool_idx = @backingInt(tool);+ if (tool_idx >= schemas.len) return null;+ const schema = schemas[tool_idx];++ const obj = if (arguments) |v| switch (v) {+ .object => |o| o,+ // A non-object shape fails in parseValue with the whole payload as+ // context; there is no key to name here.+ else => return null,+ } else std.json.ObjectMap.empty;+ for (schema.required) |name| {+ if (obj.get(name) != null) continue;+ if (schema.findField(name)) |f| {+ if (f.default_true) continue;+ }+ return arena.print("missing required argument '{s}'", .{name}) catch null;+ }+ return null;+}+/// Surface field/value context for known typed args — `std.json`'s parse/// errors only carry the tag (`InvalidEnumTag`, …), not which field failed.fn diagnoseArgs(arena: std.mem.Allocator, arguments: ?std.json.Value) ?[]const u8 {diff --git a/src/mcp/tools.zig b/src/mcp/tools.zigindex 54bf0bf67..633277dc2 100644--- a/src/mcp/tools.zig+++ b/src/mcp/tools.zig@@ -485,6 +485,58 @@ test "MCP - evaluate error reporting" {} }, out.written());}+test "MCP - missing required argument is named in the error" {+ var out: std.Io.Writer.Allocating = .init(testing.arena_allocator);+ const server = try testLoadPage("http://localhost:9582/src/browser/tests/mcp_actions.html", &out.writer);+ defer server.deinit();++ // A caller guessing the wrong argument name used to get a bare+ // InvalidParams; the diagnosis names the field to add.+ try router.handleMessage(server, testing.arena_allocator,+ \\{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"evaluate","arguments":{"expression":"1+1"}}}+ );+ try testing.expectJson(.{ .id = 1, .result = .{+ .isError = true,+ .content = &.{.{ .text = "missing required argument 'script'" }},+ } }, out.written());+ out.clearRetainingCapacity();++ // No arguments at all: same diagnosis.+ try router.handleMessage(server, testing.arena_allocator,+ \\{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"evaluate"}}+ );+ try testing.expectJson(.{ .id = 2, .result = .{+ .isError = true,+ .content = &.{.{ .text = "missing required argument 'script'" }},+ } }, out.written());+ out.clearRetainingCapacity();++ try router.handleMessage(server, testing.arena_allocator,+ \\{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"press","arguments":{"selector":"#chk"}}}+ );+ try testing.expectJson(.{ .id = 3, .result = .{+ .isError = true,+ .content = &.{.{ .text = "missing required argument 'key'" }},+ } }, out.written());+ out.clearRetainingCapacity();++ // setChecked.checked is default-true: omitted is not "missing" — the call+ // runs and checks the box.+ try router.handleMessage(server, testing.arena_allocator,+ \\{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"setChecked","arguments":{"selector":"#chk"}}}+ );+ try testing.expect(std.mem.indexOf(u8, out.written(), "missing required argument") == null);+ try testing.expect(std.mem.indexOf(u8, out.written(), "\"error\"") == null);+ out.clearRetainingCapacity();++ // PascalCase keys normalize before the check, so `Url` is not "missing".+ try router.handleMessage(server, testing.arena_allocator,+ \\{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"goto","arguments":{"Url":"about:blank"}}}+ );+ try testing.expect(std.mem.indexOf(u8, out.written(), "missing required argument") == null);+ try testing.expect(std.mem.indexOf(u8, out.written(), "\"error\"") == null);+}+test "MCP - evaluate: top-level return runs in an async wrapper" {var out: std.Io.Writer.Allocating = .init(testing.arena_allocator);const server = try testLoadPage("about:blank", &out.writer);