Skip to content

Commit 67438a3

Browse files
committed
Add tools_call_response fromJson
1 parent 606993d commit 67438a3

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

toolcall/handler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ void toolcall::mcp_impl::initialize() {
107107

108108
on_list_changed update_dirty = [&update_dirty, this] (const mcp::tools_list_changed_notification &) {
109109
tool_list_dirty_ = true;
110-
transport_->subscribe("notifications/tools/list_changed", update_dirty);
110+
transport_->subscribe(update_dirty);
111111
};
112112

113113
bool has_tools = false;
114114
for (const auto & cap : caps) {
115115
if (cap.name == "tools") {
116116
has_tools = true;
117117
if (cap.listChanged) {
118-
transport_->subscribe("notifications/tools/list_changed", update_dirty);
118+
transport_->subscribe(update_dirty);
119119
}
120120
break;
121121
}

toolcall/mcp_messages.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ mcp::initialize_response mcp::initialize_response::fromJson(const nlohmann::json
181181
}
182182

183183
mcp::tools_list_request::tools_list_request(std::optional<nlohmann::json> id, std::string cursor)
184-
: request(id, "tools/list"),
184+
: request(id, Method),
185185
cursor_(std::move(cursor))
186186
{
187187
refreshParams();
@@ -282,15 +282,14 @@ mcp::tools_list_response mcp::tools_list_response::fromJson(const nlohmann::json
282282
}
283283

284284
mcp::tools_list_changed_notification mcp::tools_list_changed_notification::fromJson(const nlohmann::json & j) {
285-
if (! (j.is_object() && j.contains("method") &&
286-
j["method"] == "notifications/tools/list_changed")) {
285+
if (! (j.is_object() && j.contains("method") && j["method"] == Method)) {
287286
throw std::invalid_argument("Invalid tools_list_changed message");
288287
}
289288
return tools_list_changed_notification();
290289
}
291290

292291
mcp::tools_call_request::tools_call_request(nlohmann::json id, std::string name, tool_arg_list args)
293-
: request(id, "tools/call"), name_(std::move(name)), args_(std::move(args))
292+
: request(id, Method), name_(std::move(name)), args_(std::move(args))
294293
{
295294
refreshParams();
296295
}
@@ -364,3 +363,30 @@ void mcp::tools_call_response::refreshResult() {
364363
result["content"] = content;
365364
this->result(std::move(result));
366365
}
366+
367+
mcp::tools_call_response mcp::tools_call_response::fromJson(const nlohmann::json & j) {
368+
mcp::tool_result_list result_list;
369+
for (const auto & content : j["result"]["content"]) {
370+
mcp::tool_result result;
371+
372+
result.type = content["type"];
373+
if (content["type"] == "text") {
374+
result.value = content["text"];
375+
376+
} else if (content["type"] == "image" || content["type"] == "audio") {
377+
result.value = content["data"];
378+
result.mime_type = content["mimeType"];
379+
380+
} else if (content["type"] == "resource") {
381+
result.value = content["resource"]["text"];
382+
result.mime_type = content["resource"]["mimeType"];
383+
result.uri = content["resource"]["uri"];
384+
}
385+
386+
result_list.push_back(std::move(result));
387+
}
388+
389+
bool error = j["result"].value("isError", false);
390+
391+
return mcp::tools_call_response(j["id"], std::move(result_list), error);
392+
}

toolcall/mcp_messages.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,15 @@ namespace mcp
166166

167167
class initialized_notification : public notification {
168168
public:
169-
initialized_notification()
170-
: notification("notifications/initialized") {}
169+
static inline const std::string Method = "notifications/initialized";
170+
171+
initialized_notification() : notification(Method) {}
171172
};
172173

173174
class tools_list_request : public request {
174175
public:
176+
static inline const std::string Method = "tools/list";
177+
175178
tools_list_request(std::optional<nlohmann::json> id, std::string cursor = "");
176179

177180
void cursor(std::string cursor);
@@ -218,8 +221,9 @@ namespace mcp
218221

219222
class tools_list_changed_notification : public notification {
220223
public:
221-
tools_list_changed_notification()
222-
: notification("notifications/tools/list_changed") {}
224+
static inline const std::string Method = "notifications/tools/list_changed";
225+
226+
tools_list_changed_notification() : notification(Method) {}
223227

224228
static tools_list_changed_notification fromJson(const nlohmann::json & j);
225229
};
@@ -233,6 +237,8 @@ namespace mcp
233237

234238
class tools_call_request : public request {
235239
public:
240+
static inline const std::string Method = "tools/call";
241+
236242
tools_call_request(nlohmann::json id, std::string name, tool_arg_list args = tool_arg_list());
237243

238244
void name(std::string name);
@@ -266,6 +272,8 @@ namespace mcp
266272
void tool_error(bool error);
267273
bool tool_error() const { return error_; }
268274

275+
static tools_call_response fromJson(const nlohmann::json & j);
276+
269277
private:
270278
void refreshResult();
271279
tool_result_list tool_result_;

toolcall/mcp_transport.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ namespace toolcall
2222
map.insert({key, callback});
2323
}
2424

25+
template <typename T>
26+
void subscribe(callback<T> callback) {
27+
auto& map =
28+
std::get<std::map<std::string, toolcall::callback<T>>>(
29+
subscribers_);
30+
31+
map.insert({T::Method, callback});
32+
}
33+
2534
template <typename T>
2635
void unsubscribe(std::string key) {
2736
auto& map =
@@ -62,7 +71,8 @@ namespace toolcall
6271

6372
class mcp_transport : public mcp_message_observer<mcp::initialize_response,
6473
mcp::tools_list_response,
65-
mcp::tools_list_changed_notification> {
74+
mcp::tools_list_changed_notification,
75+
mcp::tools_call_response> {
6676
public:
6777
virtual ~mcp_transport() = default;
6878

0 commit comments

Comments
 (0)