Skip to content

Commit 5d6a058

Browse files
committed
Store callbacks in map
1 parent 3e46978 commit 5d6a058

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

toolcall/handler.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ void toolcall::mcp_impl::initialize() {
100100
tools_populating_.notify_one();
101101
};
102102

103-
transport_->subscribe(set_caps);
103+
transport_->subscribe("set_caps", set_caps);
104104

105105
mcp::initialize_request req(next_id_++);
106106
transport_->send(req.toJson());
107107

108108
tools_populating_.wait_for(lock, std::chrono::seconds(15));
109-
transport_->unsubscribe(set_caps);
109+
transport_->unsubscribe<mcp::initialize_response>("set_caps");
110110

111111
on_list_changed update_dirty = [this] (const mcp::tools_list_changed_notification &) {
112112
tool_list_dirty_ = true;
@@ -117,7 +117,7 @@ void toolcall::mcp_impl::initialize() {
117117
if (cap.name == "tools") {
118118
has_tools = true;
119119
if (cap.listChanged) {
120-
transport_->subscribe(update_dirty);
120+
transport_->subscribe("update_dirty", update_dirty);
121121
}
122122
break;
123123
}
@@ -153,13 +153,13 @@ std::string toolcall::mcp_impl::tool_list() {
153153
tools_populating_.notify_one();
154154
};
155155

156-
transport_->subscribe(set_tools);
156+
transport_->subscribe("set_tools", set_tools);
157157

158158
mcp::tools_list_request req(std::to_string(next_id_++));
159159
transport_->send(req.toJson());
160160

161161
tools_populating_.wait_for(lock, std::chrono::seconds(15));
162-
transport_->unsubscribe(set_tools);
162+
transport_->unsubscribe<mcp::tools_list_response>("set_tools");
163163

164164
tools_ = tools_list_to_oai_json(tools);
165165
}

toolcall/mcp_transport.h

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "mcp_messages.h"
44
#include <string>
55
#include <tuple>
6-
#include <vector>
6+
#include <map>
77

88
namespace toolcall
99
{
@@ -13,27 +13,32 @@ namespace toolcall
1313
template <typename Derived, typename... MessageTypes>
1414
class mcp_transport_t {
1515
public:
16-
1716
template <typename T>
18-
void subscribe(callback<T> callback) {
19-
auto& vec = std::get<std::vector<toolcall::callback<T>>>(subscribers_);
20-
vec.push_back(std::move(callback));
17+
void subscribe(std::string key, callback<T> callback) {
18+
auto& map =
19+
std::get<std::map<std::string, toolcall::callback<T>>>(
20+
subscribers_);
21+
22+
map.insert({key, callback});
2123
}
2224

2325
template <typename T>
24-
void unsubscribe(callback<T> callback) {
25-
auto& vec = std::get<std::vector<toolcall::callback<T>>>(subscribers_);
26-
auto found = std::find(vec.begin(), vec.end(), callback);
27-
if (found != vec.end()) {
28-
vec.erase(found);
29-
}
26+
void unsubscribe(std::string key) {
27+
auto& map =
28+
std::get<std::map<std::string, toolcall::callback<T>>>(
29+
subscribers_);
30+
31+
map.erase(key);
3032
}
3133

3234
template <typename T>
3335
void notify(const T & message) const {
34-
const auto& vec = std::get<std::vector<toolcall::callback<T>>>(subscribers_);
35-
for (const auto& callback : vec) {
36-
callback(message);
36+
const auto& map =
37+
std::get<std::map<std::string, toolcall::callback<T>>>(
38+
subscribers_);
39+
40+
for (const auto & pair : map) {
41+
pair.second(message);
3742
}
3843
}
3944

@@ -50,7 +55,7 @@ namespace toolcall
5055
}
5156

5257
private:
53-
std::tuple<std::vector<toolcall::callback<MessageTypes>>...> subscribers_;
58+
std::tuple<std::map<std::string, toolcall::callback<MessageTypes>>...> subscribers_;
5459
};
5560

5661
class mcp_transport : public mcp_transport_t <mcp_transport,

0 commit comments

Comments
 (0)