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_PLUGINS_PROTOCOL_MCP_PROTOCOL_H
15#define LLDB_PLUGINS_PROTOCOL_MCP_PROTOCOL_H
16
17#include "llvm/Support/JSON.h"
18#include <optional>
19#include <string>
20#include <variant>
21
22namespace lldb_private::mcp::protocol {
23
24static llvm::StringLiteral kVersion = "2024-11-05";
25
26/// A request that expects a response.
27struct Request {
28 uint64_t id = 0;
29 std::string method;
30 std::optional<llvm::json::Value> params;
31};
32
33llvm::json::Value toJSON(const Request &);
34bool fromJSON(const llvm::json::Value &, Request &, llvm::json::Path);
35
36struct ErrorInfo {
37 int64_t code = 0;
38 std::string message;
39 std::string data;
40};
41
42llvm::json::Value toJSON(const ErrorInfo &);
43bool fromJSON(const llvm::json::Value &, ErrorInfo &, llvm::json::Path);
44
45struct Error {
46 uint64_t id = 0;
47 ErrorInfo error;
48};
49
50llvm::json::Value toJSON(const Error &);
51bool fromJSON(const llvm::json::Value &, Error &, llvm::json::Path);
52
53struct Response {
54 uint64_t id = 0;
55 std::optional<llvm::json::Value> result;
56 std::optional<ErrorInfo> error;
57};
58
59llvm::json::Value toJSON(const Response &);
60bool fromJSON(const llvm::json::Value &, Response &, llvm::json::Path);
61
62/// A notification which does not expect a response.
63struct Notification {
64 std::string method;
65 std::optional<llvm::json::Value> params;
66};
67
68llvm::json::Value toJSON(const Notification &);
69bool fromJSON(const llvm::json::Value &, Notification &, llvm::json::Path);
70
71struct ToolCapability {
72 /// Whether this server supports notifications for changes to the tool list.
73 bool listChanged = false;
74};
75
76llvm::json::Value toJSON(const ToolCapability &);
77bool fromJSON(const llvm::json::Value &, ToolCapability &, llvm::json::Path);
78
79struct ResourceCapability {
80 /// Whether this server supports notifications for changes to the resources
81 /// list.
82 bool listChanged = false;
83
84 /// Whether subscriptions are supported.
85 bool subscribe = false;
86};
87
88llvm::json::Value toJSON(const ResourceCapability &);
89bool fromJSON(const llvm::json::Value &, ResourceCapability &,
90 llvm::json::Path);
91
92/// Capabilities that a server may support. Known capabilities are defined here,
93/// in this schema, but this is not a closed set: any server can define its own,
94/// additional capabilities.
95struct Capabilities {
96 /// Tool capabilities of the server.
97 ToolCapability tools;
98
99 /// Resource capabilities of the server.
100 ResourceCapability resources;
101};
102
103llvm::json::Value toJSON(const Capabilities &);
104bool fromJSON(const llvm::json::Value &, Capabilities &, llvm::json::Path);
105
106/// A known resource that the server is capable of reading.
107struct Resource {
108 /// The URI of this resource.
109 std::string uri;
110
111 /// A human-readable name for this resource.
112 std::string name;
113
114 /// A description of what this resource represents.
115 std::string description;
116
117 /// The MIME type of this resource, if known.
118 std::string mimeType;
119};
120
121llvm::json::Value toJSON(const Resource &);
122bool fromJSON(const llvm::json::Value &, Resource &, llvm::json::Path);
123
124/// The contents of a specific resource or sub-resource.
125struct ResourceContents {
126 /// The URI of this resource.
127 std::string uri;
128
129 /// The text of the item. This must only be set if the item can actually be
130 /// represented as text (not binary data).
131 std::string text;
132
133 /// The MIME type of this resource, if known.
134 std::string mimeType;
135};
136
137llvm::json::Value toJSON(const ResourceContents &);
138bool fromJSON(const llvm::json::Value &, ResourceContents &, llvm::json::Path);
139
140/// The server's response to a resources/read request from the client.
141struct ResourceResult {
142 std::vector<ResourceContents> contents;
143};
144
145llvm::json::Value toJSON(const ResourceResult &);
146bool fromJSON(const llvm::json::Value &, ResourceResult &, llvm::json::Path);
147
148/// Text provided to or from an LLM.
149struct TextContent {
150 /// The text content of the message.
151 std::string text;
152};
153
154llvm::json::Value toJSON(const TextContent &);
155bool fromJSON(const llvm::json::Value &, TextContent &, llvm::json::Path);
156
157struct TextResult {
158 std::vector<TextContent> content;
159 bool isError = false;
160};
161
162llvm::json::Value toJSON(const TextResult &);
163bool fromJSON(const llvm::json::Value &, TextResult &, llvm::json::Path);
164
165struct ToolDefinition {
166 /// Unique identifier for the tool.
167 std::string name;
168
169 /// Human-readable description.
170 std::string description;
171
172 // JSON Schema for the tool's parameters.
173 std::optional<llvm::json::Value> inputSchema;
174};
175
176llvm::json::Value toJSON(const ToolDefinition &);
177bool fromJSON(const llvm::json::Value &, ToolDefinition &, llvm::json::Path);
178
179using Message = std::variant<Request, Response, Notification, Error>;
180
181bool fromJSON(const llvm::json::Value &, Message &, llvm::json::Path);
182llvm::json::Value toJSON(const Message &);
183
184using ToolArguments = std::variant<std::monostate, llvm::json::Value>;
185
186} // namespace lldb_private::mcp::protocol
187
188#endif
189

source code of lldb/source/Plugins/Protocol/MCP/Protocol.h