|
9 | 9 | #ifndef LLDB_PROTOCOL_MCP_SERVER_H |
10 | 10 | #define LLDB_PROTOCOL_MCP_SERVER_H |
11 | 11 |
|
| 12 | +#include "lldb/Host/JSONTransport.h" |
12 | 13 | #include "lldb/Host/MainLoop.h" |
13 | 14 | #include "lldb/Protocol/MCP/Protocol.h" |
14 | 15 | #include "lldb/Protocol/MCP/Resource.h" |
15 | 16 | #include "lldb/Protocol/MCP/Tool.h" |
16 | | -#include "lldb/Protocol/MCP/Transport.h" |
17 | 17 | #include "llvm/ADT/StringMap.h" |
18 | 18 | #include "llvm/Support/Error.h" |
| 19 | +#include <mutex> |
19 | 20 |
|
20 | 21 | namespace lldb_protocol::mcp { |
21 | 22 |
|
22 | | -class Server : public Transport::MessageHandler { |
| 23 | +class MCPTransport |
| 24 | + : public lldb_private::JSONRPCTransport<Request, Response, Notification> { |
| 25 | +public: |
| 26 | + using LogCallback = std::function<void(llvm::StringRef message)>; |
| 27 | + |
| 28 | + MCPTransport(lldb::IOObjectSP in, lldb::IOObjectSP out, |
| 29 | + std::string client_name, LogCallback log_callback = {}) |
| 30 | + : JSONRPCTransport(in, out), m_client_name(std::move(client_name)), |
| 31 | + m_log_callback(log_callback) {} |
| 32 | + virtual ~MCPTransport() = default; |
| 33 | + |
| 34 | + void Log(llvm::StringRef message) override { |
| 35 | + if (m_log_callback) |
| 36 | + m_log_callback(llvm::formatv("{0}: {1}", m_client_name, message).str()); |
| 37 | + } |
| 38 | + |
| 39 | +private: |
| 40 | + std::string m_client_name; |
| 41 | + LogCallback m_log_callback; |
| 42 | +}; |
| 43 | + |
| 44 | +class Server : public MCPTransport::MessageHandler { |
23 | 45 | public: |
24 | 46 | Server(std::string name, std::string version, |
25 | | - std::unique_ptr<Transport> transport_up, lldb_private::MainLoop &loop); |
| 47 | + std::unique_ptr<MCPTransport> transport_up, |
| 48 | + lldb_private::MainLoop &loop); |
26 | 49 | ~Server() = default; |
27 | 50 |
|
28 | 51 | using NotificationHandler = std::function<void(const Notification &)>; |
@@ -69,7 +92,7 @@ class Server : public Transport::MessageHandler { |
69 | 92 | const std::string m_name; |
70 | 93 | const std::string m_version; |
71 | 94 |
|
72 | | - std::unique_ptr<Transport> m_transport_up; |
| 95 | + std::unique_ptr<MCPTransport> m_transport_up; |
73 | 96 | lldb_private::MainLoop &m_loop; |
74 | 97 |
|
75 | 98 | llvm::StringMap<std::unique_ptr<Tool>> m_tools; |
|
0 commit comments