Skip to content

Commit 89fbf89

Browse files
committed
fix exception
1 parent 0e6375e commit 89fbf89

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

main/mcp_server.cc

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -309,27 +309,33 @@ void McpServer::DoToolCall(int id, const std::string& tool_name, const cJSON* to
309309
}
310310

311311
PropertyList arguments = (*tool_iter)->properties();
312-
for (auto& argument : arguments) {
313-
bool found = false;
314-
if (cJSON_IsObject(tool_arguments)) {
315-
auto value = cJSON_GetObjectItem(tool_arguments, argument.name().c_str());
316-
if (argument.type() == kPropertyTypeBoolean && cJSON_IsBool(value)) {
317-
argument.set_value<bool>(value->valueint == 1);
318-
found = true;
319-
} else if (argument.type() == kPropertyTypeInteger && cJSON_IsNumber(value)) {
320-
argument.set_value<int>(value->valueint);
321-
found = true;
322-
} else if (argument.type() == kPropertyTypeString && cJSON_IsString(value)) {
323-
argument.set_value<std::string>(value->valuestring);
324-
found = true;
312+
try {
313+
for (auto& argument : arguments) {
314+
bool found = false;
315+
if (cJSON_IsObject(tool_arguments)) {
316+
auto value = cJSON_GetObjectItem(tool_arguments, argument.name().c_str());
317+
if (argument.type() == kPropertyTypeBoolean && cJSON_IsBool(value)) {
318+
argument.set_value<bool>(value->valueint == 1);
319+
found = true;
320+
} else if (argument.type() == kPropertyTypeInteger && cJSON_IsNumber(value)) {
321+
argument.set_value<int>(value->valueint);
322+
found = true;
323+
} else if (argument.type() == kPropertyTypeString && cJSON_IsString(value)) {
324+
argument.set_value<std::string>(value->valuestring);
325+
found = true;
326+
}
325327
}
326-
}
327328

328-
if (!argument.has_default_value() && !found) {
329-
ESP_LOGE(TAG, "tools/call: Missing valid argument: %s", argument.name().c_str());
330-
ReplyError(id, "Missing valid argument: " + argument.name());
331-
return;
329+
if (!argument.has_default_value() && !found) {
330+
ESP_LOGE(TAG, "tools/call: Missing valid argument: %s", argument.name().c_str());
331+
ReplyError(id, "Missing valid argument: " + argument.name());
332+
return;
333+
}
332334
}
335+
} catch (const std::runtime_error& e) {
336+
ESP_LOGE(TAG, "tools/call: %s", e.what());
337+
ReplyError(id, e.what());
338+
return;
333339
}
334340

335341
Application::GetInstance().Schedule([this, id, tool_iter, arguments = std::move(arguments)]() {

0 commit comments

Comments
 (0)