Skip to content

Commit 65f60fd

Browse files
authored
[lldb] Moving MCPTransport into its own file. (#156712)
Moving `lldb_protocol::mcp::MCPTransport` into its own file and renaming to `lldb_protocol::mcp::Transport`.
1 parent 55c2f27 commit 65f60fd

File tree

8 files changed

+96
-48
lines changed

8 files changed

+96
-48
lines changed

lldb/include/lldb/Host/JSONTransport.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ template <typename Req, typename Resp, typename Evt> class Transport {
100100
virtual llvm::Expected<MainLoop::ReadHandleUP>
101101
RegisterMessageHandler(MainLoop &loop, MessageHandler &handler) = 0;
102102

103-
protected:
103+
// FIXME: Refactor mcp::Server to not directly access log on the transport.
104+
// protected:
104105
template <typename... Ts> inline auto Logv(const char *Fmt, Ts &&...Vals) {
105106
Log(llvm::formatv(Fmt, std::forward<Ts>(Vals)...).str());
106107
}
@@ -139,16 +140,18 @@ class JSONTransport : public Transport<Req, Resp, Evt> {
139140
/// detail.
140141
static constexpr size_t kReadBufferSize = 1024;
141142

142-
protected:
143-
virtual llvm::Expected<std::vector<std::string>> Parse() = 0;
144-
virtual std::string Encode(const llvm::json::Value &message) = 0;
143+
// FIXME: Write should be protected.
145144
llvm::Error Write(const llvm::json::Value &message) {
146145
this->Logv("<-- {0}", message);
147146
std::string output = Encode(message);
148147
size_t bytes_written = output.size();
149148
return m_out->Write(output.data(), bytes_written).takeError();
150149
}
151150

151+
protected:
152+
virtual llvm::Expected<std::vector<std::string>> Parse() = 0;
153+
virtual std::string Encode(const llvm::json::Value &message) = 0;
154+
152155
llvm::SmallString<kReadBufferSize> m_buffer;
153156

154157
private:

lldb/include/lldb/Protocol/MCP/Server.h

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,17 @@
1414
#include "lldb/Protocol/MCP/Protocol.h"
1515
#include "lldb/Protocol/MCP/Resource.h"
1616
#include "lldb/Protocol/MCP/Tool.h"
17+
#include "lldb/Protocol/MCP/Transport.h"
1718
#include "llvm/ADT/StringMap.h"
1819
#include "llvm/Support/Error.h"
19-
#include <mutex>
20+
#include "llvm/Support/JSON.h"
21+
#include <functional>
22+
#include <memory>
23+
#include <string>
24+
#include <vector>
2025

2126
namespace lldb_protocol::mcp {
2227

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-
4428
/// Information about this instance of lldb's MCP server for lldb-mcp to use to
4529
/// coordinate connecting an lldb-mcp client.
4630
struct ServerInfo {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_TRANSPORT_H
10+
#define LLDB_PROTOCOL_MCP_TRANSPORT_H
11+
12+
#include "lldb/Host/JSONTransport.h"
13+
#include "lldb/Protocol/MCP/Protocol.h"
14+
#include "lldb/lldb-forward.h"
15+
#include "llvm/ADT/FunctionExtras.h"
16+
#include "llvm/ADT/StringRef.h"
17+
18+
namespace lldb_protocol::mcp {
19+
20+
/// Generic transport that uses the MCP protocol.
21+
using MCPTransport = lldb_private::Transport<Request, Response, Notification>;
22+
23+
/// Generic logging callback, to allow the MCP server / client / transport layer
24+
/// to be independent of the lldb log implementation.
25+
using LogCallback = llvm::unique_function<void(llvm::StringRef message)>;
26+
27+
class Transport final
28+
: public lldb_private::JSONRPCTransport<Request, Response, Notification> {
29+
public:
30+
Transport(lldb::IOObjectSP in, lldb::IOObjectSP out,
31+
LogCallback log_callback = {});
32+
virtual ~Transport() = default;
33+
34+
/// Transport is not copyable.
35+
/// @{
36+
Transport(const Transport &) = delete;
37+
void operator=(const Transport &) = delete;
38+
/// @}
39+
40+
void Log(llvm::StringRef message) override;
41+
42+
private:
43+
LogCallback m_log_callback;
44+
};
45+
46+
} // namespace lldb_protocol::mcp
47+
48+
#endif

lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ void ProtocolServerMCP::AcceptCallback(std::unique_ptr<Socket> socket) {
7070
LLDB_LOG(log, "New MCP client connected: {0}", client_name);
7171

7272
lldb::IOObjectSP io_sp = std::move(socket);
73-
auto transport_up = std::make_unique<lldb_protocol::mcp::MCPTransport>(
74-
io_sp, io_sp, std::move(client_name), [&](llvm::StringRef message) {
75-
LLDB_LOG(GetLog(LLDBLog::Host), "{0}", message);
73+
auto transport_up = std::make_unique<lldb_protocol::mcp::Transport>(
74+
io_sp, io_sp, [client_name](llvm::StringRef message) {
75+
LLDB_LOG(GetLog(LLDBLog::Host), "{0}: {1}", client_name, message);
7676
});
7777
auto instance_up = std::make_unique<lldb_protocol::mcp::Server>(
7878
std::string(kName), std::string(kVersion), std::move(transport_up),

lldb/source/Protocol/MCP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_lldb_library(lldbProtocolMCP NO_PLUGIN_DEPENDENCIES
33
Protocol.cpp
44
Server.cpp
55
Tool.cpp
6+
Transport.cpp
67

78
LINK_COMPONENTS
89
Support
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
#include "lldb/Protocol/MCP/Transport.h"
10+
#include "llvm/ADT/StringRef.h"
11+
#include <utility>
12+
13+
using namespace lldb_protocol::mcp;
14+
using namespace llvm;
15+
16+
Transport::Transport(lldb::IOObjectSP in, lldb::IOObjectSP out,
17+
LogCallback log_callback)
18+
: JSONRPCTransport(in, out), m_log_callback(std::move(log_callback)) {}
19+
20+
void Transport::Log(StringRef message) {
21+
if (m_log_callback)
22+
m_log_callback(message);
23+
}

lldb/tools/lldb-mcp/lldb-mcp.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ int main(int argc, char *argv[]) {
6767
[](MainLoopBase &loop) { loop.RequestTermination(); });
6868
});
6969

70-
auto transport_up = std::make_unique<lldb_protocol::mcp::MCPTransport>(
71-
input, output, std::string(client_name),
72-
[&](llvm::StringRef message) { llvm::errs() << message << '\n'; });
70+
auto transport_up = std::make_unique<lldb_protocol::mcp::Transport>(
71+
input, output, [&](llvm::StringRef message) {
72+
llvm::errs() << formatv("{0}: {1}", client_name, message) << '\n';
73+
});
7374

7475
auto instance_up = std::make_unique<lldb_protocol::mcp::Server>(
7576
std::string(kName), std::string(kVersion), std::move(transport_up), loop);

lldb/unittests/Protocol/ProtocolMCPServerTest.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "lldb/Protocol/MCP/Resource.h"
2222
#include "lldb/Protocol/MCP/Server.h"
2323
#include "lldb/Protocol/MCP/Tool.h"
24+
#include "lldb/Protocol/MCP/Transport.h"
2425
#include "llvm/ADT/StringRef.h"
2526
#include "llvm/Support/Error.h"
2627
#include "llvm/Support/JSON.h"
@@ -36,19 +37,6 @@ using namespace lldb_private;
3637
using namespace lldb_protocol::mcp;
3738

3839
namespace {
39-
class TestMCPTransport final : public MCPTransport {
40-
public:
41-
TestMCPTransport(lldb::IOObjectSP in, lldb::IOObjectSP out)
42-
: lldb_protocol::mcp::MCPTransport(in, out, "unittest") {}
43-
44-
using MCPTransport::Write;
45-
46-
void Log(llvm::StringRef message) override {
47-
log_messages.emplace_back(message);
48-
}
49-
50-
std::vector<std::string> log_messages;
51-
};
5240

5341
class TestServer : public Server {
5442
public:
@@ -134,7 +122,7 @@ class ProtocolServerMCPTest : public PipePairTest {
134122
public:
135123
SubsystemRAII<FileSystem, HostInfo, Socket> subsystems;
136124

137-
std::unique_ptr<TestMCPTransport> transport_up;
125+
std::unique_ptr<lldb_protocol::mcp::Transport> transport_up;
138126
std::unique_ptr<TestServer> server_up;
139127
MainLoop loop;
140128
MockMessageHandler<Request, Response, Notification> message_handler;
@@ -163,7 +151,7 @@ class ProtocolServerMCPTest : public PipePairTest {
163151
void SetUp() override {
164152
PipePairTest::SetUp();
165153

166-
transport_up = std::make_unique<TestMCPTransport>(
154+
transport_up = std::make_unique<lldb_protocol::mcp::Transport>(
167155
std::make_shared<NativeFile>(input.GetReadFileDescriptor(),
168156
File::eOpenOptionReadOnly,
169157
NativeFile::Unowned),
@@ -173,7 +161,7 @@ class ProtocolServerMCPTest : public PipePairTest {
173161

174162
server_up = std::make_unique<TestServer>(
175163
"lldb-mcp", "0.1.0",
176-
std::make_unique<TestMCPTransport>(
164+
std::make_unique<lldb_protocol::mcp::Transport>(
177165
std::make_shared<NativeFile>(output.GetReadFileDescriptor(),
178166
File::eOpenOptionReadOnly,
179167
NativeFile::Unowned),

0 commit comments

Comments
 (0)