@@ -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