MCPcopy Create free account
hub / github.com/ClickHouse/ai-sdk-cpp / query_database_async

Function query_database_async

examples/tool_calling_async.cpp:64–92  ·  view source on GitHub ↗

Async tool: Simulate database query

Source from the content-addressed store, hash-verified

62
63// Async tool: Simulate database query
64std::future<ai::JsonValue> query_database_async(
65 const ai::JsonValue& args,
66 const ai::ToolExecutionContext& context) {
67 std::string query = args["query"].get<std::string>();
68
69 return std::async(std::launch::async, [query]() {
70 std::cout << "🗄️ Executing database query: " << query << " (async)..."
71 << std::endl;
72
73 // Simulate database processing time
74 std::this_thread::sleep_for(std::chrono::milliseconds(800));
75
76 ai::JsonValue results = ai::JsonValue::array();
77 if (query.find("users") != std::string::npos) {
78 results = {{{"id", 1}, {"name", "John"}, {"active", true}},
79 {{"id", 2}, {"name", "Jane"}, {"active", true}},
80 {{"id", 3}, {"name", "Bob"}, {"active", false}}};
81 } else if (query.find("orders") != std::string::npos) {
82 results = {
83 {{"order_id", 100}, {"amount", 99.99}, {"status", "shipped"}},
84 {{"order_id", 101}, {"amount", 149.99}, {"status", "pending"}}};
85 }
86
87 std::cout << "🗄️ Database query completed: " << query << std::endl;
88
89 return ai::JsonValue{
90 {"query", query}, {"results", results}, {"execution_time_ms", 800}};
91 });
92}
93
94// Sync tool for comparison: Quick calculation
95ai::JsonValue calculate_stats(const ai::JsonValue& args,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected