Skip to content

Commit b67a04c

Browse files
committed
Switch to compile-time polymorphic message types
1 parent 2c07ce7 commit b67a04c

File tree

2 files changed

+48
-125
lines changed

2 files changed

+48
-125
lines changed

common/toolcall/mcp_messages.cpp

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,19 @@ const std::string mcp::McpVersion = "2024-11-05";
88
const std::string mcp::ClientVersion = "1.0.0";
99
const std::string mcp::ClientName = "llama.cpp";
1010

11-
mcp::message::message(std::optional<nlohmann::json> id) : id_(std::move(id))
12-
{
13-
}
14-
15-
void mcp::message::id(std::optional<nlohmann::json> id) {
16-
id_ = std::move(id);
17-
}
18-
19-
const std::optional<nlohmann::json> & mcp::message::id() const {
20-
return id_;
21-
}
22-
23-
mcp::request::request(std::optional<nlohmann::json> id,
24-
std::string method,
25-
std::optional<nlohmann::json> params)
26-
27-
: message(id), method_(std::move(method)), params_(std::move(params))
28-
{
29-
}
30-
3111
json mcp::request::toJson() const {
3212
json j;
3313
j["jsonrpc"] = JsonRpcVersion;
34-
j["method"] = method();
3514
if (id()) {
3615
j["id"] = id().value();
3716
}
17+
j["method"] = method();
3818
if (params()) {
3919
j["params"] = params().value();
4020
}
4121
return j;
4222
}
4323

44-
void mcp::request::method(std::string method) {
45-
method_ = std::move(method);
46-
}
47-
48-
const std::string & mcp::request::method() const {
49-
return method_;
50-
}
51-
52-
void mcp::request::params(std::optional<nlohmann::json> params) {
53-
params_ = std::move(params);
54-
}
55-
56-
const std::optional<nlohmann::json> & mcp::request::params() const {
57-
return params_;
58-
}
59-
60-
mcp::response::response(std::optional<nlohmann::json> id,
61-
std::optional<nlohmann::json> result,
62-
std::optional<mcp::response::error> error)
63-
64-
: message(id), result_(result), error_(error)
65-
{
66-
}
67-
6824
json mcp::response::error::toJson() const {
6925
json j;
7026
j["code"] = code;
@@ -89,28 +45,6 @@ json mcp::response::toJson() const {
8945
return j;
9046
}
9147

92-
void mcp::response::result(std::optional<nlohmann::json> result) {
93-
result_ = std::move(result);
94-
}
95-
96-
const std::optional<nlohmann::json> & mcp::response::result() const {
97-
return result_;
98-
}
99-
100-
void mcp::response::setError(std::optional<mcp::response::error> error) {
101-
error_ = std::move(error);
102-
}
103-
104-
const std::optional<mcp::response::error> & mcp::response::getError() const {
105-
return error_;
106-
}
107-
108-
mcp::notification::notification(
109-
std::string method, std::optional<nlohmann::json> params)
110-
: message(), method_(method), params_(params)
111-
{
112-
}
113-
11448
json mcp::notification::toJson() const {
11549
json j;
11650
j["jsonrpc"] = JsonRpcVersion;
@@ -121,22 +55,6 @@ json mcp::notification::toJson() const {
12155
return j;
12256
}
12357

124-
void mcp::notification::method(std::string method) {
125-
method_ = std::move(method);
126-
}
127-
128-
const std::string & mcp::notification::method() const {
129-
return method_;
130-
}
131-
132-
void mcp::notification::params(std::optional<nlohmann::json> params) {
133-
params_ = std::move(params);
134-
}
135-
136-
const std::optional<nlohmann::json> & mcp::notification::params() const {
137-
return params_;
138-
}
139-
14058
mcp::initialize_request::initialize_request(nlohmann::json id, mcp::capabilities caps)
14159
: request(id, "initialize"), caps_(std::move(caps))
14260
{
@@ -261,8 +179,3 @@ mcp::initialize_response mcp::initialize_response::fromJson(const nlohmann::json
261179

262180
return initialize_response(j["id"], name, version, protoVersion, caps);
263181
}
264-
265-
mcp::initialized_notification::initialized_notification()
266-
: notification("notifications/initialized")
267-
{
268-
}

common/toolcall/mcp_messages.hpp

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,52 @@ namespace mcp
1010
extern const std::string ClientVersion;
1111
extern const std::string ClientName;
1212

13+
template <typename Derived>
1314
class message {
1415
public:
15-
message(std::optional<nlohmann::json> id = std::nullopt);
16+
message(std::optional<nlohmann::json> id = std::nullopt)
17+
: id_(std::move(id)) {}
1618

17-
virtual ~message() = default;
18-
virtual nlohmann::json toJson() const = 0;
19+
nlohmann::json toJson() const {
20+
return static_cast<Derived*>(this)->toJson();
21+
}
1922

20-
void id(std::optional<nlohmann::json> id);
21-
const std::optional<nlohmann::json> & id() const;
23+
void id(std::optional<nlohmann::json> id) {
24+
id_ = std::move(id);
25+
}
26+
27+
const std::optional<nlohmann::json> & id() const {
28+
return id_;
29+
}
2230

2331
private:
2432
std::optional<nlohmann::json> id_;
2533
};
2634

27-
28-
class request : public message {
35+
class request : public message<request> {
2936
public:
3037
request(std::optional<nlohmann::json> id,
3138
std::string method,
32-
std::optional<nlohmann::json> params = std::nullopt);
39+
std::optional<nlohmann::json> params = std::nullopt)
40+
41+
: message(id),
42+
method_(std::move(method)),
43+
params_(std::move(params)) {}
3344

34-
virtual ~request() = default;
35-
nlohmann::json toJson() const override;
45+
void method(std::string method) { method_ = std::move(method); }
46+
const std::string & method() const { return method_; }
3647

37-
void method(std::string method);
38-
const std::string & method() const;
48+
void params(std::optional<nlohmann::json> params) { params_ = std::move(params); }
49+
const std::optional<nlohmann::json> & params() const { return params_; }
3950

40-
void params(std::optional<nlohmann::json> params);
41-
const std::optional<nlohmann::json> & params() const;
51+
nlohmann::json toJson() const;
4252

4353
private:
4454
std::string method_;
4555
std::optional<nlohmann::json> params_;
4656
};
4757

48-
49-
class response : public message {
58+
class response : public message<response> {
5059
public:
5160
struct error {
5261
int code;
@@ -57,43 +66,47 @@ namespace mcp
5766

5867
response(std::optional<nlohmann::json> id,
5968
std::optional<nlohmann::json> result = std::nullopt,
60-
std::optional<error> error = std::nullopt);
69+
std::optional<error> error = std::nullopt)
70+
71+
: message(id),
72+
result_(std::move(result)),
73+
error_(std::move(error)) {}
6174

62-
virtual ~response() = default;
63-
virtual nlohmann::json toJson() const override;
75+
void result(std::optional<nlohmann::json> result) { result_ = std::move(result); }
76+
const std::optional<nlohmann::json> & result() const { return result_; }
6477

65-
void result(std::optional<nlohmann::json> result);
66-
const std::optional<nlohmann::json> & result() const;
78+
void setError(std::optional<error> error) { error_ = std::move(error); }
79+
const std::optional<error> & getError() const { return error_; }
6780

68-
void setError(std::optional<error> error);
69-
const std::optional<error> & getError() const;
81+
nlohmann::json toJson() const;
7082

7183
private:
7284
std::optional<nlohmann::json> result_;
7385
std::optional<error> error_;
7486
};
7587

76-
77-
class notification : public message {
88+
class notification : public message<notification> {
7889
public:
7990
notification(std::string method,
80-
std::optional<nlohmann::json> params = std::nullopt);
91+
std::optional<nlohmann::json> params = std::nullopt)
8192

82-
virtual ~notification() = default;
83-
virtual nlohmann::json toJson() const override;
93+
: message(),
94+
method_(method),
95+
params_(params) {}
8496

85-
void method(std::string method);
86-
const std::string & method() const;
97+
void method(std::string method) { method_ = std::move(method); }
98+
const std::string & method() const { return method_; }
8799

88-
void params(std::optional<nlohmann::json> params);
89-
const std::optional<nlohmann::json> & params() const;
100+
void params(std::optional<nlohmann::json> params) { params_ = std::move(params); }
101+
const std::optional<nlohmann::json> & params() const { return params_; }
102+
103+
nlohmann::json toJson() const;
90104

91105
private:
92106
std::string method_;
93107
std::optional<nlohmann::json> params_;
94108
};
95109

96-
97110
struct capability {
98111
std::string name;
99112
bool subscribe = false;
@@ -119,7 +132,6 @@ namespace mcp
119132
mcp::capabilities caps_;
120133
};
121134

122-
123135
class initialize_response : public response {
124136
public:
125137
initialize_response(nlohmann::json id,
@@ -151,10 +163,8 @@ namespace mcp
151163
mcp::capabilities caps_;
152164
};
153165

154-
155166
class initialized_notification : public notification {
156167
public:
157-
initialized_notification();
168+
initialized_notification() : notification("notifications/initialized") {}
158169
};
159170
}
160-

0 commit comments

Comments
 (0)