humanus.cpp/tool/python_execute.h

57 lines
1.8 KiB
C++

#ifndef HUMANUS_TOOL_PYTHON_EXECUTE_H
#define HUMANUS_TOOL_PYTHON_EXECUTE_H
#include "base.h"
#include "mcp/include/mcp_client.h"
namespace humanus {
struct PythonExecute : BaseTool {
inline static const std::string name_ = "python_execute";
inline static const std::string description_ = "Executes Python code string. Note: Only print outputs are visible, function return values are not captured. Use print statements to see results.";
inline static const json parameters_ = {
{"type", "object"},
{"properties", {
{"code", {
{"type", "string"},
{"description", "The Python code to execute."}
}},
{"timeout", {
{"type", "number"},
{"description", "The timeout for the Python code execution in seconds."},
{"default", 5}
}}
}},
{"required", {"code"}}
};
PythonExecute() : BaseTool(name_, description_, parameters_) {}
ToolResult execute(const json& arguments) override {
try {
// 创建MCP客户端
mcp::client client("localhost", 8088);
// 初始化客户端
client.initialize("OpenManusCppClient", "0.1.0");
client.set_timeout(arguments["timeout"].get<double>());
// 调用工具
json tool_params = {{"code", arguments["code"]}};
json result = client.call_tool("python_execute", tool_params);
if (result["isError"]) {
return ToolError(result["error"].get<std::string>());
}
return ToolResult(result["content"].get<std::string>());
} catch (const std::exception& e) {
return ToolError(e.what());
}
}
};
}
#endif // HUMANUS_TOOL_PYTHON_EXECUTE_H