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
13#include "lldb/Host/MainLoop.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/Support/Error.h"
22#include "llvm/Support/JSON.h"
23#include "llvm/Support/Signals.h"
24#include <functional>
25#include <memory>
26#include <string>
27#include <vector>
28
29namespace lldb_protocol::mcp {
30
31class Server : public MCPTransport::MessageHandler {
32 using ClosedCallback = llvm::unique_function<void()>;
33
34public:
35 Server(std::string name, std::string version, MCPTransport &client,
36 LogCallback log_callback = {}, ClosedCallback closed_callback = {});
37 ~Server() = default;
38
39 using NotificationHandler = std::function<void(const Notification &)>;
40
41 void AddTool(std::unique_ptr<Tool> tool);
42 void AddResourceProvider(std::unique_ptr<ResourceProvider> resource_provider);
43 void AddNotificationHandler(llvm::StringRef method,
44 NotificationHandler handler);
45
46protected:
48
50 std::function<llvm::Expected<Response>(const Request &)>;
51
52 void AddRequestHandlers();
53
54 void AddRequestHandler(llvm::StringRef method, RequestHandler handler);
55
56 llvm::Expected<std::optional<Message>> HandleData(llvm::StringRef data);
57
58 llvm::Expected<Response> Handle(const Request &request);
59 void Handle(const Notification &notification);
60
61 llvm::Expected<Response> InitializeHandler(const Request &);
62
63 llvm::Expected<Response> ToolsListHandler(const Request &);
64 llvm::Expected<Response> ToolsCallHandler(const Request &);
65
66 llvm::Expected<Response> ResourcesListHandler(const Request &);
67 llvm::Expected<Response> ResourcesReadHandler(const Request &);
68
69 void Received(const Request &) override;
70 void Received(const Response &) override;
71 void Received(const Notification &) override;
72 void OnError(llvm::Error) override;
73 void OnClosed() override;
74
75protected:
76 void Log(llvm::StringRef);
77
78private:
79 const std::string m_name;
80 const std::string m_version;
81
85
86 llvm::StringMap<std::unique_ptr<Tool>> m_tools;
87 std::vector<std::unique_ptr<ResourceProvider>> m_resource_providers;
88
89 llvm::StringMap<RequestHandler> m_request_handlers;
90 llvm::StringMap<NotificationHandler> m_notification_handlers;
91};
92
94
95/// Information about this instance of lldb's MCP server for lldb-mcp to use to
96/// coordinate connecting an lldb-mcp client.
97struct ServerInfo {
98 std::string connection_uri;
99
100 /// Writes the server info into a unique file in `~/.lldb`.
101 static llvm::Expected<ServerInfoHandle> Write(const ServerInfo &);
102 /// Loads any server info saved in `~/.lldb`.
103 static llvm::Expected<std::vector<ServerInfo>> Load();
104};
105llvm::json::Value toJSON(const ServerInfo &);
106bool fromJSON(const llvm::json::Value &, ServerInfo &, llvm::json::Path);
107
108/// A handle that tracks the server info on disk and cleans up the disk record
109/// once it is no longer referenced.
111public:
112 explicit ServerInfoHandle(llvm::StringRef filename = "");
114
117
118 /// ServerIinfoHandle is not copyable.
119 /// @{
122 /// @}
123
124 /// Remove the file.
125 void Remove();
126
127private:
128 llvm::SmallString<128> m_filename;
129};
130
131} // namespace lldb_protocol::mcp
132
133#endif
A handle that tracks the server info on disk and cleans up the disk record once it is no longer refer...
Definition Server.h:110
llvm::SmallString< 128 > m_filename
Definition Server.h:128
void Remove()
Remove the file.
Definition Server.cpp:41
ServerInfoHandle(const ServerInfoHandle &)=delete
ServerIinfoHandle is not copyable.
ServerInfoHandle & operator=(ServerInfoHandle &&other) noexcept
Definition Server.cpp:36
ServerInfoHandle(llvm::StringRef filename="")
ServerInfoHandle & operator=(const ServerInfoHandle &)=delete
std::function< void(const Notification &)> NotificationHandler
Definition Server.h:39
llvm::Expected< Response > ToolsCallHandler(const Request &)
Definition Server.cpp:199
const std::string m_version
Definition Server.h:80
const std::string m_name
Definition Server.h:79
llvm::unique_function< void()> ClosedCallback
Definition Server.h:32
void AddTool(std::unique_ptr< Tool > tool)
Definition Server.cpp:154
void AddResourceProvider(std::unique_ptr< ResourceProvider > resource_provider)
Definition Server.cpp:160
llvm::StringMap< std::unique_ptr< Tool > > m_tools
Definition Server.h:86
llvm::Expected< std::optional< Message > > HandleData(llvm::StringRef data)
std::vector< std::unique_ptr< ResourceProvider > > m_resource_providers
Definition Server.h:87
llvm::StringMap< NotificationHandler > m_notification_handlers
Definition Server.h:90
LogCallback m_log_callback
Definition Server.h:83
Server(std::string name, std::string version, MCPTransport &client, LogCallback log_callback={}, ClosedCallback closed_callback={})
Definition Server.cpp:111
llvm::StringMap< RequestHandler > m_request_handlers
Definition Server.h:89
ClosedCallback m_closed_callback
Definition Server.h:84
void OnError(llvm::Error) override
Definition Server.cpp:326
llvm::Expected< Response > ResourcesListHandler(const Request &)
Definition Server.cpp:230
llvm::Expected< Response > ResourcesReadHandler(const Request &)
Definition Server.cpp:244
llvm::Expected< Response > Handle(const Request &request)
Definition Server.cpp:132
llvm::Expected< Response > ToolsListHandler(const Request &)
Definition Server.cpp:187
void Log(llvm::StringRef)
Definition Server.cpp:289
MCPTransport & m_client
Definition Server.h:82
void AddRequestHandler(llvm::StringRef method, RequestHandler handler)
Definition Server.cpp:167
std::function< llvm::Expected< Response >(const Request &)> RequestHandler
Definition Server.h:49
void Received(const Request &) override
Definition Server.cpp:294
void OnClosed() override
Definition Server.cpp:330
ServerCapabilities GetCapabilities()
Definition Server.cpp:280
void AddNotificationHandler(llvm::StringRef method, NotificationHandler handler)
Definition Server.cpp:171
llvm::Expected< Response > InitializeHandler(const Request &)
Definition Server.cpp:176
llvm::json::Value toJSON(const Request &)
Definition Protocol.cpp:66
lldb_private::Transport< Request, Response, Notification > MCPTransport
Generic transport that uses the MCP protocol.
Definition Transport.h:21
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:25
bool fromJSON(const llvm::json::Value &, Request &, llvm::json::Path)
Definition Protocol.cpp:74
A notification which does not expect a response.
Definition Protocol.h:88
A request that expects a response.
Definition Protocol.h:34
A response to a request, either an error or a result.
Definition Protocol.h:76
Capabilities that a server may support.
Definition Protocol.h:224
Information about this instance of lldb's MCP server for lldb-mcp to use to coordinate connecting an ...
Definition Server.h:97
static llvm::Expected< std::vector< ServerInfo > > Load()
Loads any server info saved in ~/.lldb.
Definition Server.cpp:85
static llvm::Expected< ServerInfoHandle > Write(const ServerInfo &)
Writes the server info into a unique file in ~/.lldb.
Definition Server.cpp:60