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

Function fetch_news_async

examples/tool_calling_async.cpp:29–61  ·  view source on GitHub ↗

Async tool: Simulate web API call

Source from the content-addressed store, hash-verified

27
28// Async tool: Simulate web API call
29std::future<ai::JsonValue> fetch_news_async(
30 const ai::JsonValue& args,
31 const ai::ToolExecutionContext& context) {
32 std::string category = args["category"].get<std::string>();
33
34 return std::async(std::launch::async, [category]() {
35 std::cout << "📰 Fetching news for category: " << category << " (async)..."
36 << std::endl;
37
38 // Simulate network delay
39 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
40
41 ai::JsonValue news_articles = ai::JsonValue::array();
42 if (category == "tech") {
43 news_articles = {"AI breakthrough in quantum computing",
44 "New smartphone features announced",
45 "Cloud computing trends for 2024"};
46 } else if (category == "sports") {
47 news_articles = {"Championship game results",
48 "Olympic preparations underway",
49 "New stadium construction begins"};
50 } else {
51 news_articles = {"General news update 1", "General news update 2",
52 "General news update 3"};
53 }
54
55 std::cout << "📰 News fetch completed for: " << category << std::endl;
56
57 return ai::JsonValue{{"category", category},
58 {"articles", news_articles},
59 {"fetch_time_ms", 1000}};
60 });
61}
62
63// Async tool: Simulate database query
64std::future<ai::JsonValue> query_database_async(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected