|
9 | 9 | #ifndef LLDB_PROTOCOL_MCP_SERVER_H
|
10 | 10 | #define LLDB_PROTOCOL_MCP_SERVER_H
|
11 | 11 |
|
12 |
| -#include "lldb/Host/JSONTransport.h" |
13 | 12 | #include "lldb/Host/MainLoop.h"
|
14 | 13 | #include "lldb/Protocol/MCP/Protocol.h"
|
15 | 14 | #include "lldb/Protocol/MCP/Resource.h"
|
|
19 | 18 | #include "llvm/ADT/StringMap.h"
|
20 | 19 | #include "llvm/ADT/StringRef.h"
|
21 | 20 | #include "llvm/Support/Error.h"
|
| 21 | +#include "llvm/Support/FormatVariadic.h" |
22 | 22 | #include "llvm/Support/JSON.h"
|
23 | 23 | #include "llvm/Support/Signals.h"
|
24 |
| -#include <functional> |
25 | 24 | #include <memory>
|
26 | 25 | #include <string>
|
27 | 26 | #include <vector>
|
28 | 27 |
|
29 | 28 | namespace lldb_protocol::mcp {
|
30 | 29 |
|
31 |
| -class Server : public MCPTransport::MessageHandler { |
32 |
| - using ClosedCallback = llvm::unique_function<void()>; |
| 30 | +class Server { |
| 31 | + |
| 32 | + using MCPTransportUP = std::unique_ptr<lldb_protocol::mcp::MCPTransport>; |
| 33 | + |
| 34 | + using ReadHandleUP = lldb_private::MainLoop::ReadHandleUP; |
33 | 35 |
|
34 | 36 | public:
|
35 |
| - Server(std::string name, std::string version, MCPTransport &client, |
36 |
| - LogCallback log_callback = {}, ClosedCallback closed_callback = {}); |
| 37 | + Server(std::string name, std::string version, LogCallback log_callback = {}); |
37 | 38 | ~Server() = default;
|
38 | 39 |
|
39 |
| - using NotificationHandler = std::function<void(const Notification &)>; |
40 |
| - |
41 | 40 | void AddTool(std::unique_ptr<Tool> tool);
|
42 | 41 | void AddResourceProvider(std::unique_ptr<ResourceProvider> resource_provider);
|
43 |
| - void AddNotificationHandler(llvm::StringRef method, |
44 |
| - NotificationHandler handler); |
45 |
| - |
46 |
| -protected: |
47 |
| - ServerCapabilities GetCapabilities(); |
48 |
| - |
49 |
| - using RequestHandler = |
50 |
| - std::function<llvm::Expected<Response>(const Request &)>; |
51 | 42 |
|
52 |
| - void AddRequestHandlers(); |
| 43 | + llvm::Error Accept(lldb_private::MainLoop &, MCPTransportUP); |
53 | 44 |
|
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 ¬ification); |
| 45 | +protected: |
| 46 | + MCPBinderUP Bind(MCPTransport &); |
60 | 47 |
|
61 |
| - llvm::Expected<Response> InitializeHandler(const Request &); |
| 48 | + ServerCapabilities GetCapabilities(); |
62 | 49 |
|
63 |
| - llvm::Expected<Response> ToolsListHandler(const Request &); |
64 |
| - llvm::Expected<Response> ToolsCallHandler(const Request &); |
| 50 | + llvm::Expected<InitializeResult> InitializeHandler(const InitializeParams &); |
65 | 51 |
|
66 |
| - llvm::Expected<Response> ResourcesListHandler(const Request &); |
67 |
| - llvm::Expected<Response> ResourcesReadHandler(const Request &); |
| 52 | + llvm::Expected<ListToolsResult> ToolsListHandler(); |
| 53 | + llvm::Expected<CallToolResult> ToolsCallHandler(const CallToolParams &); |
68 | 54 |
|
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; |
| 55 | + llvm::Expected<ListResourcesResult> ResourcesListHandler(); |
| 56 | + llvm::Expected<ReadResourceResult> |
| 57 | + ResourcesReadHandler(const ReadResourceParams &); |
74 | 58 |
|
75 |
| -protected: |
76 |
| - void Log(llvm::StringRef); |
| 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) { |
| 63 | + if (m_log_callback) |
| 64 | + m_log_callback(message); |
| 65 | + } |
77 | 66 |
|
78 | 67 | private:
|
79 | 68 | const std::string m_name;
|
80 | 69 | const std::string m_version;
|
81 | 70 |
|
82 |
| - MCPTransport &m_client; |
83 | 71 | LogCallback m_log_callback;
|
84 |
| - ClosedCallback m_closed_callback; |
| 72 | + struct Client { |
| 73 | + ReadHandleUP handle; |
| 74 | + MCPTransportUP transport; |
| 75 | + MCPBinderUP binder; |
| 76 | + }; |
| 77 | + std::map<MCPTransport *, Client> m_instances; |
85 | 78 |
|
86 | 79 | llvm::StringMap<std::unique_ptr<Tool>> m_tools;
|
87 | 80 | 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 | 81 | };
|
92 | 82 |
|
93 | 83 | class ServerInfoHandle;
|
@@ -121,7 +111,7 @@ class ServerInfoHandle {
|
121 | 111 | ServerInfoHandle &operator=(const ServerInfoHandle &) = delete;
|
122 | 112 | /// @}
|
123 | 113 |
|
124 |
| - /// Remove the file. |
| 114 | + /// Remove the file on disk, if one is tracked. |
125 | 115 | void Remove();
|
126 | 116 |
|
127 | 117 | private:
|
|
0 commit comments