|
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 | + |
| 31 | +class Server { |
| 32 | + |
| 33 | + using MCPTransportUP = std::unique_ptr<lldb_protocol::mcp::MCPTransport>; |
| 34 | + |
| 35 | + using ReadHandleUP = lldb_private::MainLoop::ReadHandleUP; |
33 | 36 |
|
34 | 37 | public:
|
35 |
| - Server(std::string name, std::string version, MCPTransport &client, |
36 |
| - LogCallback log_callback = {}, ClosedCallback closed_callback = {}); |
| 38 | + Server(std::string name, std::string version, LogCallback log_callback = {}); |
37 | 39 | ~Server() = default;
|
38 | 40 |
|
39 |
| - using NotificationHandler = std::function<void(const Notification &)>; |
40 |
| - |
41 | 41 | void AddTool(std::unique_ptr<Tool> tool);
|
42 | 42 | void AddResourceProvider(std::unique_ptr<ResourceProvider> resource_provider);
|
43 |
| - void AddNotificationHandler(llvm::StringRef method, |
44 |
| - NotificationHandler handler); |
45 |
| - |
46 |
| -protected: |
47 |
| - ServerCapabilities GetCapabilities(); |
48 | 43 |
|
49 |
| - using RequestHandler = |
50 |
| - std::function<llvm::Expected<Response>(const Request &)>; |
| 44 | + llvm::Error Accept(lldb_private::MainLoop &, MCPTransportUP); |
51 | 45 |
|
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 ¬ification); |
| 46 | +protected: |
| 47 | + MCPTransport::BinderUP Bind(MCPTransport &); |
60 | 48 |
|
61 |
| - llvm::Expected<Response> InitializeHandler(const Request &); |
| 49 | + ServerCapabilities GetCapabilities(); |
62 | 50 |
|
63 |
| - llvm::Expected<Response> ToolsListHandler(const Request &); |
64 |
| - llvm::Expected<Response> ToolsCallHandler(const Request &); |
| 51 | + llvm::Expected<InitializeResult> InitializeHandler(const InitializeParams &); |
65 | 52 |
|
66 |
| - llvm::Expected<Response> ResourcesListHandler(const Request &); |
67 |
| - llvm::Expected<Response> ResourcesReadHandler(const Request &); |
| 53 | + llvm::Expected<ListToolsResult> ToolsListHandler(); |
| 54 | + llvm::Expected<CallToolResult> ToolsCallHandler(const CallToolParams &); |
68 | 55 |
|
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; |
| 56 | + llvm::Expected<ListResourcesResult> ResourcesListHandler(); |
| 57 | + llvm::Expected<ReadResourceResult> |
| 58 | + ResourcesReadHandler(const ReadResourceParams &); |
74 | 59 |
|
75 |
| -protected: |
76 |
| - void Log(llvm::StringRef); |
| 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) { |
| 64 | + if (m_log_callback) |
| 65 | + m_log_callback(message); |
| 66 | + } |
77 | 67 |
|
78 | 68 | private:
|
79 | 69 | const std::string m_name;
|
80 | 70 | const std::string m_version;
|
81 | 71 |
|
82 |
| - MCPTransport &m_client; |
83 | 72 | LogCallback m_log_callback;
|
84 |
| - ClosedCallback m_closed_callback; |
| 73 | + struct Client { |
| 74 | + ReadHandleUP handle; |
| 75 | + MCPTransportUP transport; |
| 76 | + MCPTransport::BinderUP binder; |
| 77 | + }; |
| 78 | + std::map<MCPTransport *, Client> m_instances; |
85 | 79 |
|
86 | 80 | llvm::StringMap<std::unique_ptr<Tool>> m_tools;
|
87 | 81 | 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 | 82 | };
|
92 | 83 |
|
93 | 84 | class ServerInfoHandle;
|
@@ -121,7 +112,7 @@ class ServerInfoHandle {
|
121 | 112 | ServerInfoHandle &operator=(const ServerInfoHandle &) = delete;
|
122 | 113 | /// @}
|
123 | 114 |
|
124 |
| - /// Remove the file. |
| 115 | + /// Remove the file on disk, if one is tracked. |
125 | 116 | void Remove();
|
126 | 117 |
|
127 | 118 | private:
|
|
0 commit comments