LLDB mainline
Server.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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#ifndef LLDB_PROTOCOL_MCP_SERVER_H
10#define LLDB_PROTOCOL_MCP_SERVER_H
11
12#include "lldb/Host/MainLoop.h"
17#include "lldb/lldb-defines.h"
18#include "lldb/lldb-types.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/ADT/StringMap.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/FormatVariadic.h"
24#include "llvm/Support/JSON.h"
25#include "llvm/Support/Signals.h"
26#include <map>
27#include <memory>
28#include <string>
29#include <vector>
30
31namespace lldb_protocol::mcp {
32
33class Server {
34
35 using MCPTransportUP = std::unique_ptr<lldb_protocol::mcp::MCPTransport>;
36
38
39public:
40 Server(std::string name, std::string version, LogCallback log_callback = {});
41 ~Server() = default;
42
43 void AddTool(std::unique_ptr<Tool> tool);
44 void AddResourceProvider(std::unique_ptr<ResourceProvider> resource_provider);
45
46 llvm::Error Accept(MCPTransportUP);
47
48protected:
50
52
53 llvm::Expected<InitializeResult> InitializeHandler(const InitializeParams &);
54
55 llvm::Expected<ListToolsResult> ToolsListHandler();
56 llvm::Expected<CallToolResult> ToolsCallHandler(const CallToolParams &);
57
58 llvm::Expected<ListResourcesResult> ResourcesListHandler();
59 llvm::Expected<ReadResourceResult>
61
62 template <typename... Ts> inline auto Logv(const char *Fmt, Ts &&...Vals) {
63 Log(llvm::formatv(Fmt, std::forward<Ts>(Vals)...).str());
64 }
65 void Log(llvm::StringRef message) {
67 m_log_callback(message);
68 }
69
70private:
71 const std::string m_name;
72 const std::string m_version;
73
79 std::map<MCPTransport *, Client> m_instances;
80
81 llvm::StringMap<std::unique_ptr<Tool>> m_tools;
82 std::vector<std::unique_ptr<ResourceProvider>> m_resource_providers;
83};
84
86
87/// Information about this instance of lldb's MCP server for lldb-mcp to use to
88/// coordinate connecting an lldb-mcp client.
89struct ServerInfo {
90 std::string connection_uri;
91 /// Process id of the lldb instance hosting this server, used as its stable
92 /// identity.
94
95 /// Writes the server info into a unique file in `~/.lldb`.
96 static llvm::Expected<ServerInfoHandle> Write(const ServerInfo &);
97 /// Loads any server info saved in `~/.lldb`.
98 static llvm::Expected<std::vector<ServerInfo>> Load();
99 /// Removes the registry entry for the instance with the given \p pid. The
100 /// handle removes it on a clean exit, so this only prunes one orphaned by a
101 /// crash.
102 static llvm::Error Remove(lldb::pid_t pid);
103};
104llvm::json::Value toJSON(const ServerInfo &);
105bool fromJSON(const llvm::json::Value &, ServerInfo &, llvm::json::Path);
106
107/// A handle that tracks the server info on disk and cleans up the disk record
108/// once it is no longer referenced.
110public:
111 explicit ServerInfoHandle(llvm::StringRef filename = "");
113
116
117 /// ServerIinfoHandle is not copyable.
118 /// @{
121 /// @}
122
123 /// Remove the file on disk, if one is tracked.
124 void Remove();
125
126private:
127 llvm::SmallString<128> m_filename;
128};
129
130} // namespace lldb_protocol::mcp
131
132#endif
std::unique_ptr< ReadHandle > ReadHandleUP
A handle that tracks the server info on disk and cleans up the disk record once it is no longer refer...
Definition Server.h:109
llvm::SmallString< 128 > m_filename
Definition Server.h:127
void Remove()
Remove the file on disk, if one is tracked.
Definition Server.cpp:43
ServerInfoHandle(const ServerInfoHandle &)=delete
ServerIinfoHandle is not copyable.
ServerInfoHandle & operator=(ServerInfoHandle &&other) noexcept
Definition Server.cpp:38
ServerInfoHandle(llvm::StringRef filename="")
ServerInfoHandle & operator=(const ServerInfoHandle &)=delete
const std::string m_version
Definition Server.h:72
const std::string m_name
Definition Server.h:71
void AddTool(std::unique_ptr< Tool > tool)
Definition Server.cpp:137
void AddResourceProvider(std::unique_ptr< ResourceProvider > resource_provider)
Definition Server.cpp:143
llvm::StringMap< std::unique_ptr< Tool > > m_tools
Definition Server.h:81
MCPBinderUP Bind(MCPTransport &)
Definition Server.cpp:150
std::vector< std::unique_ptr< ResourceProvider > > m_resource_providers
Definition Server.h:82
llvm::Expected< ReadResourceResult > ResourcesReadHandler(const ReadResourceParams &)
Definition Server.cpp:236
llvm::Error Accept(MCPTransportUP)
Definition Server.cpp:167
LogCallback m_log_callback
Definition Server.h:74
Server(std::string name, std::string version, LogCallback log_callback={})
Definition Server.cpp:133
llvm::Expected< ListToolsResult > ToolsListHandler()
Definition Server.cpp:196
void Log(llvm::StringRef message)
Definition Server.h:65
llvm::Expected< InitializeResult > InitializeHandler(const InitializeParams &)
Definition Server.cpp:187
llvm::Expected< CallToolResult > ToolsCallHandler(const CallToolParams &)
Definition Server.cpp:205
std::unique_ptr< lldb_protocol::mcp::MCPTransport > MCPTransportUP
Definition Server.h:35
lldb_private::MainLoop::ReadHandleUP ReadHandleUP
Definition Server.h:37
llvm::Expected< ListResourcesResult > ResourcesListHandler()
Definition Server.cpp:225
auto Logv(const char *Fmt, Ts &&...Vals)
Definition Server.h:62
std::map< MCPTransport *, Client > m_instances
Definition Server.h:79
ServerCapabilities GetCapabilities()
Definition Server.cpp:260
#define LLDB_INVALID_PROCESS_ID
std::unique_ptr< MCPBinder > MCPBinderUP
Definition Transport.h:77
lldb_private::transport::JSONTransport< ProtocolDescriptor > MCPTransport
Generic transport that uses the MCP protocol.
Definition Transport.h:75
llvm::json::Value toJSON(const Request &)
Definition Protocol.cpp:69
llvm::unique_function< void(llvm::StringRef message)> LogCallback
Generic logging callback, to allow the MCP server / client / transport layer to be independent of the...
Definition Transport.h:81
bool fromJSON(const llvm::json::Value &, Request &, llvm::json::Path)
Definition Protocol.cpp:77
uint64_t pid_t
Definition lldb-types.h:83
Used by the client to invoke a tool provided by the server.
Definition Protocol.h:296
Sent from the client to the server, to read a specific resource URI.
Definition Protocol.h:156
Capabilities that a server may support.
Definition Protocol.h:229
Information about this instance of lldb's MCP server for lldb-mcp to use to coordinate connecting an ...
Definition Server.h:89
static llvm::Expected< std::vector< ServerInfo > > Load()
Loads any server info saved in ~/.lldb.
Definition Server.cpp:107
lldb::pid_t pid
Process id of the lldb instance hosting this server, used as its stable identity.
Definition Server.h:93
static llvm::Error Remove(lldb::pid_t pid)
Removes the registry entry for the instance with the given pid.
Definition Server.cpp:98
static llvm::Expected< ServerInfoHandle > Write(const ServerInfo &)
Writes the server info into a unique file in ~/.lldb.
Definition Server.cpp:69