Skip to content

Commit

Permalink
Rename handler -> client to reflect MCP terminology
Browse files Browse the repository at this point in the history
  • Loading branch information
bandoti committed Feb 25, 2025
1 parent 9db9686 commit e8dd857
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
28 changes: 14 additions & 14 deletions examples/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <vector>

#ifdef LLAMA_USE_TOOLCALL
# include "toolcall-handler.h"
# include "toolcall-client.h"
#endif

#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
Expand Down Expand Up @@ -100,9 +100,9 @@ class chat_formatter {
std::vector<common_chat_msg> & chat_msgs,
struct common_chat_templates * chat_templates,
const llama_vocab * vocab,
toolcall::handler::ptr tc_handler)
toolcall::client::ptr tc_client)

: params_(params), chat_msgs_(chat_msgs), chat_templates_(chat_templates), vocab_(vocab), tc_handler_(tc_handler) {}
: params_(params), chat_msgs_(chat_msgs), chat_templates_(chat_templates), vocab_(vocab), tc_client_(tc_client) {}
#endif

std::string operator () (const std::string & role, const std::string & content, [[maybe_unused]] bool use_toolcalls = false) {
Expand All @@ -114,9 +114,9 @@ class chat_formatter {
common_chat_params cparams;
common_chat_templates_inputs cinputs;
#ifdef LLAMA_USE_TOOLCALL
if (tc_handler_ != nullptr && use_toolcalls) {
cinputs.tool_choice = common_chat_tool_choice_parse_oaicompat(tc_handler_->tool_choice());
cinputs.tools = common_chat_tools_parse_oaicompat(tc_handler_->tool_list());
if (tc_client_ != nullptr && use_toolcalls) {
cinputs.tool_choice = common_chat_tool_choice_parse_oaicompat(tc_client_->tool_choice());
cinputs.tools = common_chat_tools_parse_oaicompat(tc_client_->tool_list());
}
#endif
bool add_ass = role == "user";
Expand All @@ -140,7 +140,7 @@ class chat_formatter {

#ifdef LLAMA_USE_TOOLCALL
const llama_vocab * vocab_;
toolcall::handler::ptr tc_handler_;
toolcall::client::ptr tc_client_;
#endif
};

Expand Down Expand Up @@ -328,11 +328,11 @@ int main(int argc, char ** argv) {
std::vector<llama_token> embd_inp;

#ifdef LLAMA_USE_TOOLCALL
auto tc_handler = toolcall::create_handler(tc_params);
if (tc_handler) {
tc_handler->initialize();
auto tc_client = toolcall::create_client(tc_params);
if (tc_client) {
tc_client->initialize();
}
chat_formatter chat_add_and_format(params, chat_msgs, chat_templates.get(), vocab, tc_handler);
chat_formatter chat_add_and_format(params, chat_msgs, chat_templates.get(), vocab, tc_client);
#else
chat_formatter chat_add_and_format(params, chat_msgs, chat_templates.get());
#endif
Expand Down Expand Up @@ -851,8 +851,8 @@ int main(int argc, char ** argv) {
if (params.enable_chat_template) {
chat_add_and_format("assistant", assistant_ss.str(), true);
#ifdef LLAMA_USE_TOOLCALL
auto should_use_toolcall = [&params, tc_handler] (const std::string & asst_msg) {
if (! params.use_jinja || tc_handler == nullptr) {
auto should_use_toolcall = [&params, tc_client] (const std::string & asst_msg) {
if (! params.use_jinja || tc_client == nullptr) {
return false;
}
try {
Expand All @@ -865,7 +865,7 @@ int main(int argc, char ** argv) {
};

if (should_use_toolcall(assistant_ss.str())) {
toolcall::result_set res = tc_handler->call(assistant_ss.str());
toolcall::result_set res = tc_client->call(assistant_ss.str());
if (! res.empty()) {
std::string toolcall_result_str;
for (const auto & r : res) {
Expand Down
4 changes: 2 additions & 2 deletions toolcall/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
set(TARGET toolcall)

set(SOURCES
handler.cpp
client.cpp
mcp_messages.cpp
mcp_stdio_transport.cpp
params.cpp
)

set(HEADERS
toolcall-params.h
toolcall-handler.h
toolcall-client.h
mcp_transport.h
mcp_messages.h
mcp_stdio_transport.h
Expand Down
28 changes: 14 additions & 14 deletions toolcall/handler.cpp → toolcall/client.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

#include <json.hpp>
#include "toolcall-handler.h"
#include "toolcall-client.h"
#include <chrono>
#include <stdexcept>

Expand All @@ -12,48 +12,48 @@

using json = nlohmann::json;

std::shared_ptr<toolcall::handler> toolcall::create_handler(const toolcall::params & params) {
std::shared_ptr<toolcall::handler> handler;
std::shared_ptr<toolcall::client> toolcall::create_client(const toolcall::params & params) {
std::shared_ptr<toolcall::client> client;

auto tools = params.tools();
auto choice = params.choice();
if (params) {
if (params.has_uri()) {
#ifdef LLAMA_USE_CURL
handler.reset(new toolcall::handler(
client.reset(new toolcall::client(
std::make_unique<toolcall::mcp_impl>(tools, choice)));
#endif
} else {
handler.reset(new toolcall::handler(
client.reset(new toolcall::client(
std::make_unique<toolcall::loopback_impl>(tools, choice)));
}
}
return handler;
return client;
}

std::string toolcall::handler::tool_list() {
std::string toolcall::client::tool_list() {
return impl_->tool_list();
}

bool toolcall::handler::tool_list_dirty() const {
bool toolcall::client::tool_list_dirty() const {
return impl_->tool_list_dirty();
}

toolcall::result_set toolcall::handler::call(const std::string & request) {
toolcall::result_set toolcall::client::call(const std::string & request) {
return impl_->call(request);
}

const std::string & toolcall::handler::tool_choice() const {
const std::string & toolcall::client::tool_choice() const {
return impl_->tool_choice();
}

void toolcall::handler::initialize() {
void toolcall::client::initialize() {
impl_->initialize();
}

#ifdef LLAMA_USE_CURL
toolcall::mcp_impl::mcp_impl(std::string server_uri, std::string tool_choice)
: handler_impl(tool_choice),
: client_impl(tool_choice),
transport_(new mcp_sse_transport(server_uri)),
tools_("[]"),
tools_mutex_(),
Expand All @@ -63,7 +63,7 @@ toolcall::mcp_impl::mcp_impl(std::string server_uri, std::string tool_choice)
}
#else
toolcall::mcp_impl::mcp_impl(std::string /*server_uri*/, std::string tool_choice)
: handler_impl(tool_choice),
: client_impl(tool_choice),
transport_(nullptr),
tools_("[]"),
tools_mutex_(),
Expand All @@ -74,7 +74,7 @@ toolcall::mcp_impl::mcp_impl(std::string /*server_uri*/, std::string tool_choice
#endif

toolcall::mcp_impl::mcp_impl(std::vector<std::string> argv, std::string tool_choice)
: handler_impl(tool_choice),
: client_impl(tool_choice),
transport_(new mcp_stdio_transport(argv))
{
}
Expand Down
24 changes: 12 additions & 12 deletions toolcall/toolcall-handler.h → toolcall/toolcall-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ namespace toolcall

using result_set = std::vector<result>;

class handler_impl;
class handler {
class client_impl;
class client {
public:
using ptr = std::shared_ptr<handler>;
using ptr = std::shared_ptr<client>;

handler(std::unique_ptr<handler_impl> impl) : impl_(std::move(impl)) {}
client(std::unique_ptr<client_impl> impl) : impl_(std::move(impl)) {}

result_set call(const std::string & request);

Expand All @@ -37,17 +37,17 @@ namespace toolcall
void initialize();

private:
std::unique_ptr<handler_impl> impl_;
std::unique_ptr<client_impl> impl_;
};

std::shared_ptr<toolcall::handler> create_handler(const toolcall::params & params);
std::shared_ptr<toolcall::client> create_client(const toolcall::params & params);

class handler_impl {
class client_impl {
public:
handler_impl(std::string tool_choice)
client_impl(std::string tool_choice)
: tool_choice_(std::move(tool_choice)), tool_list_dirty_(true) {}

virtual ~handler_impl() = default;
virtual ~client_impl() = default;

virtual std::string tool_list() = 0;

Expand All @@ -66,10 +66,10 @@ namespace toolcall
bool tool_list_dirty_;
};

class loopback_impl : public handler_impl {
class loopback_impl : public client_impl {
public:
loopback_impl(std::string tools, std::string tool_choice)
: handler_impl(tool_choice), tools_(std::move(tools)) {}
: client_impl(tool_choice), tools_(std::move(tools)) {}

virtual std::string tool_list() override {
tool_list_dirty_ = false;
Expand All @@ -87,7 +87,7 @@ namespace toolcall
};

class mcp_transport;
class mcp_impl : public handler_impl {
class mcp_impl : public client_impl {
public:
mcp_impl(std::string server_uri, std::string tool_choice);
mcp_impl(std::vector<std::string> argv, std::string tool_choice);
Expand Down

0 comments on commit e8dd857

Please sign in to comment.