LLDB mainline
Protocol.h
Go to the documentation of this file.
1//===- Protocol.h ---------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains POD structs based on the MCP specification at
10// https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2024-11-05/schema.json
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLDB_PROTOCOL_MCP_PROTOCOL_H
15#define LLDB_PROTOCOL_MCP_PROTOCOL_H
16
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/JSON.h"
19#include <optional>
20#include <string>
21#include <variant>
22#include <vector>
23
24namespace lldb_protocol::mcp {
25
26static llvm::StringLiteral kProtocolVersion = "2024-11-05";
27
28/// Version reported by LLDB's MCP server. The function avoids having to include
29/// the version header.
30llvm::StringLiteral GetServerVersion();
31
32/// A Request or Response 'id'.
33///
34/// NOTE: This differs from the JSON-RPC 2.0 spec. The MCP spec says this must
35/// be a string or number, excluding a json 'null' as a valid id.
36using Id = std::variant<int64_t, std::string>;
37
38/// A request that expects a response.
39struct Request {
40 /// The request id.
41 Id id = 0;
42 /// The method to be invoked.
43 std::string method;
44 /// The method's params.
45 std::optional<llvm::json::Value> params;
46};
47llvm::json::Value toJSON(const Request &);
48bool fromJSON(const llvm::json::Value &, Request &, llvm::json::Path);
49bool operator==(const Request &, const Request &);
50
51enum ErrorCode : signed {
52 /// Invalid JSON was received by the server. An error occurred on the server
53 /// while parsing the JSON text.
55 /// The JSON sent is not a valid Request object.
57 /// The method does not exist / is not available.
59 /// Invalid method parameter(s).
61 /// Internal JSON-RPC error.
63};
64
65struct Error {
66 /// The error type that occurred.
67 int64_t code = 0;
68 /// A short description of the error. The message SHOULD be limited to a
69 /// concise single sentence.
70 std::string message;
71 /// Additional information about the error. The value of this member is
72 /// defined by the sender (e.g. detailed error information, nested errors
73 /// etc.).
74 std::optional<llvm::json::Value> data = std::nullopt;
75};
76llvm::json::Value toJSON(const Error &);
77bool fromJSON(const llvm::json::Value &, Error &, llvm::json::Path);
78bool operator==(const Error &, const Error &);
79
80/// A response to a request, either an error or a result.
81struct Response {
82 /// The request id.
83 Id id = 0;
84 /// The result of the request, either an Error or the JSON value of the
85 /// response.
86 std::variant<Error, llvm::json::Value> result;
87};
88llvm::json::Value toJSON(const Response &);
89bool fromJSON(const llvm::json::Value &, Response &, llvm::json::Path);
90bool operator==(const Response &, const Response &);
91
92/// A notification which does not expect a response.
94 /// The method to be invoked.
95 std::string method;
96 /// The notification's params.
97 std::optional<llvm::json::Value> params;
98};
99llvm::json::Value toJSON(const Notification &);
100bool fromJSON(const llvm::json::Value &, Notification &, llvm::json::Path);
101bool operator==(const Notification &, const Notification &);
102
103/// A general message as defined by the JSON-RPC 2.0 spec.
104using Message = std::variant<Request, Response, Notification>;
105// With clang-cl and MSVC STL 202208, convertible can be false later if we do
106// not force it to be checked early here.
107static_assert(std::is_convertible_v<Message, Message>,
108 "Message is not convertible to itself");
109bool fromJSON(const llvm::json::Value &, Message &, llvm::json::Path);
110llvm::json::Value toJSON(const Message &);
111
112/// A known resource that the server is capable of reading.
113struct Resource {
114 /// The URI of this resource.
115 std::string uri;
116
117 /// A human-readable name for this resource.
118 std::string name;
119
120 /// A description of what this resource represents.
121 std::string description = "";
122
123 /// The MIME type of this resource, if known.
124 std::string mimeType = "";
125};
126
127llvm::json::Value toJSON(const Resource &);
128bool fromJSON(const llvm::json::Value &, Resource &, llvm::json::Path);
129
130/// The server’s response to a resources/list request from the client.
132 std::vector<Resource> resources;
133};
134llvm::json::Value toJSON(const ListResourcesResult &);
135bool fromJSON(const llvm::json::Value &, ListResourcesResult &,
136 llvm::json::Path);
137
138/// The contents of a specific resource or sub-resource.
140 /// The URI of this resource.
141 std::string uri;
142
143 /// The text of the item. This must only be set if the item can actually be
144 /// represented as text (not binary data).
145 std::string text;
146
147 /// The MIME type of this resource, if known.
148 std::string mimeType;
149};
150
151llvm::json::Value toJSON(const TextResourceContents &);
152bool fromJSON(const llvm::json::Value &, TextResourceContents &,
153 llvm::json::Path);
154
155/// Sent from the client to the server, to read a specific resource URI.
157 /// The URI of the resource to read. The URI can use any protocol; it is up to
158 /// the server how to interpret it.
159 std::string uri;
160};
161llvm::json::Value toJSON(const ReadResourceParams &);
162bool fromJSON(const llvm::json::Value &, ReadResourceParams &,
163 llvm::json::Path);
164
165/// The server's response to a resources/read request from the client.
167 std::vector<TextResourceContents> contents;
168};
169llvm::json::Value toJSON(const ReadResourceResult &);
170bool fromJSON(const llvm::json::Value &, ReadResourceResult &,
171 llvm::json::Path);
172
173/// Text provided to or from an LLM.
175 /// The text content of the message.
176 std::string text;
177};
178llvm::json::Value toJSON(const TextContent &);
179bool fromJSON(const llvm::json::Value &, TextContent &, llvm::json::Path);
180
181/// Definition for a tool the client can call.
183 /// Unique identifier for the tool.
184 std::string name;
185
186 /// Human-readable description.
187 std::string description;
188
189 // JSON Schema for the tool's parameters.
190 std::optional<llvm::json::Value> inputSchema;
191};
192llvm::json::Value toJSON(const ToolDefinition &);
193bool fromJSON(const llvm::json::Value &, ToolDefinition &, llvm::json::Path);
194
195using ToolArguments = std::variant<std::monostate, llvm::json::Value>;
196
197/// Describes the name and version of an MCP implementation, with an optional
198/// title for UI representation.
200 /// Intended for programmatic or logical use, but used as a display name in
201 /// past specs or fallback (if title isn’t present).
202 std::string name;
203
204 std::string version;
205
206 /// Intended for UI and end-user contexts — optimized to be human-readable and
207 /// easily understood, even by those unfamiliar with domain-specific
208 /// terminology.
209 ///
210 /// If not provided, the name should be used for display (except for Tool,
211 /// where annotations.title should be given precedence over using name, if
212 /// present).
213 std::string title = "";
214};
215llvm::json::Value toJSON(const Implementation &);
216bool fromJSON(const llvm::json::Value &, Implementation &, llvm::json::Path);
217
218/// Capabilities a client may support. Known capabilities are defined here, in
219/// this schema, but this is not a closed set: any client can define its own,
220/// additional capabilities.
222llvm::json::Value toJSON(const ClientCapabilities &);
223bool fromJSON(const llvm::json::Value &, ClientCapabilities &,
224 llvm::json::Path);
225
226/// Capabilities that a server may support. Known capabilities are defined here,
227/// in this schema, but this is not a closed set: any server can define its own,
228/// additional capabilities.
230 bool supportsToolsList = false;
233
234 /// Utilities.
236 bool supportsLogging = false;
237};
238llvm::json::Value toJSON(const ServerCapabilities &);
239bool fromJSON(const llvm::json::Value &, ServerCapabilities &,
240 llvm::json::Path);
241
242/// Initialization
243
244/// This request is sent from the client to the server when it first connects,
245/// asking it to begin initialization.
247 /// The latest version of the Model Context Protocol that the client supports.
248 /// The client MAY decide to support older versions as well.
249 std::string protocolVersion;
250
252
254};
255llvm::json::Value toJSON(const InitializeParams &);
256bool fromJSON(const llvm::json::Value &, InitializeParams &, llvm::json::Path);
257
258/// After receiving an initialize request from the client, the server sends this
259/// response.
261 /// The version of the Model Context Protocol that the server wants to use.
262 /// This may not match the version that the client requested. If the client
263 /// cannot support this version, it MUST disconnect.
264 std::string protocolVersion;
265
268
269 /// Instructions describing how to use the server and its features.
270 ///
271 /// This can be used by clients to improve the LLM's understanding of
272 /// available tools, resources, etc. It can be thought of like a "hint" to the
273 /// model. For example, this information MAY be added to the system prompt.
274 std::string instructions = "";
275};
276llvm::json::Value toJSON(const InitializeResult &);
277bool fromJSON(const llvm::json::Value &, InitializeResult &, llvm::json::Path);
278
279/// Special case parameter or result that has no value.
280using Void = std::monostate;
281llvm::json::Value toJSON(const Void &);
282bool fromJSON(const llvm::json::Value &, Void &, llvm::json::Path);
283
284/// The server's response to a `tools/list` request from the client.
286 std::vector<ToolDefinition> tools;
287};
288llvm::json::Value toJSON(const ListToolsResult &);
289bool fromJSON(const llvm::json::Value &, ListToolsResult &, llvm::json::Path);
290
291/// Supported content types, currently only TextContent, but the spec includes
292/// additional content types.
294
295/// Used by the client to invoke a tool provided by the server.
297 std::string name;
298 std::optional<llvm::json::Value> arguments;
299};
300llvm::json::Value toJSON(const CallToolParams &);
301bool fromJSON(const llvm::json::Value &, CallToolParams &, llvm::json::Path);
302
303/// The server’s response to a tool call.
305 /// A list of content objects that represent the unstructured result of the
306 /// tool call.
307 std::vector<ContentBlock> content;
308
309 /// Whether the tool call ended in an error.
310 ///
311 /// If not set, this is assumed to be false (the call was successful).
312 ///
313 /// Any errors that originate from the tool SHOULD be reported inside the
314 /// result object, with `isError` set to true, not as an MCP protocol-level
315 /// error response. Otherwise, the LLM would not be able to see that an error
316 /// occurred and self-correct.
317 ///
318 /// However, any errors in finding the tool, an error indicating that the
319 /// server does not support tool calls, or any other exceptional conditions,
320 /// should be reported as an MCP error response.
321 bool isError = false;
322
323 /// An optional JSON object that represents the structured result of the tool
324 /// call.
325 std::optional<llvm::json::Value> structuredContent = std::nullopt;
326};
327llvm::json::Value toJSON(const CallToolResult &);
328bool fromJSON(const llvm::json::Value &, CallToolResult &, llvm::json::Path);
329
331MakeRequest(int64_t id, llvm::StringRef method,
332 std::optional<llvm::json::Value> params);
333
334} // namespace lldb_protocol::mcp
335
336#endif
std::variant< Request, Response, Notification > Message
A general message as defined by the JSON-RPC 2.0 spec.
Definition Protocol.h:104
std::monostate Void
Special case parameter or result that has no value.
Definition Protocol.h:280
std::variant< int64_t, std::string > Id
A Request or Response 'id'.
Definition Protocol.h:36
std::variant< std::monostate, llvm::json::Value > ToolArguments
Definition Protocol.h:195
@ eErrorCodeMethodNotFound
The method does not exist / is not available.
Definition Protocol.h:58
@ eErrorCodeInvalidParams
Invalid method parameter(s).
Definition Protocol.h:60
@ eErrorCodeInvalidRequest
The JSON sent is not a valid Request object.
Definition Protocol.h:56
@ eErrorCodeInternalError
Internal JSON-RPC error.
Definition Protocol.h:62
@ eErrorCodeParseError
Invalid JSON was received by the server.
Definition Protocol.h:54
TextContent ContentBlock
Supported content types, currently only TextContent, but the spec includes additional content types.
Definition Protocol.h:293
llvm::json::Value toJSON(const Request &)
Definition Protocol.cpp:69
lldb_protocol::mcp::Request MakeRequest(int64_t id, llvm::StringRef method, std::optional< llvm::json::Value > params)
llvm::StringLiteral GetServerVersion()
Version reported by LLDB's MCP server.
Definition Protocol.cpp:18
bool operator==(const Request &, const Request &)
Definition Protocol.cpp:83
static llvm::StringLiteral kProtocolVersion
Definition Protocol.h:26
bool fromJSON(const llvm::json::Value &, Request &, llvm::json::Path)
Definition Protocol.cpp:77
Used by the client to invoke a tool provided by the server.
Definition Protocol.h:296
std::optional< llvm::json::Value > arguments
Definition Protocol.h:298
The server’s response to a tool call.
Definition Protocol.h:304
std::vector< ContentBlock > content
A list of content objects that represent the unstructured result of the tool call.
Definition Protocol.h:307
std::optional< llvm::json::Value > structuredContent
An optional JSON object that represents the structured result of the tool call.
Definition Protocol.h:325
bool isError
Whether the tool call ended in an error.
Definition Protocol.h:321
Capabilities a client may support.
Definition Protocol.h:221
std::string message
A short description of the error.
Definition Protocol.h:70
std::optional< llvm::json::Value > data
Additional information about the error.
Definition Protocol.h:74
int64_t code
The error type that occurred.
Definition Protocol.h:67
Describes the name and version of an MCP implementation, with an optional title for UI representation...
Definition Protocol.h:199
std::string title
Intended for UI and end-user contexts — optimized to be human-readable and easily understood,...
Definition Protocol.h:213
std::string name
Intended for programmatic or logical use, but used as a display name in past specs or fallback (if ti...
Definition Protocol.h:202
std::string protocolVersion
The latest version of the Model Context Protocol that the client supports.
Definition Protocol.h:249
After receiving an initialize request from the client, the server sends this response.
Definition Protocol.h:260
std::string protocolVersion
The version of the Model Context Protocol that the server wants to use.
Definition Protocol.h:264
std::string instructions
Instructions describing how to use the server and its features.
Definition Protocol.h:274
The server’s response to a resources/list request from the client.
Definition Protocol.h:131
std::vector< Resource > resources
Definition Protocol.h:132
The server's response to a tools/list request from the client.
Definition Protocol.h:285
std::vector< ToolDefinition > tools
Definition Protocol.h:286
A notification which does not expect a response.
Definition Protocol.h:93
std::optional< llvm::json::Value > params
The notification's params.
Definition Protocol.h:97
std::string method
The method to be invoked.
Definition Protocol.h:95
Sent from the client to the server, to read a specific resource URI.
Definition Protocol.h:156
std::string uri
The URI of the resource to read.
Definition Protocol.h:159
The server's response to a resources/read request from the client.
Definition Protocol.h:166
std::vector< TextResourceContents > contents
Definition Protocol.h:167
A request that expects a response.
Definition Protocol.h:39
std::optional< llvm::json::Value > params
The method's params.
Definition Protocol.h:45
std::string method
The method to be invoked.
Definition Protocol.h:43
A known resource that the server is capable of reading.
Definition Protocol.h:113
std::string description
A description of what this resource represents.
Definition Protocol.h:121
std::string uri
The URI of this resource.
Definition Protocol.h:115
std::string mimeType
The MIME type of this resource, if known.
Definition Protocol.h:124
std::string name
A human-readable name for this resource.
Definition Protocol.h:118
A response to a request, either an error or a result.
Definition Protocol.h:81
std::variant< Error, llvm::json::Value > result
The result of the request, either an Error or the JSON value of the response.
Definition Protocol.h:86
Capabilities that a server may support.
Definition Protocol.h:229
Text provided to or from an LLM.
Definition Protocol.h:174
std::string text
The text content of the message.
Definition Protocol.h:176
The contents of a specific resource or sub-resource.
Definition Protocol.h:139
std::string text
The text of the item.
Definition Protocol.h:145
std::string uri
The URI of this resource.
Definition Protocol.h:141
std::string mimeType
The MIME type of this resource, if known.
Definition Protocol.h:148
Definition for a tool the client can call.
Definition Protocol.h:182
std::string description
Human-readable description.
Definition Protocol.h:187
std::string name
Unique identifier for the tool.
Definition Protocol.h:184
std::optional< llvm::json::Value > inputSchema
Definition Protocol.h:190