18
18
#include < optional>
19
19
#include < string>
20
20
#include < variant>
21
+ #include < vector>
21
22
22
23
namespace lldb_protocol ::mcp {
23
24
@@ -38,11 +39,24 @@ struct Request {
38
39
// / The method's params.
39
40
std::optional<llvm::json::Value> params;
40
41
};
41
-
42
42
llvm::json::Value toJSON (const Request &);
43
43
bool fromJSON (const llvm::json::Value &, Request &, llvm::json::Path);
44
44
bool operator ==(const Request &, const Request &);
45
45
46
+ enum ErrorCode : signed {
47
+ // / Invalid JSON was received by the server. An error occurred on the server
48
+ // / while parsing the JSON text.
49
+ eErrorCodeParseError = -32700 ,
50
+ // / The JSON sent is not a valid Request object.
51
+ eErrorCodeInvalidRequest = -32600 ,
52
+ // / The method does not exist / is not available.
53
+ eErrorCodeMethodNotFound = -32601 ,
54
+ // / Invalid method parameter(s).
55
+ eErrorCodeInvalidParams = -32602 ,
56
+ // / Internal JSON-RPC error.
57
+ eErrorCodeInternalError = -32603 ,
58
+ };
59
+
46
60
struct Error {
47
61
// / The error type that occurred.
48
62
int64_t code = 0 ;
@@ -52,9 +66,8 @@ struct Error {
52
66
// / Additional information about the error. The value of this member is
53
67
// / defined by the sender (e.g. detailed error information, nested errors
54
68
// / etc.).
55
- std::optional<llvm::json::Value> data;
69
+ std::optional<llvm::json::Value> data = std::nullopt ;
56
70
};
57
-
58
71
llvm::json::Value toJSON (const Error &);
59
72
bool fromJSON (const llvm::json::Value &, Error &, llvm::json::Path);
60
73
bool operator ==(const Error &, const Error &);
@@ -67,7 +80,6 @@ struct Response {
67
80
// / response.
68
81
std::variant<Error, llvm::json::Value> result;
69
82
};
70
-
71
83
llvm::json::Value toJSON (const Response &);
72
84
bool fromJSON (const llvm::json::Value &, Response &, llvm::json::Path);
73
85
bool operator ==(const Response &, const Response &);
@@ -79,7 +91,6 @@ struct Notification {
79
91
// / The notification's params.
80
92
std::optional<llvm::json::Value> params;
81
93
};
82
-
83
94
llvm::json::Value toJSON (const Notification &);
84
95
bool fromJSON (const llvm::json::Value &, Notification &, llvm::json::Path);
85
96
bool operator ==(const Notification &, const Notification &);
@@ -90,45 +101,9 @@ using Message = std::variant<Request, Response, Notification>;
90
101
// not force it to be checked early here.
91
102
static_assert (std::is_convertible_v<Message, Message>,
92
103
" Message is not convertible to itself" );
93
-
94
104
bool fromJSON (const llvm::json::Value &, Message &, llvm::json::Path);
95
105
llvm::json::Value toJSON (const Message &);
96
106
97
- struct ToolCapability {
98
- // / Whether this server supports notifications for changes to the tool list.
99
- bool listChanged = false ;
100
- };
101
-
102
- llvm::json::Value toJSON (const ToolCapability &);
103
- bool fromJSON (const llvm::json::Value &, ToolCapability &, llvm::json::Path);
104
-
105
- struct ResourceCapability {
106
- // / Whether this server supports notifications for changes to the resources
107
- // / list.
108
- bool listChanged = false ;
109
-
110
- // / Whether subscriptions are supported.
111
- bool subscribe = false ;
112
- };
113
-
114
- llvm::json::Value toJSON (const ResourceCapability &);
115
- bool fromJSON (const llvm::json::Value &, ResourceCapability &,
116
- llvm::json::Path);
117
-
118
- // / Capabilities that a server may support. Known capabilities are defined here,
119
- // / in this schema, but this is not a closed set: any server can define its own,
120
- // / additional capabilities.
121
- struct Capabilities {
122
- // / Tool capabilities of the server.
123
- ToolCapability tools;
124
-
125
- // / Resource capabilities of the server.
126
- ResourceCapability resources;
127
- };
128
-
129
- llvm::json::Value toJSON (const Capabilities &);
130
- bool fromJSON (const llvm::json::Value &, Capabilities &, llvm::json::Path);
131
-
132
107
// / A known resource that the server is capable of reading.
133
108
struct Resource {
134
109
// / The URI of this resource.
@@ -138,17 +113,25 @@ struct Resource {
138
113
std::string name;
139
114
140
115
// / A description of what this resource represents.
141
- std::string description;
116
+ std::string description = " " ;
142
117
143
118
// / The MIME type of this resource, if known.
144
- std::string mimeType;
119
+ std::string mimeType = " " ;
145
120
};
146
121
147
122
llvm::json::Value toJSON (const Resource &);
148
123
bool fromJSON (const llvm::json::Value &, Resource &, llvm::json::Path);
149
124
125
+ // / The server’s response to a resources/list request from the client.
126
+ struct ListResourcesResult {
127
+ std::vector<Resource> resources;
128
+ };
129
+ llvm::json::Value toJSON (const ListResourcesResult &);
130
+ bool fromJSON (const llvm::json::Value &, ListResourcesResult &,
131
+ llvm::json::Path);
132
+
150
133
// / The contents of a specific resource or sub-resource.
151
- struct ResourceContents {
134
+ struct TextResourceContents {
152
135
// / The URI of this resource.
153
136
std::string uri;
154
137
@@ -160,34 +143,37 @@ struct ResourceContents {
160
143
std::string mimeType;
161
144
};
162
145
163
- llvm::json::Value toJSON (const ResourceContents &);
164
- bool fromJSON (const llvm::json::Value &, ResourceContents &, llvm::json::Path);
146
+ llvm::json::Value toJSON (const TextResourceContents &);
147
+ bool fromJSON (const llvm::json::Value &, TextResourceContents &,
148
+ llvm::json::Path);
165
149
166
- // / The server's response to a resources/read request from the client.
167
- struct ResourceResult {
168
- std::vector<ResourceContents> contents;
150
+ // / Sent from the client to the server, to read a specific resource URI.
151
+ struct ReadResourceParams {
152
+ // / The URI of the resource to read. The URI can use any protocol; it is up to
153
+ // / the server how to interpret it.
154
+ std::string uri;
169
155
};
156
+ llvm::json::Value toJSON (const ReadResourceParams &);
157
+ bool fromJSON (const llvm::json::Value &, ReadResourceParams &,
158
+ llvm::json::Path);
170
159
171
- llvm::json::Value toJSON (const ResourceResult &);
172
- bool fromJSON (const llvm::json::Value &, ResourceResult &, llvm::json::Path);
160
+ // / The server's response to a resources/read request from the client.
161
+ struct ReadResourceResult {
162
+ std::vector<TextResourceContents> contents;
163
+ };
164
+ llvm::json::Value toJSON (const ReadResourceResult &);
165
+ bool fromJSON (const llvm::json::Value &, ReadResourceResult &,
166
+ llvm::json::Path);
173
167
174
168
// / Text provided to or from an LLM.
175
169
struct TextContent {
176
170
// / The text content of the message.
177
171
std::string text;
178
172
};
179
-
180
173
llvm::json::Value toJSON (const TextContent &);
181
174
bool fromJSON (const llvm::json::Value &, TextContent &, llvm::json::Path);
182
175
183
- struct TextResult {
184
- std::vector<TextContent> content;
185
- bool isError = false ;
186
- };
187
-
188
- llvm::json::Value toJSON (const TextResult &);
189
- bool fromJSON (const llvm::json::Value &, TextResult &, llvm::json::Path);
190
-
176
+ // / Definition for a tool the client can call.
191
177
struct ToolDefinition {
192
178
// / Unique identifier for the tool.
193
179
std::string name;
@@ -198,12 +184,144 @@ struct ToolDefinition {
198
184
// JSON Schema for the tool's parameters.
199
185
std::optional<llvm::json::Value> inputSchema;
200
186
};
201
-
202
187
llvm::json::Value toJSON (const ToolDefinition &);
203
188
bool fromJSON (const llvm::json::Value &, ToolDefinition &, llvm::json::Path);
204
189
205
190
using ToolArguments = std::variant<std::monostate, llvm::json::Value>;
206
191
192
+ // / Describes the name and version of an MCP implementation, with an optional
193
+ // / title for UI representation.
194
+ struct Implementation {
195
+ // / Intended for programmatic or logical use, but used as a display name in
196
+ // / past specs or fallback (if title isn’t present).
197
+ std::string name;
198
+
199
+ std::string version;
200
+
201
+ // / Intended for UI and end-user contexts — optimized to be human-readable and
202
+ // / easily understood, even by those unfamiliar with domain-specific
203
+ // / terminology.
204
+ // /
205
+ // / If not provided, the name should be used for display (except for Tool,
206
+ // / where annotations.title should be given precedence over using name, if
207
+ // / present).
208
+ std::string title = " " ;
209
+ };
210
+ llvm::json::Value toJSON (const Implementation &);
211
+ bool fromJSON (const llvm::json::Value &, Implementation &, llvm::json::Path);
212
+
213
+ // / Capabilities a client may support. Known capabilities are defined here, in
214
+ // / this schema, but this is not a closed set: any client can define its own,
215
+ // / additional capabilities.
216
+ struct ClientCapabilities {};
217
+ llvm::json::Value toJSON (const ClientCapabilities &);
218
+ bool fromJSON (const llvm::json::Value &, ClientCapabilities &,
219
+ llvm::json::Path);
220
+
221
+ // / Capabilities that a server may support. Known capabilities are defined here,
222
+ // / in this schema, but this is not a closed set: any server can define its own,
223
+ // / additional capabilities.
224
+ struct ServerCapabilities {
225
+ bool supportsToolsList = false ;
226
+ bool supportsResourcesList = false ;
227
+ bool supportsResourcesSubscribe = false ;
228
+
229
+ // / Utilities.
230
+ bool supportsCompletions = false ;
231
+ bool supportsLogging = false ;
232
+ };
233
+ llvm::json::Value toJSON (const ServerCapabilities &);
234
+ bool fromJSON (const llvm::json::Value &, ServerCapabilities &,
235
+ llvm::json::Path);
236
+
237
+ // / Initialization
238
+
239
+ // / This request is sent from the client to the server when it first connects,
240
+ // / asking it to begin initialization.
241
+ struct InitializeParams {
242
+ // / The latest version of the Model Context Protocol that the client supports.
243
+ // / The client MAY decide to support older versions as well.
244
+ std::string protocolVersion;
245
+
246
+ ClientCapabilities capabilities;
247
+
248
+ Implementation clientInfo;
249
+ };
250
+ llvm::json::Value toJSON (const InitializeParams &);
251
+ bool fromJSON (const llvm::json::Value &, InitializeParams &, llvm::json::Path);
252
+
253
+ // / After receiving an initialize request from the client, the server sends this
254
+ // / response.
255
+ struct InitializeResult {
256
+ // / The version of the Model Context Protocol that the server wants to use.
257
+ // / This may not match the version that the client requested. If the client
258
+ // / cannot support this version, it MUST disconnect.
259
+ std::string protocolVersion;
260
+
261
+ ServerCapabilities capabilities;
262
+ Implementation serverInfo;
263
+
264
+ // / Instructions describing how to use the server and its features.
265
+ // /
266
+ // / This can be used by clients to improve the LLM's understanding of
267
+ // / available tools, resources, etc. It can be thought of like a "hint" to the
268
+ // / model. For example, this information MAY be added to the system prompt.
269
+ std::string instructions = " " ;
270
+ };
271
+ llvm::json::Value toJSON (const InitializeResult &);
272
+ bool fromJSON (const llvm::json::Value &, InitializeResult &, llvm::json::Path);
273
+
274
+ // / Special case parameter or result that has no value.
275
+ using Void = std::monostate;
276
+ llvm::json::Value toJSON (const Void &);
277
+ bool fromJSON (const llvm::json::Value &, Void &, llvm::json::Path);
278
+
279
+ // / The server's response to a `tools/list` request from the client.
280
+ struct ListToolsResult {
281
+ std::vector<ToolDefinition> tools;
282
+ };
283
+ llvm::json::Value toJSON (const ListToolsResult &);
284
+ bool fromJSON (const llvm::json::Value &, ListToolsResult &, llvm::json::Path);
285
+
286
+ // / Supported content types, currently only TextContent, but the spec includes
287
+ // / additional content types.
288
+ using ContentBlock = TextContent;
289
+
290
+ // / Used by the client to invoke a tool provided by the server.
291
+ struct CallToolParams {
292
+ std::string name;
293
+ std::optional<llvm::json::Value> arguments;
294
+ };
295
+ llvm::json::Value toJSON (const CallToolParams &);
296
+ bool fromJSON (const llvm::json::Value &, CallToolParams &, llvm::json::Path);
297
+
298
+ // / The server’s response to a tool call.
299
+ struct CallToolResult {
300
+ // / A list of content objects that represent the unstructured result of the
301
+ // / tool call.
302
+ std::vector<ContentBlock> content;
303
+
304
+ // / Whether the tool call ended in an error.
305
+ // /
306
+ // / If not set, this is assumed to be false (the call was successful).
307
+ // /
308
+ // / Any errors that originate from the tool SHOULD be reported inside the
309
+ // / result object, with `isError` set to true, not as an MCP protocol-level
310
+ // / error response. Otherwise, the LLM would not be able to see that an error
311
+ // / occurred and self-correct.
312
+ // /
313
+ // / However, any errors in finding the tool, an error indicating that the
314
+ // / server does not support tool calls, or any other exceptional conditions,
315
+ // / should be reported as an MCP error response.
316
+ bool isError = false ;
317
+
318
+ // / An optional JSON object that represents the structured result of the tool
319
+ // / call.
320
+ std::optional<llvm::json::Value> structuredContent = std::nullopt;
321
+ };
322
+ llvm::json::Value toJSON (const CallToolResult &);
323
+ bool fromJSON (const llvm::json::Value &, CallToolResult &, llvm::json::Path);
324
+
207
325
} // namespace lldb_protocol::mcp
208
326
209
327
#endif
0 commit comments