|
11 | 11 | // |
12 | 12 | //===----------------------------------------------------------------------===// |
13 | 13 |
|
14 | | -#ifndef LLDB_PLUGINS_PROTOCOL_MCP_PROTOCOL_H |
15 | | -#define LLDB_PLUGINS_PROTOCOL_MCP_PROTOCOL_H |
| 14 | +#ifndef LLDB_PROTOCOL_MCP_PROTOCOL_H |
| 15 | +#define LLDB_PROTOCOL_MCP_PROTOCOL_H |
16 | 16 |
|
17 | 17 | #include "llvm/Support/JSON.h" |
18 | 18 | #include <optional> |
19 | 19 | #include <string> |
20 | 20 | #include <variant> |
21 | 21 |
|
22 | | -namespace lldb_private::mcp::protocol { |
| 22 | +namespace lldb_protocol::mcp { |
23 | 23 |
|
24 | | -static llvm::StringLiteral kVersion = "2024-11-05"; |
| 24 | +static llvm::StringLiteral kProtocolVersion = "2024-11-05"; |
| 25 | + |
| 26 | +/// A Request or Response 'id'. |
| 27 | +/// |
| 28 | +/// NOTE: This differs from the JSON-RPC 2.0 spec. The MCP spec says this must |
| 29 | +/// be a string or number, excluding a json 'null' as a valid id. |
| 30 | +using Id = std::variant<int64_t, std::string>; |
25 | 31 |
|
26 | 32 | /// A request that expects a response. |
27 | 33 | struct Request { |
28 | | - uint64_t id = 0; |
| 34 | + /// The request id. |
| 35 | + Id id = 0; |
| 36 | + /// The method to be invoked. |
29 | 37 | std::string method; |
| 38 | + /// The method's params. |
30 | 39 | std::optional<llvm::json::Value> params; |
31 | 40 | }; |
32 | 41 |
|
33 | 42 | llvm::json::Value toJSON(const Request &); |
34 | 43 | bool fromJSON(const llvm::json::Value &, Request &, llvm::json::Path); |
| 44 | +bool operator==(const Request &, const Request &); |
35 | 45 |
|
36 | | -struct ErrorInfo { |
| 46 | +struct Error { |
| 47 | + /// The error type that occurred. |
37 | 48 | int64_t code = 0; |
| 49 | + /// A short description of the error. The message SHOULD be limited to a |
| 50 | + /// concise single sentence. |
38 | 51 | std::string message; |
39 | | - std::string data; |
40 | | -}; |
41 | | - |
42 | | -llvm::json::Value toJSON(const ErrorInfo &); |
43 | | -bool fromJSON(const llvm::json::Value &, ErrorInfo &, llvm::json::Path); |
44 | | - |
45 | | -struct Error { |
46 | | - uint64_t id = 0; |
47 | | - ErrorInfo error; |
| 52 | + /// Additional information about the error. The value of this member is |
| 53 | + /// defined by the sender (e.g. detailed error information, nested errors |
| 54 | + /// etc.). |
| 55 | + std::optional<llvm::json::Value> data; |
48 | 56 | }; |
49 | 57 |
|
50 | 58 | llvm::json::Value toJSON(const Error &); |
51 | 59 | bool fromJSON(const llvm::json::Value &, Error &, llvm::json::Path); |
| 60 | +bool operator==(const Error &, const Error &); |
52 | 61 |
|
| 62 | +/// A response to a request, either an error or a result. |
53 | 63 | struct Response { |
54 | | - uint64_t id = 0; |
55 | | - std::optional<llvm::json::Value> result; |
56 | | - std::optional<ErrorInfo> error; |
| 64 | + /// The request id. |
| 65 | + Id id = 0; |
| 66 | + /// The result of the request, either an Error or the JSON value of the |
| 67 | + /// response. |
| 68 | + std::variant<Error, llvm::json::Value> result; |
57 | 69 | }; |
58 | 70 |
|
59 | 71 | llvm::json::Value toJSON(const Response &); |
60 | 72 | bool fromJSON(const llvm::json::Value &, Response &, llvm::json::Path); |
| 73 | +bool operator==(const Response &, const Response &); |
61 | 74 |
|
62 | 75 | /// A notification which does not expect a response. |
63 | 76 | struct Notification { |
| 77 | + /// The method to be invoked. |
64 | 78 | std::string method; |
| 79 | + /// The notification's params. |
65 | 80 | std::optional<llvm::json::Value> params; |
66 | 81 | }; |
67 | 82 |
|
68 | 83 | llvm::json::Value toJSON(const Notification &); |
69 | 84 | bool fromJSON(const llvm::json::Value &, Notification &, llvm::json::Path); |
| 85 | +bool operator==(const Notification &, const Notification &); |
| 86 | + |
| 87 | +/// A general message as defined by the JSON-RPC 2.0 spec. |
| 88 | +using Message = std::variant<Request, Response, Notification>; |
| 89 | +// With clang-cl and MSVC STL 202208, convertible can be false later if we do |
| 90 | +// not force it to be checked early here. |
| 91 | +static_assert(std::is_convertible_v<Message, Message>, |
| 92 | + "Message is not convertible to itself"); |
| 93 | + |
| 94 | +bool fromJSON(const llvm::json::Value &, Message &, llvm::json::Path); |
| 95 | +llvm::json::Value toJSON(const Message &); |
70 | 96 |
|
71 | 97 | struct ToolCapability { |
72 | 98 | /// Whether this server supports notifications for changes to the tool list. |
@@ -176,13 +202,8 @@ struct ToolDefinition { |
176 | 202 | llvm::json::Value toJSON(const ToolDefinition &); |
177 | 203 | bool fromJSON(const llvm::json::Value &, ToolDefinition &, llvm::json::Path); |
178 | 204 |
|
179 | | -using Message = std::variant<Request, Response, Notification, Error>; |
180 | | - |
181 | | -bool fromJSON(const llvm::json::Value &, Message &, llvm::json::Path); |
182 | | -llvm::json::Value toJSON(const Message &); |
183 | | - |
184 | 205 | using ToolArguments = std::variant<std::monostate, llvm::json::Value>; |
185 | 206 |
|
186 | | -} // namespace lldb_private::mcp::protocol |
| 207 | +} // namespace lldb_protocol::mcp |
187 | 208 |
|
188 | 209 | #endif |
0 commit comments