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 <map>
25#include <memory>
26#include <string>
27#include <vector>
28
29namespace lldb_protocol::mcp {
30
31class Server {
32
33 using MCPTransportUP = std::unique_ptr<lldb_protocol::mcp::MCPTransport>;
34
36
37public:
38 Server(std::string name, std::string version, LogCallback log_callback = {});
39 ~Server() = default;
40
41 void AddTool(std::unique_ptr<Tool> tool);
42 void AddResourceProvider(std::unique_ptr<ResourceProvider> resource_provider);
43
44 llvm::Error Accept(MCPTransportUP);
45
46protected:
48
50
51 llvm::Expected<InitializeResult> InitializeHandler(const InitializeParams &);
52
53 llvm::Expected<ListToolsResult> ToolsListHandler();
54 llvm::Expected<CallToolResult> ToolsCallHandler(const CallToolParams &);
55
56 llvm::Expected<ListResourcesResult> ResourcesListHandler();
57 llvm::Expected<ReadResourceResult>
59
60 template <typename... Ts> inline auto Logv(const char *Fmt, Ts &&...Vals) {
61 Log(llvm::formatv(Fmt, std::forward<Ts>(Vals)...).str());
62 }
63 void Log(llvm::StringRef message) {
65 m_log_callback(message);
66 }
67
68private:
69 const std::string m_name;
70 const std::string m_version;
71
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: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:70
const std::string m_name
Definition Server.h:69
void AddTool(std::unique_ptr< Tool > tool)
Definition Server.cpp:117
void AddResourceProvider(std::unique_ptr< ResourceProvider > resource_provider)
Definition Server.cpp:123
llvm::StringMap< std::unique_ptr< Tool > > m_tools
Definition Server.h:79
MCPBinderUP Bind(MCPTransport &)
Definition Server.cpp:130
std::vector< std::unique_ptr< ResourceProvider > > m_resource_providers
Definition Server.h:80
llvm::Expected< ReadResourceResult > ResourcesReadHandler(const ReadResourceParams &)
Definition Server.cpp:216
llvm::Error Accept(MCPTransportUP)
Definition Server.cpp:147
LogCallback m_log_callback
Definition Server.h:72
Server(std::string name, std::string version, LogCallback log_callback={})
Definition Server.cpp:113
llvm::Expected< ListToolsResult > ToolsListHandler()
Definition Server.cpp:176
void Log(llvm::StringRef message)
Definition Server.h:63
llvm::Expected< InitializeResult > InitializeHandler(const InitializeParams &)
Definition Server.cpp:167
llvm::Expected< CallToolResult > ToolsCallHandler(const CallToolParams &)
Definition Server.cpp:185
std::unique_ptr< lldb_protocol::mcp::MCPTransport > MCPTransportUP
Definition Server.h:33
lldb_private::MainLoop::ReadHandleUP ReadHandleUP
Definition Server.h:35
llvm::Expected< ListResourcesResult > ResourcesListHandler()
Definition Server.cpp:205
auto Logv(const char *Fmt, Ts &&...Vals)
Definition Server.h:60
std::map< MCPTransport *, Client > m_instances
Definition Server.h:77
ServerCapabilities GetCapabilities()
Definition Server.cpp:240
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:87
static llvm::Expected< ServerInfoHandle > Write(const ServerInfo &)
Writes the server info into a unique file in ~/.lldb.
Definition Server.cpp:62