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/Support/JSON.h"
18#include <optional>
19#include <string>
20#include <variant>
21#include <vector>
22
23namespace lldb_protocol::mcp {
24
25static llvm::StringLiteral kProtocolVersion = "2024-11-05";
26
27/// A Request or Response 'id'.
28///
29/// NOTE: This differs from the JSON-RPC 2.0 spec. The MCP spec says this must
30/// be a string or number, excluding a json 'null' as a valid id.
31using Id = std::variant<int64_t, std::string>;
32
33/// A request that expects a response.
34struct Request {
35 /// The request id.
36 Id id = 0;
37 /// The method to be invoked.
38 std::string method;
39 /// The method's params.
40 std::optional<llvm::json::Value> params;
41};
42llvm::json::Value toJSON(const Request &);
43bool fromJSON(const llvm::json::Value &, Request &, llvm::json::Path);
44bool operator==(const Request &, const Request &);
45
46enum ErrorCode : signed {
47 /// Invalid JSON was received by the server. An error occurred on the server
48 /// while parsing the JSON text.
50 /// The JSON sent is not a valid Request object.
52 /// The method does not exist / is not available.
54 /// Invalid method parameter(s).
56 /// Internal JSON-RPC error.
58};
59
60struct Error {
61 /// The error type that occurred.
62 int64_t code = 0;
63 /// A short description of the error. The message SHOULD be limited to a
64 /// concise single sentence.
65 std::string message;
66 /// Additional information about the error. The value of this member is
67 /// defined by the sender (e.g. detailed error information, nested errors
68 /// etc.).
69 std::optional<llvm::json::Value> data = std::nullopt;
70};
71llvm::json::Value toJSON(const Error &);
72bool fromJSON(const llvm::json::Value &, Error &, llvm::json::Path);
73bool operator==(const Error &, const Error &);
74
75/// A response to a request, either an error or a result.
76struct Response {
77 /// The request id.
78 Id id = 0;
79 /// The result of the request, either an Error or the JSON value of the
80 /// response.
81 std::variant<Error, llvm::json::Value> result;
82};
83llvm::json::Value toJSON(const Response &);
84bool fromJSON(const llvm::json::Value &, Response &, llvm::json::Path);
85bool operator==(const Response &, const Response &);
86
87/// A notification which does not expect a response.
89 /// The method to be invoked.
90 std::string method;
91 /// The notification's params.
92 std::optional<llvm::json::Value> params;
93};
94llvm::json::Value toJSON(const Notification &);
95bool fromJSON(const llvm::json::Value &, Notification &, llvm::json::Path);
96bool operator==(const Notification &, const Notification &);
97
98/// A general message as defined by the JSON-RPC 2.0 spec.
99using Message = std::variant<Request, Response, Notification>;
100// With clang-cl and MSVC STL 202208, convertible can be false later if we do
101// not force it to be checked early here.
102static_assert(std::is_convertible_v<Message, Message>,
103 "Message is not convertible to itself");
104bool fromJSON(const llvm::json::Value &, Message &, llvm::json::Path);
105llvm::json::Value toJSON(const Message &);
106
107/// A known resource that the server is capable of reading.
108struct Resource {
109 /// The URI of this resource.
110 std::string uri;
111
112 /// A human-readable name for this resource.
113 std::string name;
114
115 /// A description of what this resource represents.
116 std::string description = "";
117
118 /// The MIME type of this resource, if known.
119 std::string mimeType = "";
120};
121
122llvm::json::Value toJSON(const Resource &);
123bool fromJSON(const llvm::json::Value &, Resource &, llvm::json::Path);
124
125/// The server’s response to a resources/list request from the client.
127 std::vector<Resource> resources;
128};
129llvm::json::Value toJSON(const ListResourcesResult &);
130bool fromJSON(const llvm::json::Value &, ListResourcesResult &,
131 llvm::json::Path);
132
133/// The contents of a specific resource or sub-resource.
135 /// The URI of this resource.
136 std::string uri;
137
138 /// The text of the item. This must only be set if the item can actually be
139 /// represented as text (not binary data).
140 std::string text;
141
142 /// The MIME type of this resource, if known.
143 std::string mimeType;
144};
145
146llvm::json::Value toJSON(const TextResourceContents &);
147bool fromJSON(const llvm::json::Value &, TextResourceContents &,
148 llvm::json::Path);
149
150/// Sent from the client to the server, to read a specific resource URI.
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;
155};
156llvm::json::Value toJSON(const ReadResourceParams &);
157bool fromJSON(const llvm::json::Value &, ReadResourceParams &,
158 llvm::json::Path);
159
160/// The server's response to a resources/read request from the client.
162 std::vector<TextResourceContents> contents;
163};
164llvm::json::Value toJSON(const ReadResourceResult &);
165bool fromJSON(const llvm::json::Value &, ReadResourceResult &,
166 llvm::json::Path);
167
168/// Text provided to or from an LLM.
170 /// The text content of the message.
171 std::string text;
172};
173llvm::json::Value toJSON(const TextContent &);
174bool fromJSON(const llvm::json::Value &, TextContent &, llvm::json::Path);
175
176/// Definition for a tool the client can call.
178 /// Unique identifier for the tool.
179 std::string name;
180
181 /// Human-readable description.
182 std::string description;
183
184 // JSON Schema for the tool's parameters.
185 std::optional<llvm::json::Value> inputSchema;
186};
187llvm::json::Value toJSON(const ToolDefinition &);
188bool fromJSON(const llvm::json::Value &, ToolDefinition &, llvm::json::Path);
189
190using ToolArguments = std::variant<std::monostate, llvm::json::Value>;
191
192/// Describes the name and version of an MCP implementation, with an optional
193/// title for UI representation.
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};
210llvm::json::Value toJSON(const Implementation &);
211bool 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.
217llvm::json::Value toJSON(const ClientCapabilities &);
218bool 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.
225 bool supportsToolsList = false;
228
229 /// Utilities.
231 bool supportsLogging = false;
232};
233llvm::json::Value toJSON(const ServerCapabilities &);
234bool 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.
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
247
249};
250llvm::json::Value toJSON(const InitializeParams &);
251bool 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.
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
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};
271llvm::json::Value toJSON(const InitializeResult &);
272bool fromJSON(const llvm::json::Value &, InitializeResult &, llvm::json::Path);
273
274/// Special case parameter or result that has no value.
275using Void = std::monostate;
276llvm::json::Value toJSON(const Void &);
277bool fromJSON(const llvm::json::Value &, Void &, llvm::json::Path);
278
279/// The server's response to a `tools/list` request from the client.
281 std::vector<ToolDefinition> tools;
282};
283llvm::json::Value toJSON(const ListToolsResult &);
284bool 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.
289
290/// Used by the client to invoke a tool provided by the server.
292 std::string name;
293 std::optional<llvm::json::Value> arguments;
294};
295llvm::json::Value toJSON(const CallToolParams &);
296bool fromJSON(const llvm::json::Value &, CallToolParams &, llvm::json::Path);
297
298/// The server’s response to a tool call.
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};
322llvm::json::Value toJSON(const CallToolResult &);
323bool fromJSON(const llvm::json::Value &, CallToolResult &, llvm::json::Path);
324
325} // namespace lldb_protocol::mcp
326
327#endif
std::variant< Request, Response, Notification > Message
A general message as defined by the JSON-RPC 2.0 spec.
Definition Protocol.h:99
std::monostate Void
Special case parameter or result that has no value.
Definition Protocol.h:275
std::variant< int64_t, std::string > Id
A Request or Response 'id'.
Definition Protocol.h:31
std::variant< std::monostate, llvm::json::Value > ToolArguments
Definition Protocol.h:190
@ eErrorCodeMethodNotFound
The method does not exist / is not available.
Definition Protocol.h:53
@ eErrorCodeInvalidParams
Invalid method parameter(s).
Definition Protocol.h:55
@ eErrorCodeInvalidRequest
The JSON sent is not a valid Request object.
Definition Protocol.h:51
@ eErrorCodeInternalError
Internal JSON-RPC error.
Definition Protocol.h:57
@ eErrorCodeParseError
Invalid JSON was received by the server.
Definition Protocol.h:49
TextContent ContentBlock
Supported content types, currently only TextContent, but the spec includes additional content types.
Definition Protocol.h:288
llvm::json::Value toJSON(const Request &)
Definition Protocol.cpp:66
bool operator==(const Request &, const Request &)
Definition Protocol.cpp:80
static llvm::StringLiteral kProtocolVersion
Definition Protocol.h:25
bool fromJSON(const llvm::json::Value &, Request &, llvm::json::Path)
Definition Protocol.cpp:74
Used by the client to invoke a tool provided by the server.
Definition Protocol.h:291
std::optional< llvm::json::Value > arguments
Definition Protocol.h:293
The server’s response to a tool call.
Definition Protocol.h:299
std::vector< ContentBlock > content
A list of content objects that represent the unstructured result of the tool call.
Definition Protocol.h:302
std::optional< llvm::json::Value > structuredContent
An optional JSON object that represents the structured result of the tool call.
Definition Protocol.h:320
bool isError
Whether the tool call ended in an error.
Definition Protocol.h:316
Capabilities a client may support.
Definition Protocol.h:216
std::string message
A short description of the error.
Definition Protocol.h:65
std::optional< llvm::json::Value > data
Additional information about the error.
Definition Protocol.h:69
int64_t code
The error type that occurred.
Definition Protocol.h:62
Describes the name and version of an MCP implementation, with an optional title for UI representation...
Definition Protocol.h:194
std::string title
Intended for UI and end-user contexts — optimized to be human-readable and easily understood,...
Definition Protocol.h:208
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:197
std::string protocolVersion
The latest version of the Model Context Protocol that the client supports.
Definition Protocol.h:244
After receiving an initialize request from the client, the server sends this response.
Definition Protocol.h:255
std::string protocolVersion
The version of the Model Context Protocol that the server wants to use.
Definition Protocol.h:259
std::string instructions
Instructions describing how to use the server and its features.
Definition Protocol.h:269
The server’s response to a resources/list request from the client.
Definition Protocol.h:126
std::vector< Resource > resources
Definition Protocol.h:127
The server's response to a tools/list request from the client.
Definition Protocol.h:280
std::vector< ToolDefinition > tools
Definition Protocol.h:281
A notification which does not expect a response.
Definition Protocol.h:88
std::optional< llvm::json::Value > params
The notification's params.
Definition Protocol.h:92
std::string method
The method to be invoked.
Definition Protocol.h:90
Sent from the client to the server, to read a specific resource URI.
Definition Protocol.h:151
std::string uri
The URI of the resource to read.
Definition Protocol.h:154
The server's response to a resources/read request from the client.
Definition Protocol.h:161
std::vector< TextResourceContents > contents
Definition Protocol.h:162
A request that expects a response.
Definition Protocol.h:34
std::optional< llvm::json::Value > params
The method's params.
Definition Protocol.h:40
std::string method
The method to be invoked.
Definition Protocol.h:38
A known resource that the server is capable of reading.
Definition Protocol.h:108
std::string description
A description of what this resource represents.
Definition Protocol.h:116
std::string uri
The URI of this resource.
Definition Protocol.h:110
std::string mimeType
The MIME type of this resource, if known.
Definition Protocol.h:119
std::string name
A human-readable name for this resource.
Definition Protocol.h:113
A response to a request, either an error or a result.
Definition Protocol.h:76
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:81
Capabilities that a server may support.
Definition Protocol.h:224
Text provided to or from an LLM.
Definition Protocol.h:169
std::string text
The text content of the message.
Definition Protocol.h:171
The contents of a specific resource or sub-resource.
Definition Protocol.h:134
std::string text
The text of the item.
Definition Protocol.h:140
std::string uri
The URI of this resource.
Definition Protocol.h:136
std::string mimeType
The MIME type of this resource, if known.
Definition Protocol.h:143
Definition for a tool the client can call.
Definition Protocol.h:177
std::string description
Human-readable description.
Definition Protocol.h:182
std::string name
Unique identifier for the tool.
Definition Protocol.h:179
std::optional< llvm::json::Value > inputSchema
Definition Protocol.h:185