fix exception

This commit is contained in:
Terrence
2025-05-29 13:46:01 +08:00
parent 0e6375e347
commit 89fbf89d0f

View File

@@ -309,27 +309,33 @@ void McpServer::DoToolCall(int id, const std::string& tool_name, const cJSON* to
} }
PropertyList arguments = (*tool_iter)->properties(); PropertyList arguments = (*tool_iter)->properties();
for (auto& argument : arguments) { try {
bool found = false; for (auto& argument : arguments) {
if (cJSON_IsObject(tool_arguments)) { bool found = false;
auto value = cJSON_GetObjectItem(tool_arguments, argument.name().c_str()); if (cJSON_IsObject(tool_arguments)) {
if (argument.type() == kPropertyTypeBoolean && cJSON_IsBool(value)) { auto value = cJSON_GetObjectItem(tool_arguments, argument.name().c_str());
argument.set_value<bool>(value->valueint == 1); if (argument.type() == kPropertyTypeBoolean && cJSON_IsBool(value)) {
found = true; argument.set_value<bool>(value->valueint == 1);
} else if (argument.type() == kPropertyTypeInteger && cJSON_IsNumber(value)) { found = true;
argument.set_value<int>(value->valueint); } else if (argument.type() == kPropertyTypeInteger && cJSON_IsNumber(value)) {
found = true; argument.set_value<int>(value->valueint);
} else if (argument.type() == kPropertyTypeString && cJSON_IsString(value)) { found = true;
argument.set_value<std::string>(value->valuestring); } else if (argument.type() == kPropertyTypeString && cJSON_IsString(value)) {
found = true; argument.set_value<std::string>(value->valuestring);
found = true;
}
}
if (!argument.has_default_value() && !found) {
ESP_LOGE(TAG, "tools/call: Missing valid argument: %s", argument.name().c_str());
ReplyError(id, "Missing valid argument: " + argument.name());
return;
} }
} }
} catch (const std::runtime_error& e) {
if (!argument.has_default_value() && !found) { ESP_LOGE(TAG, "tools/call: %s", e.what());
ESP_LOGE(TAG, "tools/call: Missing valid argument: %s", argument.name().c_str()); ReplyError(id, e.what());
ReplyError(id, "Missing valid argument: " + argument.name()); return;
return;
}
} }
Application::GetInstance().Schedule([this, id, tool_iter, arguments = std::move(arguments)]() { Application::GetInstance().Schedule([this, id, tool_iter, arguments = std::move(arguments)]() {