diff --git a/main/boards/common/esp32_camera.cc b/main/boards/common/esp32_camera.cc index 277c16bc..380414cd 100644 --- a/main/boards/common/esp32_camera.cc +++ b/main/boards/common/esp32_camera.cc @@ -295,6 +295,9 @@ std::string Esp32Camera::Explain(const std::string& question) { std::string result = http->ReadAll(); http->Close(); - ESP_LOGI(TAG, "Explain image size=%dx%d, compressed size=%d, question=%s\n%s", fb_->width, fb_->height, total_sent, question.c_str(), result.c_str()); + // Get remain task stack size + size_t remain_stack_size = uxTaskGetStackHighWaterMark(nullptr); + ESP_LOGI(TAG, "Explain image size=%dx%d, compressed size=%d, remain stack size=%d, question=%s\n%s", + fb_->width, fb_->height, total_sent, remain_stack_size, question.c_str(), result.c_str()); return result; } diff --git a/main/mcp_server.cc b/main/mcp_server.cc index 47945cba..dba41b40 100644 --- a/main/mcp_server.cc +++ b/main/mcp_server.cc @@ -16,6 +16,8 @@ #define TAG "MCP" +#define DEFAULT_TOOLCALL_STACK_SIZE 6144 + McpServer::McpServer() { } @@ -222,7 +224,13 @@ void McpServer::ParseMessage(const cJSON* json) { ReplyError(id_int, "Invalid arguments"); return; } - DoToolCall(id_int, std::string(tool_name->valuestring), tool_arguments); + auto stack_size = cJSON_GetObjectItem(params, "stackSize"); + if (stack_size != nullptr && !cJSON_IsNumber(stack_size)) { + ESP_LOGE(TAG, "tools/call: Invalid stackSize"); + ReplyError(id_int, "Invalid stackSize"); + return; + } + DoToolCall(id_int, std::string(tool_name->valuestring), tool_arguments, stack_size ? stack_size->valueint : DEFAULT_TOOLCALL_STACK_SIZE); } else { ESP_LOGE(TAG, "Method not implemented: %s", method_str.c_str()); ReplyError(id_int, "Method not implemented: " + method_str); @@ -297,7 +305,7 @@ void McpServer::GetToolsList(int id, const std::string& cursor) { ReplyResult(id, json); } -void McpServer::DoToolCall(int id, const std::string& tool_name, const cJSON* tool_arguments) { +void McpServer::DoToolCall(int id, const std::string& tool_name, const cJSON* tool_arguments, int stack_size) { auto tool_iter = std::find_if(tools_.begin(), tools_.end(), [&tool_name](const McpTool* tool) { return tool->name() == tool_name; @@ -339,10 +347,10 @@ void McpServer::DoToolCall(int id, const std::string& tool_name, const cJSON* to return; } - // Start a task to receive data with stack size 4096 + // Start a task to receive data with stack size esp_pthread_cfg_t cfg = esp_pthread_get_default_config(); cfg.thread_name = "tool_call"; - cfg.stack_size = 4096; + cfg.stack_size = stack_size; cfg.prio = 1; esp_pthread_set_cfg(&cfg); diff --git a/main/mcp_server.h b/main/mcp_server.h index a1564ae3..27ace329 100644 --- a/main/mcp_server.h +++ b/main/mcp_server.h @@ -272,7 +272,7 @@ private: void ReplyError(int id, const std::string& message); void GetToolsList(int id, const std::string& cursor); - void DoToolCall(int id, const std::string& tool_name, const cJSON* tool_arguments); + void DoToolCall(int id, const std::string& tool_name, const cJSON* tool_arguments, int stack_size); std::vector tools_; std::thread tool_call_thread_;