1616
1717namespace mcp {
1818
19+ // ==========================================================================================================
20+ // JSONValue
21+ // Purpose: Simplified JSON representation backed by std::variant and shared_ptr graphs.
22+ // Fields:
23+ // Array: vector<shared_ptr<JSONValue>> representing a JSON array.
24+ // Object: unordered_map<string, shared_ptr<JSONValue>> representing a JSON object.
25+ // value: variant holding nullptr, bool, int64_t, double, string, Array, or Object.
26+ // ==========================================================================================================
1927// JSON value type - simplified JSON representation using std library only
2028struct JSONValue {
2129 using Array = std::vector<std::shared_ptr<JSONValue>>;
@@ -57,9 +65,20 @@ struct JSONValue {
5765 const auto & get () const { return value; }
5866};
5967
68+ // ==========================================================================================================
69+ // JSONRPCId
70+ // Purpose: JSON-RPC 2.0 id variant (per spec): string, integer, or null.
71+ // ==========================================================================================================
6072// JSON-RPC 2.0 ID type
6173using JSONRPCId = std::variant<std::string, int64_t , std::nullptr_t >;
6274
75+ // ==========================================================================================================
76+ // JSONRPCMessage
77+ // Purpose: Abstract base for JSON-RPC 2.0 messages providing serialization APIs.
78+ // Methods:
79+ // Serialize(): Returns canonical JSON string for the message.
80+ // Deserialize(json): Parses JSON string into this object; returns true on success.
81+ // ==========================================================================================================
6382// Base JSON-RPC message
6483class JSONRPCMessage {
6584public:
@@ -70,6 +89,15 @@ class JSONRPCMessage {
7089 virtual bool Deserialize (const std::string& json) = 0;
7190};
7291
92+ // ==========================================================================================================
93+ // JSONRPCRequest
94+ // Purpose: JSON-RPC 2.0 request message with id, method, and optional params.
95+ // Ctors:
96+ // JSONRPCRequest(id, method, params?): Initializes fields (params optional).
97+ // Methods:
98+ // Serialize(): JSON string.
99+ // Deserialize(json): Returns true when parsed successfully.
100+ // ==========================================================================================================
73101// JSON-RPC Request
74102class JSONRPCRequest : public JSONRPCMessage {
75103public:
@@ -85,6 +113,16 @@ class JSONRPCRequest : public JSONRPCMessage {
85113 bool Deserialize (const std::string& json) override ;
86114};
87115
116+ // ==========================================================================================================
117+ // JSONRPCResponse
118+ // Purpose: JSON-RPC 2.0 response message carrying either result or error.
119+ // Ctors:
120+ // JSONRPCResponse(id, result): Success response with result set.
121+ // JSONRPCResponse(id, error, /*isError*/): Error response with error set.
122+ // Methods:
123+ // Serialize()/Deserialize(json)
124+ // IsError(): True when error is present.
125+ // ==========================================================================================================
88126// JSON-RPC Response
89127class JSONRPCResponse : public JSONRPCMessage {
90128public:
@@ -104,6 +142,14 @@ class JSONRPCResponse : public JSONRPCMessage {
104142 bool IsError () const { return error.has_value (); }
105143};
106144
145+ // ==========================================================================================================
146+ // JSONRPCNotification
147+ // Purpose: JSON-RPC 2.0 notification (no id, no response).
148+ // Ctors:
149+ // JSONRPCNotification(method, params?): Initializes method and optional params.
150+ // Methods:
151+ // Serialize()/Deserialize(json)
152+ // ==========================================================================================================
107153// JSON-RPC Notification
108154class JSONRPCNotification : public JSONRPCMessage {
109155public:
@@ -118,6 +164,12 @@ class JSONRPCNotification : public JSONRPCMessage {
118164 bool Deserialize (const std::string& json) override ;
119165};
120166
167+ // ==========================================================================================================
168+ // JSONRPCErrorCodes
169+ // Purpose: Standard JSON-RPC error codes plus MCP-specific codes for SDK ergonomics.
170+ // Notes:
171+ // Values are stable and align with JSON-RPC 2.0 spec; MCP codes are negative values in the -320xx range.
172+ // ==========================================================================================================
121173// JSON-RPC Error codes (standard + MCP specific)
122174namespace JSONRPCErrorCodes {
123175 constexpr int ParseError = -32700 ;
@@ -134,10 +186,31 @@ namespace JSONRPCErrorCodes {
134186 constexpr int PromptNotFound = -32004 ;
135187}
136188
189+ // ==========================================================================================================
190+ // CreateErrorObject
191+ // Purpose: Build a JSON error object with shape { code, message, data? }.
192+ // Args:
193+ // code: Integer error code (standard or MCP-specific).
194+ // message: Human-readable description.
195+ // data: Optional structured payload.
196+ // Returns:
197+ // JSONValue object representing the error.
198+ // ==========================================================================================================
137199// Utility functions for JSON-RPC error responses
138200JSONValue CreateErrorObject (int code, const std::string& message,
139201 const std::optional<JSONValue>& data = std::nullopt );
140202
203+ // ==========================================================================================================
204+ // CreateErrorResponse
205+ // Purpose: Convenience to wrap an error object into a JSONRPCResponse with the given id.
206+ // Args:
207+ // id: Request id to echo in the response (string | int64 | null).
208+ // code: Integer error code.
209+ // message: Error message.
210+ // data: Optional structured payload.
211+ // Returns:
212+ // unique_ptr<JSONRPCResponse> with error populated.
213+ // ==========================================================================================================
141214std::unique_ptr<JSONRPCResponse> CreateErrorResponse (
142215 const JSONRPCId& id, int code, const std::string& message,
143216 const std::optional<JSONValue>& data = std::nullopt );
0 commit comments