Skip to content

Commit 7a23d06

Browse files
committed
Implement call routine
1 parent 67438a3 commit 7a23d06

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

toolcall/handler.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,35 @@ std::string toolcall::mcp_impl::tool_list() {
185185
return tools_;
186186
}
187187

188-
toolcall::action toolcall::mcp_impl::call(const std::string & /*request*/, std::string & /*response*/) {
188+
static mcp::tools_call_request tools_call_request_from_local_json(nlohmann::json id, const std::string & local_json) {
189+
nlohmann::json j = json::parse(local_json);
190+
mcp::tool_arg_list args;
191+
for (const auto & [key, val] : j["parameters"].items()) {
192+
args.push_back({key, val.dump()});
193+
}
194+
return mcp::tools_call_request(id, j["name"], args);
195+
}
196+
197+
static std::string tools_call_response_to_local_json(const mcp::tools_call_response & resp) {
198+
return resp.toJson().dump(-1); // The AI will figure it out?
199+
}
200+
201+
toolcall::action toolcall::mcp_impl::call(const std::string & request, std::string & response) {
202+
using on_response = toolcall::callback<mcp::tools_call_response>;
203+
189204
if (transport_ == nullptr) {
190205
return toolcall::DEFER;
191206
}
192-
// Construct tool call and send to transport
193-
return toolcall::ACCEPT; // TODO
207+
std::unique_lock<std::mutex> lock(tools_mutex_);
208+
209+
response.clear();
210+
on_response set_response = [this, &response] (const mcp::tools_call_response & resp) {
211+
std::unique_lock<std::mutex> lock(tools_mutex_);
212+
response = tools_call_response_to_local_json(resp);
213+
tools_populating_.notify_one();
214+
};
215+
transport_->send(tools_call_request_from_local_json(next_id_++, request), set_response);
216+
tools_populating_.wait_for(lock, std::chrono::seconds(15), [&response] { return ! response.empty(); });
217+
218+
return toolcall::ACCEPT;
194219
}

0 commit comments

Comments
 (0)