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 "llvm/ADT/SmallString.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/Error.h"
21#include "llvm/Support/FormatVariadic.h"
22#include "llvm/Support/JSON.h"
23#include "llvm/Support/Signals.h"
24#include <memory>
25#include <string>
26#include <vector>
27
28namespace lldb_protocol::mcp {
29
30class Server {
31
32 using MCPTransportUP = std::unique_ptr<lldb_protocol::mcp::MCPTransport>;
33
35
36public:
37 Server(std::string name, std::string version, LogCallback log_callback = {});
38 ~Server() = default;
39
40 void AddTool(std::unique_ptr<Tool> tool);
41 void AddResourceProvider(std::unique_ptr<ResourceProvider> resource_provider);
42
44
45protected:
47
49
50 llvm::Expected<InitializeResult> InitializeHandler(const InitializeParams &);
51
52 llvm::Expected<ListToolsResult> ToolsListHandler();
53 llvm::Expected<CallToolResult> ToolsCallHandler(const CallToolParams &);
54
55 llvm::Expected<ListResourcesResult> ResourcesListHandler();
56 llvm::Expected<ReadResourceResult>
58
59 template <typename... Ts> inline auto Logv(const char *Fmt, Ts &&...Vals) {
60 Log(llvm::formatv(Fmt, std::forward<Ts>(Vals)...).str());
61 }
62 void Log(llvm::StringRef message) {
64 m_log_callback(message);
65 }
66
67private:
68 const std::string m_name;
69 const std::string m_version;
70
77 std::map<MCPTransport *, Client> m_instances;
78
79 llvm::StringMap<std::unique_ptr<Tool>> m_tools;
80 std::vector<std::unique_ptr<ResourceProvider>> m_resource_providers;
81};
82
84
85/// Information about this instance of lldb's MCP server for lldb-mcp to use to
86/// coordinate connecting an lldb-mcp client.
87struct ServerInfo {
88 std::string connection_uri;
89
90 /// Writes the server info into a unique file in `~/.lldb`.
91 static llvm::Expected<ServerInfoHandle> Write(const ServerInfo &);
92 /// Loads any server info saved in `~/.lldb`.
93 static llvm::Expected<std::vector<ServerInfo>> Load();
94};
95llvm::json::Value toJSON(const ServerInfo &);
96bool fromJSON(const llvm::json::Value &, ServerInfo &, llvm::json::Path);
97
98/// A handle that tracks the server info on disk and cleans up the disk record
99/// once it is no longer referenced.
101public:
102 explicit ServerInfoHandle(llvm::StringRef filename = "");
104
107
108 /// ServerIinfoHandle is not copyable.
109 /// @{
112 /// @}
113
114 /// Remove the file on disk, if one is tracked.
115 void Remove();
116
117private:
118 llvm::SmallString<128> m_filename;
119};
120
121} // namespace lldb_protocol::mcp
122
123#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:100
llvm::SmallString< 128 > m_filename
Definition Server.h:118
void Remove()
Remove the file on disk, if one is tracked.
Definition Server.cpp:42
ServerInfoHandle(const ServerInfoHandle &)=delete
ServerIinfoHandle is not copyable.
ServerInfoHandle & operator=(ServerInfoHandle &&other) noexcept
Definition Server.cpp:37
ServerInfoHandle(llvm::StringRef filename="")
ServerInfoHandle & operator=(const ServerInfoHandle &)=delete
const std::string m_version
Definition Server.h:69
const std::string m_name
Definition Server.h:68
void AddTool(std::unique_ptr< Tool > tool)
Definition Server.cpp:116
void AddResourceProvider(std::unique_ptr< ResourceProvider > resource_provider)
Definition Server.cpp:122
llvm::StringMap< std::unique_ptr< Tool > > m_tools
Definition Server.h:79
MCPBinderUP Bind(MCPTransport &)
Definition Server.cpp:129
std::vector< std::unique_ptr< ResourceProvider > > m_resource_providers
Definition Server.h:80
llvm::Expected< ReadResourceResult > ResourcesReadHandler(const ReadResourceParams &)
Definition Server.cpp:217
LogCallback m_log_callback
Definition Server.h:71
Server(std::string name, std::string version, LogCallback log_callback={})
Definition Server.cpp:112
llvm::Expected< ListToolsResult > ToolsListHandler()
Definition Server.cpp:177
void Log(llvm::StringRef message)
Definition Server.h:62
llvm::Expected< InitializeResult > InitializeHandler(const InitializeParams &)
Definition Server.cpp:168
llvm::Expected< CallToolResult > ToolsCallHandler(const CallToolParams &)
Definition Server.cpp:186
llvm::Error Accept(lldb_private::MainLoop &, MCPTransportUP)
Definition Server.cpp:146
std::unique_ptr< lldb_protocol::mcp::MCPTransport > MCPTransportUP
Definition Server.h:32
lldb_private::MainLoop::ReadHandleUP ReadHandleUP
Definition Server.h:34
llvm::Expected< ListResourcesResult > ResourcesListHandler()
Definition Server.cpp:206
auto Logv(const char *Fmt, Ts &&...Vals)
Definition Server.h:59
std::map< MCPTransport *, Client > m_instances
Definition Server.h:77
ServerCapabilities GetCapabilities()
Definition Server.cpp:241
MainLoopPosix MainLoop
Definition MainLoop.h:20
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:66
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:74
Used by the client to invoke a tool provided by the server.
Definition Protocol.h:292
Sent from the client to the server, to read a specific resource URI.
Definition Protocol.h:152
Capabilities that a server may support.
Definition Protocol.h:225
Information about this instance of lldb's MCP server for lldb-mcp to use to coordinate connecting an ...
Definition Server.h:87
static llvm::Expected< std::vector< ServerInfo > > Load()
Loads any server info saved in ~/.lldb.
Definition Server.cpp:86
static llvm::Expected< ServerInfoHandle > Write(const ServerInfo &)
Writes the server info into a unique file in ~/.lldb.
Definition Server.cpp:61