Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Protocol/MCP/Protocol.h
213845 views
//===- Protocol.h ---------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains POD structs based on the MCP specification at9// https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2024-11-05/schema.json10//11//===----------------------------------------------------------------------===//1213#ifndef LLDB_PLUGINS_PROTOCOL_MCP_PROTOCOL_H14#define LLDB_PLUGINS_PROTOCOL_MCP_PROTOCOL_H1516#include "llvm/Support/JSON.h"17#include <optional>18#include <string>19#include <variant>2021namespace lldb_private::mcp::protocol {2223static llvm::StringLiteral kVersion = "2024-11-05";2425/// A request that expects a response.26struct Request {27uint64_t id = 0;28std::string method;29std::optional<llvm::json::Value> params;30};3132llvm::json::Value toJSON(const Request &);33bool fromJSON(const llvm::json::Value &, Request &, llvm::json::Path);3435struct ErrorInfo {36int64_t code = 0;37std::string message;38std::string data;39};4041llvm::json::Value toJSON(const ErrorInfo &);42bool fromJSON(const llvm::json::Value &, ErrorInfo &, llvm::json::Path);4344struct Error {45uint64_t id = 0;46ErrorInfo error;47};4849llvm::json::Value toJSON(const Error &);50bool fromJSON(const llvm::json::Value &, Error &, llvm::json::Path);5152struct Response {53uint64_t id = 0;54std::optional<llvm::json::Value> result;55std::optional<ErrorInfo> error;56};5758llvm::json::Value toJSON(const Response &);59bool fromJSON(const llvm::json::Value &, Response &, llvm::json::Path);6061/// A notification which does not expect a response.62struct Notification {63std::string method;64std::optional<llvm::json::Value> params;65};6667llvm::json::Value toJSON(const Notification &);68bool fromJSON(const llvm::json::Value &, Notification &, llvm::json::Path);6970struct ToolCapability {71/// Whether this server supports notifications for changes to the tool list.72bool listChanged = false;73};7475llvm::json::Value toJSON(const ToolCapability &);76bool fromJSON(const llvm::json::Value &, ToolCapability &, llvm::json::Path);7778struct ResourceCapability {79/// Whether this server supports notifications for changes to the resources80/// list.81bool listChanged = false;8283/// Whether subscriptions are supported.84bool subscribe = false;85};8687llvm::json::Value toJSON(const ResourceCapability &);88bool fromJSON(const llvm::json::Value &, ResourceCapability &,89llvm::json::Path);9091/// Capabilities that a server may support. Known capabilities are defined here,92/// in this schema, but this is not a closed set: any server can define its own,93/// additional capabilities.94struct Capabilities {95/// Tool capabilities of the server.96ToolCapability tools;9798/// Resource capabilities of the server.99ResourceCapability resources;100};101102llvm::json::Value toJSON(const Capabilities &);103bool fromJSON(const llvm::json::Value &, Capabilities &, llvm::json::Path);104105/// A known resource that the server is capable of reading.106struct Resource {107/// The URI of this resource.108std::string uri;109110/// A human-readable name for this resource.111std::string name;112113/// A description of what this resource represents.114std::string description;115116/// The MIME type of this resource, if known.117std::string mimeType;118};119120llvm::json::Value toJSON(const Resource &);121bool fromJSON(const llvm::json::Value &, Resource &, llvm::json::Path);122123/// The contents of a specific resource or sub-resource.124struct ResourceContents {125/// The URI of this resource.126std::string uri;127128/// The text of the item. This must only be set if the item can actually be129/// represented as text (not binary data).130std::string text;131132/// The MIME type of this resource, if known.133std::string mimeType;134};135136llvm::json::Value toJSON(const ResourceContents &);137bool fromJSON(const llvm::json::Value &, ResourceContents &, llvm::json::Path);138139/// The server's response to a resources/read request from the client.140struct ResourceResult {141std::vector<ResourceContents> contents;142};143144llvm::json::Value toJSON(const ResourceResult &);145bool fromJSON(const llvm::json::Value &, ResourceResult &, llvm::json::Path);146147/// Text provided to or from an LLM.148struct TextContent {149/// The text content of the message.150std::string text;151};152153llvm::json::Value toJSON(const TextContent &);154bool fromJSON(const llvm::json::Value &, TextContent &, llvm::json::Path);155156struct TextResult {157std::vector<TextContent> content;158bool isError = false;159};160161llvm::json::Value toJSON(const TextResult &);162bool fromJSON(const llvm::json::Value &, TextResult &, llvm::json::Path);163164struct ToolDefinition {165/// Unique identifier for the tool.166std::string name;167168/// Human-readable description.169std::string description;170171// JSON Schema for the tool's parameters.172std::optional<llvm::json::Value> inputSchema;173};174175llvm::json::Value toJSON(const ToolDefinition &);176bool fromJSON(const llvm::json::Value &, ToolDefinition &, llvm::json::Path);177178using Message = std::variant<Request, Response, Notification, Error>;179180bool fromJSON(const llvm::json::Value &, Message &, llvm::json::Path);181llvm::json::Value toJSON(const Message &);182183using ToolArguments = std::variant<std::monostate, llvm::json::Value>;184185} // namespace lldb_private::mcp::protocol186187#endif188189190