Skip to content

Commit 9dbe42f

Browse files
committed
Add tools/list response
1 parent 20a19f8 commit 9dbe42f

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

common/toolcall/mcp_messages.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,60 @@ void mcp::tools_list_request::refreshParams() {
199199
this->params(params);
200200
}
201201
}
202+
203+
mcp::tools_list_response::tools_list_response(nlohmann::json id,
204+
mcp::tools_list tools,
205+
std::string next_cursor)
206+
: response(id),
207+
tools_(std::move(tools)),
208+
next_cursor_(std::move(next_cursor))
209+
{
210+
refreshResult();
211+
}
212+
213+
void mcp::tools_list_response::tools(mcp::tools_list tools) {
214+
tools_ = std::move(tools);
215+
refreshResult();
216+
}
217+
218+
void mcp::tools_list_response::next_cursor(std::string next_cursor) {
219+
next_cursor_ = std::move(next_cursor);
220+
refreshResult();
221+
}
222+
223+
void mcp::tools_list_response::refreshResult() {
224+
json result;
225+
226+
json tools = json::array();
227+
for (const auto & tool : tools_) {
228+
json t;
229+
230+
t["name"] = tool.tool_name;
231+
t["description"] = tool.tool_description;
232+
t["inputSchema"]["type"] = "object";
233+
234+
json props;
235+
for (const auto & param : tool.params) {
236+
props[param.name] = {
237+
{"type"}, {param.type},
238+
{"description"}, {param.description}
239+
};
240+
}
241+
t["inputSchema"]["properties"] = props;
242+
243+
json required = json::array();
244+
for (const auto & req_param : tool.required_params) {
245+
required.push_back(req_param);
246+
}
247+
t["inputSchema"]["required"] = required;
248+
249+
tools.push_back(t);
250+
}
251+
result["tools"] = tools;
252+
253+
if (! next_cursor_.empty()) {
254+
result["nextCursor"] = next_cursor_;
255+
}
256+
257+
this->result(result);
258+
}

common/toolcall/mcp_messages.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,36 @@ namespace mcp
180180
void refreshParams();
181181
std::string cursor_;
182182
};
183+
184+
struct tool {
185+
struct param {
186+
std::string name;
187+
std::string type;
188+
std::string description;
189+
};
190+
std::string tool_name;
191+
std::string tool_description;
192+
std::vector<param> params;
193+
std::vector<std::string> required_params;
194+
};
195+
196+
using tools_list = std::vector<tool>;
197+
198+
class tools_list_response : public response {
199+
public:
200+
tools_list_response(nlohmann::json id,
201+
tools_list tools = tools_list(),
202+
std::string next_cursor = "");
203+
204+
void tools(tools_list tools);
205+
const tools_list & tools() const { return tools_; }
206+
207+
void next_cursor(std::string next_cursor);
208+
const std::string & next_cursor() { return next_cursor_; }
209+
210+
private:
211+
void refreshResult();
212+
tools_list tools_;
213+
std::string next_cursor_;
214+
};
183215
}

0 commit comments

Comments
 (0)