Skip to content

Commit 40156ff

Browse files
committed
Add tools_list_response::fromJson
1 parent 7d6c29b commit 40156ff

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

toolcall/handler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ void toolcall::mcp_impl::initialize() {
9595

9696
bool caps_received = false;
9797
mcp::capabilities caps;
98-
on_response set_caps = [this, &caps] (const mcp::initialize_response & resp) {
98+
on_response set_caps = [this, &caps, &caps_received] (const mcp::initialize_response & resp) {
9999
std::unique_lock<std::mutex> lock(tools_mutex_);
100100
caps = resp.capabilities();
101+
caps_received = true;
101102
tools_populating_.notify_one();
102103
};
103104

toolcall/mcp_messages.cpp

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "mcp_messages.h"
22
#include <iostream>
3+
#include <log.h>
34

45
using json = nlohmann::json;
56

@@ -106,20 +107,19 @@ void mcp::initialize_response::refreshResult() {
106107
result["protocolVersion"] = protoVersion();
107108
result["serverInfo"]["name"] = name();
108109
result["serverInfo"]["version"] = version();
109-
result["capabilities"] = {};
110110

111+
json capabilities = json::object();
111112
for (auto cap = caps_.cbegin(); cap != caps_.cend(); ++cap) {
112113
json cap_json;
113-
114114
if (cap->subscribe) {
115115
cap_json["subscribe"] = true;
116116
}
117117
if (cap->listChanged) {
118118
cap_json["listChanged"] = true;
119119
}
120-
121-
result["capabilities"][cap->name] = cap_json;
120+
capabilities[cap->name] = cap_json;
122121
}
122+
result["capabilities"] = capabilities;
123123

124124
this->result(std::move(result));
125125
}
@@ -256,8 +256,36 @@ void mcp::tools_list_response::refreshResult() {
256256
this->result(result);
257257
}
258258

259+
mcp::tools_list_response mcp::tools_list_response::fromJson(const nlohmann::json & j) {
260+
mcp::tools_list tools;
261+
for (const auto & t : j["result"]["tools"]) {
262+
mcp::tool tool;
263+
tool.tool_name = t["name"];
264+
tool.tool_description = t["description"];
265+
for (const auto & [key, value] : t["inputSchema"]["properties"].items()) {
266+
mcp::tool::param param;
267+
param.name = key;
268+
param.type = value["type"];
269+
param.description = value["description"];
270+
tool.params.push_back(param);
271+
}
272+
if (t["inputSchema"].contains("required") && t["inputSchema"]["required"].is_array()) {
273+
for (const auto & required : t["inputSchema"]["required"]) {
274+
tool.required_params.push_back(required);
275+
}
276+
}
277+
tools.push_back(std::move(tool));
278+
}
279+
std::string next_cursor = j["result"].value("nextCursor", "");
280+
return tools_list_response(j["id"], std::move(tools), next_cursor);
281+
}
282+
259283
static bool has_initialized_response(const nlohmann::json & data) {
260-
return data["result"].contains("serverInfo");
284+
return data["result"].contains("capabilities");
285+
}
286+
287+
static bool has_tools_list_response(const nlohmann::json & data) {
288+
return data["result"].contains("tools");
261289
}
262290

263291
bool mcp::create_message(const std::string & data, mcp::message_variant & message) {
@@ -266,7 +294,11 @@ bool mcp::create_message(const std::string & data, mcp::message_variant & messag
266294
if (has_initialized_response(j)) {
267295
message = mcp::initialize_response::fromJson(j);
268296

297+
} else if (has_tools_list_response(j)) {
298+
message = mcp::tools_list_response::fromJson(j);
299+
269300
} else {
301+
message = std::monostate();
270302
return false;
271303
}
272304
return true;

toolcall/mcp_messages.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ namespace mcp
208208
void next_cursor(std::string next_cursor);
209209
const std::string & next_cursor() const { return next_cursor_; }
210210

211+
static tools_list_response fromJson(const nlohmann::json & j);
212+
211213
private:
212214
void refreshResult();
213215
tools_list tools_;

0 commit comments

Comments
 (0)