@@ -280,6 +280,82 @@ mcp::tools_list_response mcp::tools_list_response::fromJson(const nlohmann::json
280280 return tools_list_response (j[" id" ], std::move (tools), next_cursor);
281281}
282282
283+ mcp::tools_call_request::tools_call_request (nlohmann::json id, std::string name, tool_arg_list args)
284+ : request(id, " tools/call" ), name_(std::move(name)), args_(std::move(args))
285+ {
286+ refreshParams ();
287+ }
288+
289+ void mcp::tools_call_request::name (std::string name) {
290+ name_ = std::move (name);
291+ refreshParams ();
292+ }
293+
294+ void mcp::tools_call_request::args (mcp::tool_arg_list args) {
295+ args_ = std::move (args);
296+ refreshParams ();
297+ }
298+
299+ void mcp::tools_call_request::refreshParams () {
300+ json params = json::object ();
301+ params[" name" ] = name_;
302+ if (! args_.empty ()) {
303+ json args = json::object ();
304+ for (const auto & arg : args_) {
305+ args[arg.name ] = arg.value ;
306+ }
307+ params[" arguments" ] = args;
308+ }
309+ this ->params (params);
310+ }
311+
312+ mcp::tools_call_response::tools_call_response (nlohmann::json id, tool_result_list result, bool error)
313+ : response(id), tool_result_(std::move(result)), error_(error)
314+ {
315+ refreshResult ();
316+ }
317+
318+ void mcp::tools_call_response::tool_result (mcp::tool_result_list result) {
319+ tool_result_ = std::move (result);
320+ refreshResult ();
321+ }
322+
323+ void mcp::tools_call_response::tool_error (bool error) {
324+ error_ = error;
325+ refreshResult ();
326+ }
327+
328+ void mcp::tools_call_response::refreshResult () {
329+ json result = json::object ();
330+ result[" isError" ] = error_;
331+ json content = json::array ();
332+ for (const auto & res : tool_result_) {
333+ json r;
334+ r[" type" ] = res.type ;
335+ if (res.type == " text" ) {
336+ r[" text" ] = res.value ;
337+
338+ } else if (res.type == " image" || res.type == " audio" ) {
339+ r[" data" ] = res.value ;
340+ r[" mimeType" ] = res.mime_type .value (); // throws
341+
342+ } else if (res.type == " resource" ) {
343+ json rr;
344+ rr[" uri" ] = res.uri .value (); // throws
345+ rr[" mimeType" ] = res.mime_type .value (); // throws
346+ rr[" text" ] = res.value ;
347+
348+ r[" resource" ] = rr;
349+
350+ } else {
351+ // throw
352+ }
353+ content.push_back (r);
354+ }
355+ result[" content" ] = content;
356+ this ->result (std::move (result));
357+ }
358+
283359static bool has_initialized_response (const nlohmann::json & data) {
284360 return data[" result" ].contains (" capabilities" );
285361}
0 commit comments