10#include "lldb/Version/Version.inc"
11#include "llvm/Support/ErrorHandling.h"
12#include "llvm/Support/JSON.h"
20static bool mapRaw(
const json::Value &Params, StringLiteral Prop,
21 std::optional<json::Value> &V, json::Path P) {
22 const auto *O = Params.getAsObject();
24 P.report(
"expected object");
27 const json::Value *E = O->get(Prop);
34 if (
const int64_t *I = std::get_if<int64_t>(&
Id))
35 return json::Value(*I);
36 if (
const std::string *S = std::get_if<std::string>(&
Id))
37 return json::Value(*S);
38 llvm_unreachable(
"unexpected type in protocol::Id");
41static bool mapId(
const llvm::json::Value &V, StringLiteral Prop,
Id &
Id,
43 const auto *O = V.getAsObject();
45 P.report(
"expected object");
49 const auto *E = O->get(Prop);
51 P.field(Prop).report(
"not found");
55 if (
auto S = E->getAsString()) {
60 if (
auto I = E->getAsInteger()) {
65 P.report(
"expected string or number");
73 Result.insert({
"params", R.
params});
78 llvm::json::ObjectMapper O(V, P);
79 return O &&
mapId(V,
"id", R.
id, P) && O.map(
"method", R.
method) &&
88 llvm::json::Object Result{{
"code", E.
code}, {
"message", E.
message}};
90 Result.insert({
"data", *E.
data});
94bool fromJSON(
const llvm::json::Value &V,
Error &E, llvm::json::Path P) {
95 llvm::json::ObjectMapper O(V, P);
96 return O && O.map(
"code", E.
code) && O.map(
"message", E.
message) &&
105 llvm::json::Object Result{{
"jsonrpc",
"2.0"}, {
"id",
toJSON(R.
id)}};
108 Result.insert({
"error", *
error});
109 if (
const json::Value *result = std::get_if<json::Value>(&R.
result))
110 Result.insert({
"result", *result});
115 const json::Object *E = V.getAsObject();
117 P.report(
"expected object");
121 const json::Value *result = E->get(
"result");
122 const json::Value *raw_error = E->get(
"error");
124 if (result && raw_error) {
125 P.report(
"'result' and 'error' fields are mutually exclusive");
129 if (!result && !raw_error) {
130 P.report(
"'result' or 'error' fields are required'");
135 R.
result = std::move(*result);
143 return mapId(V,
"id", R.
id, P);
151 llvm::json::Object Result{{
"jsonrpc",
"2.0"}, {
"method", N.method}};
153 Result.insert({
"params", N.params});
158 llvm::json::ObjectMapper O(V, P);
159 if (!O || !O.map(
"method", N.method))
161 auto *Obj = V.getAsObject();
164 if (
auto *Params = Obj->get(
"params"))
174 llvm::json::ObjectMapper O(V, P);
175 return O && O.map(
"uri", R.
uri) && O.map(
"name", R.
name) &&
177 O.mapOptional(
"mimeType", R.
mimeType);
181 llvm::json::Object Result{{
"uri", R.
uri}, {
"name", R.
name}};
185 Result.insert({
"mimeType", R.
mimeType});
190 llvm::json::Object Result{{
"uri", RC.
uri}, {
"text", RC.
text}};
192 Result.insert({
"mimeType", RC.
mimeType});
197 llvm::json::Path P) {
198 llvm::json::ObjectMapper O(V, P);
199 return O && O.map(
"uri", RC.
uri) && O.map(
"text", RC.
text) &&
200 O.mapOptional(
"mimeType", RC.
mimeType);
204 return llvm::json::Object{{
"contents", RR.
contents}};
208 llvm::json::Path P) {
209 llvm::json::ObjectMapper O(V, P);
210 return O && O.map(
"contents", RR.
contents);
214 return llvm::json::Object{{
"type",
"text"}, {
"text", TC.
text}};
218 llvm::json::ObjectMapper O(V, P);
219 return O && O.map(
"text", TC.
text);
223 llvm::json::Object Result{{
"name", TD.
name}};
232 llvm::json::Path P) {
234 llvm::json::ObjectMapper O(V, P);
235 if (!O || !O.map(
"name", TD.
name) ||
242 return std::visit([](
auto &M) {
return toJSON(M); }, M);
246 const auto *O = V.getAsObject();
248 P.report(
"expected object");
252 if (
const json::Value *V = O->get(
"jsonrpc")) {
253 if (V->getAsString().value_or(
"") !=
"2.0") {
254 P.report(
"unsupported JSON RPC version");
258 P.report(
"not a valid JSON RPC message");
271 if (O->get(
"method")) {
279 if (O->get(
"result") || O->get(
"error")) {
287 P.report(
"unrecognized message type");
292 json::Object result{{
"name", I.
name}, {
"version", I.
version}};
294 if (!I.
title.empty())
295 result.insert({
"title", I.
title});
301 json::ObjectMapper O(V, P);
302 return O && O.map(
"name", I.
name) && O.mapOptional(
"title", I.
title) &&
303 O.mapOptional(
"version", I.
version);
313 json::Object result{};
316 result.insert({
"tools", json::Object{{
"listChanged",
true}}});
319 json::Object resources;
321 resources.insert({
"listChanged",
true});
323 resources.insert({
"subscribe",
true});
324 result.insert({
"resources", std::move(resources)});
328 result.insert({
"completions", json::Object{}});
331 result.insert({
"logging", json::Object{}});
337 const json::Object *O = V.getAsObject();
339 P.report(
"expected object");
343 if (O->find(
"tools") != O->end())
346 if (
const json::Object *resources = O->getObject(
"resources")) {
348 resources->getBoolean(
"listChanged").value_or(
false);
350 resources->getBoolean(
"subscribe").value_or(
false);
353 if (O->find(
"completions") != O->end())
356 if (O->find(
"logging") != O->end())
371 json::ObjectMapper O(V, P);
389 json::ObjectMapper O(V, P);
397 return json::Object{{
"tools", R.
tools}};
401 json::ObjectMapper O(V, P);
402 return O && O.map(
"tools", R.
tools);
406 json::Object result{{
"content", R.
content}};
409 result.insert({
"isError", R.
isError});
417 json::ObjectMapper O(V, P);
418 return O && O.map(
"content", R.
content) &&
419 O.mapOptional(
"isError", R.
isError) &&
424 json::Object result{{
"name", R.
name}};
427 result.insert({
"arguments", *R.
arguments});
433 json::ObjectMapper O(V, P);
438 return json::Object{{
"uri", R.
uri}};
442 json::ObjectMapper O(V, P);
443 return O && O.map(
"uri", R.
uri);
447 return json::Object{{
"resources", R.
resources}};
451 json::ObjectMapper O(V, P);
452 return O && O.map(
"resources", R.
resources);
457bool fromJSON(
const json::Value &V,
Void &R, json::Path P) {
return true; }
static llvm::raw_ostream & error(Stream &strm)
std::variant< Request, Response, Notification > Message
A general message as defined by the JSON-RPC 2.0 spec.
std::monostate Void
Special case parameter or result that has no value.
std::variant< int64_t, std::string > Id
A Request or Response 'id'.
llvm::json::Value toJSON(const Request &)
llvm::StringLiteral GetServerVersion()
Version reported by LLDB's MCP server.
bool operator==(const Request &, const Request &)
static bool mapRaw(const json::Value &Params, StringLiteral Prop, std::optional< json::Value > &V, json::Path P)
static bool mapId(const llvm::json::Value &V, StringLiteral Prop, Id &Id, llvm::json::Path P)
bool fromJSON(const llvm::json::Value &, Request &, llvm::json::Path)
Capabilities a client may support.
std::string message
A short description of the error.
std::optional< llvm::json::Value > data
Additional information about the error.
int64_t code
The error type that occurred.
Describes the name and version of an MCP implementation, with an optional title for UI representation...
std::string title
Intended for UI and end-user contexts — optimized to be human-readable and easily understood,...
std::string name
Intended for programmatic or logical use, but used as a display name in past specs or fallback (if ti...
Implementation clientInfo
std::string protocolVersion
The latest version of the Model Context Protocol that the client supports.
ClientCapabilities capabilities
After receiving an initialize request from the client, the server sends this response.
std::string protocolVersion
The version of the Model Context Protocol that the server wants to use.
std::string instructions
Instructions describing how to use the server and its features.
ServerCapabilities capabilities
Implementation serverInfo
The server’s response to a resources/list request from the client.
std::vector< Resource > resources
A notification which does not expect a response.
std::optional< llvm::json::Value > params
The notification's params.
std::string method
The method to be invoked.
Sent from the client to the server, to read a specific resource URI.
std::string uri
The URI of the resource to read.
The server's response to a resources/read request from the client.
std::vector< TextResourceContents > contents
A request that expects a response.
std::optional< llvm::json::Value > params
The method's params.
std::string method
The method to be invoked.
A known resource that the server is capable of reading.
std::string description
A description of what this resource represents.
std::string uri
The URI of this resource.
std::string mimeType
The MIME type of this resource, if known.
std::string name
A human-readable name for this resource.
A response to a request, either an error or a result.
std::variant< Error, llvm::json::Value > result
The result of the request, either an Error or the JSON value of the response.
Capabilities that a server may support.
bool supportsCompletions
Utilities.
bool supportsResourcesList
bool supportsResourcesSubscribe
Text provided to or from an LLM.
std::string text
The text content of the message.
The contents of a specific resource or sub-resource.
std::string text
The text of the item.
std::string uri
The URI of this resource.
std::string mimeType
The MIME type of this resource, if known.