diff --git a/main/mcp_server.cc b/main/mcp_server.cc index 42e31c8a..b5a114af 100644 --- a/main/mcp_server.cc +++ b/main/mcp_server.cc @@ -309,27 +309,33 @@ void McpServer::DoToolCall(int id, const std::string& tool_name, const cJSON* to } PropertyList arguments = (*tool_iter)->properties(); - for (auto& argument : arguments) { - bool found = false; - if (cJSON_IsObject(tool_arguments)) { - auto value = cJSON_GetObjectItem(tool_arguments, argument.name().c_str()); - if (argument.type() == kPropertyTypeBoolean && cJSON_IsBool(value)) { - argument.set_value(value->valueint == 1); - found = true; - } else if (argument.type() == kPropertyTypeInteger && cJSON_IsNumber(value)) { - argument.set_value(value->valueint); - found = true; - } else if (argument.type() == kPropertyTypeString && cJSON_IsString(value)) { - argument.set_value(value->valuestring); - found = true; + try { + for (auto& argument : arguments) { + bool found = false; + if (cJSON_IsObject(tool_arguments)) { + auto value = cJSON_GetObjectItem(tool_arguments, argument.name().c_str()); + if (argument.type() == kPropertyTypeBoolean && cJSON_IsBool(value)) { + argument.set_value(value->valueint == 1); + found = true; + } else if (argument.type() == kPropertyTypeInteger && cJSON_IsNumber(value)) { + argument.set_value(value->valueint); + found = true; + } else if (argument.type() == kPropertyTypeString && cJSON_IsString(value)) { + argument.set_value(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; } } - - 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) { + ESP_LOGE(TAG, "tools/call: %s", e.what()); + ReplyError(id, e.what()); + return; } Application::GetInstance().Schedule([this, id, tool_iter, arguments = std::move(arguments)]() {