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 22 namespace lldb_private::mcp::protocol { 23 24 static llvm::StringLiteral kVersion = "2024-11-05"; 25 26 /// A request that expects a response. 27 struct Request { 28 uint64_t id = 0; 29 std::string method; 30 std::optional<llvm::json::Value> params; 31 }; 32 33 llvm::json::Value toJSON(const Request &); 34 bool fromJSON(const llvm::json::Value &, Request &, llvm::json::Path); 35 36 struct ErrorInfo { 37 int64_t code = 0; 38 std::string message; 39 std::string data; 40 }; 41 42 llvm::json::Value toJSON(const ErrorInfo &); 43 bool fromJSON(const llvm::json::Value &, ErrorInfo &, llvm::json::Path); 44 45 struct Error { 46 uint64_t id = 0; 47 ErrorInfo error; 48 }; 49 50 llvm::json::Value toJSON(const Error &); 51 bool fromJSON(const llvm::json::Value &, Error &, llvm::json::Path); 52 53 struct Response { 54 uint64_t id = 0; 55 std::optional<llvm::json::Value> result; 56 std::optional<ErrorInfo> error; 57 }; 58 59 llvm::json::Value toJSON(const Response &); 60 bool fromJSON(const llvm::json::Value &, Response &, llvm::json::Path); 61 62 /// A notification which does not expect a response. 63 struct Notification { 64 std::string method; 65 std::optional<llvm::json::Value> params; 66 }; 67 68 llvm::json::Value toJSON(const Notification &); 69 bool fromJSON(const llvm::json::Value &, Notification &, llvm::json::Path); 70 71 struct ToolCapability { 72 /// Whether this server supports notifications for changes to the tool list. 73 bool listChanged = false; 74 }; 75 76 llvm::json::Value toJSON(const ToolCapability &); 77 bool fromJSON(const llvm::json::Value &, ToolCapability &, llvm::json::Path); 78 79 struct 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 88 llvm::json::Value toJSON(const ResourceCapability &); 89 bool 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. 95 struct Capabilities { 96 /// Tool capabilities of the server. 97 ToolCapability tools; 98 99 /// Resource capabilities of the server. 100 ResourceCapability resources; 101 }; 102 103 llvm::json::Value toJSON(const Capabilities &); 104 bool fromJSON(const llvm::json::Value &, Capabilities &, llvm::json::Path); 105 106 /// A known resource that the server is capable of reading. 107 struct 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 121 llvm::json::Value toJSON(const Resource &); 122 bool fromJSON(const llvm::json::Value &, Resource &, llvm::json::Path); 123 124 /// The contents of a specific resource or sub-resource. 125 struct 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 137 llvm::json::Value toJSON(const ResourceContents &); 138 bool fromJSON(const llvm::json::Value &, ResourceContents &, llvm::json::Path); 139 140 /// The server's response to a resources/read request from the client. 141 struct ResourceResult { 142 std::vector<ResourceContents> contents; 143 }; 144 145 llvm::json::Value toJSON(const ResourceResult &); 146 bool fromJSON(const llvm::json::Value &, ResourceResult &, llvm::json::Path); 147 148 /// Text provided to or from an LLM. 149 struct TextContent { 150 /// The text content of the message. 151 std::string text; 152 }; 153 154 llvm::json::Value toJSON(const TextContent &); 155 bool fromJSON(const llvm::json::Value &, TextContent &, llvm::json::Path); 156 157 struct TextResult { 158 std::vector<TextContent> content; 159 bool isError = false; 160 }; 161 162 llvm::json::Value toJSON(const TextResult &); 163 bool fromJSON(const llvm::json::Value &, TextResult &, llvm::json::Path); 164 165 struct 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 176 llvm::json::Value toJSON(const ToolDefinition &); 177 bool fromJSON(const llvm::json::Value &, ToolDefinition &, llvm::json::Path); 178 179 using Message = std::variant<Request, Response, Notification, Error>; 180 181 bool fromJSON(const llvm::json::Value &, Message &, llvm::json::Path); 182 llvm::json::Value toJSON(const Message &); 183 184 using ToolArguments = std::variant<std::monostate, llvm::json::Value>; 185 186 } // namespace lldb_private::mcp::protocol 187 188 #endif 189