diff --git a/docs/HLSD/mcp-server.md b/docs/HLSD/mcp-server.md index f6971cf1f5..a2903bae1d 100644 --- a/docs/HLSD/mcp-server.md +++ b/docs/HLSD/mcp-server.md @@ -4591,7 +4591,9 @@ call, access, description, parameters, rules, menu_items, examples, complete, in to call it, by its name or through `tool_call`, and `access` holds the four labels `tools/list` sends only for listed commands. Each parameter is `{name, type, required, default?, values?, min?, max?, exclusive_min?, min_items?, max_items?, min_length?, max_length?, pattern?, format?, goes_with?, values_from?, items?, -properties?, other_keys?, schema?, description}`, the same shape at every depth. `required` is true, false, or +properties?, other_keys?, schema?, description}`, the same shape at every depth. `other_keys`, on an object, +is false when it takes nothing besides its properties, true when it takes anything, or the shape of the values +it takes. `required` is true, false, or the condition in words where a schema rule decides it; `default` is the schema's alone, a default a handler applies staying in the description's words; `goes_with` names the `type` or `target` the parameter belongs to where its description starts "Type X:" or "Target X:". `rules` gives the schema's conditional parts as @@ -4604,14 +4606,16 @@ as a plugin's schema may hold) stays raw under the parameter's `schema`, and the `values_from` names, for a value the schema cannot list, the tools that list it: `object_list` for an object index, `settings_list` for a setting key, `tools/list` and `tool_list` for a command's name, `tool_describe` for -another command's arguments. It comes from one table (`parameter_class()`) that classifies every parameter name -the built-in commands use, at every depth, apart from enums and booleans: a source, with the tools that list its -values; described, its description listing them; or free, the caller's own value, such as a number, a path or a -new name. A row keyed by command and path comes before the name's row. A parameter with fields of its own is -read through them, and names a source besides only where a row keyed by its command and path gives one -(`printer_install`'s `models`, `dialog_answer`'s `dialog`), since a name such as `plane` is a reference in one -command and a cut plane's fields in another. A unit test fails for a built-in parameter the table does not classify, so a new command's parameters are classified when it is added; a plugin -operation's are not, their names being the plugin's own. Every refusal of a call's arguments by the schema check +another command's arguments. It comes from one table (`parameter_class()`) that classifies every name the +built-in commands give a parameter without fields of its own, at every depth, apart from enums and booleans: a +source, with the tools that list its values; described, its description listing them; or free, the caller's own +value, such as a number, a path or a new name. A row keyed by command and path comes before the name's row. A +parameter with fields of its own is read through them, and names a source besides only where a row keyed by its +command and path gives one (`printer_install`'s `models`, `dialog_answer`'s `dialog`), since a name such as +`plane` is a reference in one command and a cut plane's fields in another. A unit test fails for a built-in +parameter without fields that the table does not classify, and for one with fields that gains or loses a source, +so a new command's parameters are classified when it is added; a plugin operation's are not, their names being +the plugin's own. Every refusal of a call's arguments by the schema check names `tool_describe` for the command (`argument_refusal()`), by each route that checks: `tools/call`, `tool_call`'s target, a child call, a plugin's call and `action_run`. diff --git a/src/slic3r/GUI/Mcp/McpToolsApi.cpp b/src/slic3r/GUI/Mcp/McpToolsApi.cpp index bf4f6ac529..2c39b931e2 100644 --- a/src/slic3r/GUI/Mcp/McpToolsApi.cpp +++ b/src/slic3r/GUI/Mcp/McpToolsApi.cpp @@ -353,6 +353,15 @@ bool requires_only_its_fields(const json& schema) }); } +// Whether `schema` describes an object, whose other keys the manual then states: its type says so, or it has +// properties. +bool takes_an_object(const json& schema) +{ + const json type = schema.value("type", json()); + return schema.contains("properties") || type == "object" || + (type.is_array() && std::find(type.begin(), type.end(), "object") != type.end()); +} + // Whether `schema` has fields of its own: an object's properties, or its items'. bool has_fields(const json& schema) { @@ -448,13 +457,11 @@ struct ManualBuilder } if (!requires_only_its_fields(schema)) raw = true; - if (const auto other = schema.find("additionalProperties"); other != schema.end()) { - if (other->is_boolean()) { - if (!other->get()) - out["other_keys"] = false; - } else - out["other_keys"] = shape(*other, path, nullptr, false); - } + // What an object takes besides its properties: nothing (false), anything (true), or values of a shape. + if (const auto other = schema.find("additionalProperties"); other != schema.end()) + out["other_keys"] = other->is_boolean() ? json(other->get()) : shape(*other, path, nullptr, false); + else if (takes_an_object(schema)) + out["other_keys"] = true; if (const auto items = schema.find("items"); items != schema.end()) out["items"] = shape(*items, path + "[]", nullptr, classified); // A parameter names where its values come from, unless it lists them itself. One with fields is read through @@ -582,7 +589,8 @@ json command_manual(const NativeCommand& command) if (p.value("required", false) == false) if (const auto c = conditional.find(p["name"].get()); c != conditional.end()) p["required"] = c->second; - } + } else if (props != schema.end()) + builder.complete = false; // A call's other arguments are refused only when the schema closes its top level (validate_value()). if (const auto other = schema.find("additionalProperties"); other == schema.end() || *other == true) rules.push_back("Other arguments are accepted."); diff --git a/tests/slic3rutils/test_mcp_tools.cpp b/tests/slic3rutils/test_mcp_tools.cpp index 85ec600a0d..fd28bd622e 100644 --- a/tests/slic3rutils/test_mcp_tools.cpp +++ b/tests/slic3rutils/test_mcp_tools.cpp @@ -919,6 +919,10 @@ void check_shape(const json& param, const json& schema, const std::string& where CHECK(param.at("type") == type_words(schema["type"])); else CHECK_FALSE(param.contains("type")); + if (schema.contains("description")) + CHECK(param.at("description") == schema["description"]); + else + CHECK_FALSE(param.contains("description")); static const std::pair kBounds[] = { {"minimum", "min"}, {"maximum", "max"}, {"exclusiveMinimum", "exclusive_min"}, {"minItems", "min_items"}, {"maxItems", "max_items"}, {"minLength", "min_length"}, @@ -940,12 +944,18 @@ void check_shape(const json& param, const json& schema, const std::string& where CHECK_FALSE(param.contains("default")); if (schema.contains("properties")) check_parameters(param.at("properties"), schema["properties"], schema.value("required", json::array()), where + "."); - if (schema.contains("additionalProperties")) { - if (schema["additionalProperties"] == false) - CHECK(param.at("other_keys") == false); - else if (schema["additionalProperties"].is_object()) - CHECK(param.at("other_keys").is_object()); - } + // An object states its other keys: none, any, or values of a shape. + const json type = schema.value("type", json()); + const bool object = schema.contains("properties") || type == "object" || + (type.is_array() && std::find(type.begin(), type.end(), "object") != type.end()); + if (schema.contains("additionalProperties") && schema["additionalProperties"].is_boolean()) + CHECK(param.at("other_keys") == schema["additionalProperties"]); + else if (schema.contains("additionalProperties")) + CHECK(param.at("other_keys").is_object()); + else if (object) + CHECK(param.at("other_keys") == true); + else + CHECK_FALSE(param.contains("other_keys")); if (schema.contains("items")) check_shape(param.at("items"), schema["items"], where + "[]"); } @@ -1076,7 +1086,7 @@ TEST_CASE("tool_describe's manual names a command's menu items with their argume CHECK(manual_of(catalog, "fillet").at("call") == "tool_call {tool: \"fillet\", args}"); } -TEST_CASE("every parameter of a built-in command is classified for the manual", "[Mcp][CAD]") +TEST_CASE("every built-in parameter is classified for the manual, or read through its fields", "[Mcp][CAD]") { const CommandCatalog catalog = catalog_with_cad(); // The parameters with fields of their own that name a source besides their fields, and the source. @@ -1222,6 +1232,18 @@ TEST_CASE("tool_describe keeps raw what it can't express, and says the manual is CHECK(inner_needs.at("complete") == false); CHECK(inner_needs.at("parameters")[0].at("schema") == inner); + // A properties that is not an object lists no parameters. + const json no_props = json::parse(R"({"type": "object", "properties": true})"); + const json unlisted = manual(no_props); + CHECK(unlisted.at("complete") == false); + CHECK(unlisted.at("input_schema") == no_props); + + // A nested object open to other keys says so, as an open top level does. + const json nested = manual(json::parse(R"({"type": "object", "properties": {"opts": {"type": "object", + "properties": {"x": {"type": "number"}}}}, "additionalProperties": false})")); + CHECK(nested.at("complete") == true); + CHECK(nested.at("parameters")[0].at("other_keys") == true); + // An open top level takes arguments no parameter names; a closed one says it refuses them. const json open = manual(json::parse(R"({"type": "object", "additionalProperties": true})")); CHECK(open.at("complete") == true);