Skip to content

Commit 1bb8db5

Browse files
committed
Revert "[lldb] NFC Moving mcp::Transport into its own file. (#155711)"
This reverts commit 71a065e.
1 parent 7db1b2a commit 1bb8db5

File tree

9 files changed

+38
-90
lines changed

9 files changed

+38
-90
lines changed

lldb/include/lldb/Protocol/MCP/MCPError.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class MCPError : public llvm::ErrorInfo<MCPError> {
1919
public:
2020
static char ID;
2121

22-
MCPError(std::string message, int64_t error_code = eErrorCodeInternalError);
22+
MCPError(std::string message, int64_t error_code = kInternalError);
2323

2424
void log(llvm::raw_ostream &OS) const override;
2525
std::error_code convertToErrorCode() const override;
@@ -28,6 +28,9 @@ class MCPError : public llvm::ErrorInfo<MCPError> {
2828

2929
lldb_protocol::mcp::Error toProtocolError() const;
3030

31+
static constexpr int64_t kResourceNotFound = -32002;
32+
static constexpr int64_t kInternalError = -32603;
33+
3134
private:
3235
std::string m_message;
3336
int64_t m_error_code;

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ enum ErrorCode : signed {
5555
eErrorCodeInvalidParams = -32602,
5656
/// Internal JSON-RPC error.
5757
eErrorCodeInternalError = -32603,
58-
59-
/// Additional MCP error codes.
60-
61-
/// Resource related uri not found.
62-
eErrorCodeResourceNotFound = -32002,
6358
};
6459

6560
struct Error {

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,43 @@
99
#ifndef LLDB_PROTOCOL_MCP_SERVER_H
1010
#define LLDB_PROTOCOL_MCP_SERVER_H
1111

12+
#include "lldb/Host/JSONTransport.h"
1213
#include "lldb/Host/MainLoop.h"
1314
#include "lldb/Protocol/MCP/Protocol.h"
1415
#include "lldb/Protocol/MCP/Resource.h"
1516
#include "lldb/Protocol/MCP/Tool.h"
16-
#include "lldb/Protocol/MCP/Transport.h"
1717
#include "llvm/ADT/StringMap.h"
1818
#include "llvm/Support/Error.h"
19+
#include <mutex>
1920

2021
namespace lldb_protocol::mcp {
2122

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 {
2345
public:
2446
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);
2649
~Server() = default;
2750

2851
using NotificationHandler = std::function<void(const Notification &)>;
@@ -69,7 +92,7 @@ class Server : public Transport::MessageHandler {
6992
const std::string m_name;
7093
const std::string m_version;
7194

72-
std::unique_ptr<Transport> m_transport_up;
95+
std::unique_ptr<MCPTransport> m_transport_up;
7396
lldb_private::MainLoop &m_loop;
7497

7598
llvm::StringMap<std::unique_ptr<Tool>> m_tools;

lldb/include/lldb/Protocol/MCP/Transport.h

Lines changed: 0 additions & 39 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void ProtocolServerMCP::AcceptCallback(std::unique_ptr<Socket> socket) {
6767
LLDB_LOG(log, "New MCP client connected: {0}", client_name);
6868

6969
lldb::IOObjectSP io_sp = std::move(socket);
70-
auto transport_up = std::make_unique<lldb_protocol::mcp::Transport>(
70+
auto transport_up = std::make_unique<lldb_protocol::mcp::MCPTransport>(
7171
io_sp, io_sp, std::move(client_name), [&](llvm::StringRef message) {
7272
LLDB_LOG(GetLog(LLDBLog::Host), "{0}", message);
7373
});

lldb/source/Protocol/MCP/CMakeLists.txt

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

87
LINK_COMPONENTS
98
Support

lldb/source/Protocol/MCP/Server.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ using namespace lldb_protocol::mcp;
1515
using namespace llvm;
1616

1717
Server::Server(std::string name, std::string version,
18-
std::unique_ptr<Transport> transport_up,
18+
std::unique_ptr<MCPTransport> transport_up,
1919
lldb_private::MainLoop &loop)
2020
: m_name(std::move(name)), m_version(std::move(version)),
2121
m_transport_up(std::move(transport_up)), m_loop(loop) {
@@ -180,7 +180,7 @@ llvm::Expected<Response> Server::ResourcesReadHandler(const Request &request) {
180180

181181
return make_error<MCPError>(
182182
llvm::formatv("no resource handler for uri: {0}", uri_str).str(),
183-
eErrorCodeResourceNotFound);
183+
MCPError::kResourceNotFound);
184184
}
185185

186186
ServerCapabilities Server::GetCapabilities() {
@@ -219,7 +219,7 @@ void Server::Received(const Request &request) {
219219
response.takeError(),
220220
[&](const MCPError &err) { protocol_error = err.toProtocolError(); },
221221
[&](const llvm::ErrorInfoBase &err) {
222-
protocol_error.code = eErrorCodeInternalError;
222+
protocol_error.code = MCPError::kInternalError;
223223
protocol_error.message = err.message();
224224
});
225225
Response error_response;

lldb/source/Protocol/MCP/Transport.cpp

Lines changed: 0 additions & 32 deletions
This file was deleted.

lldb/unittests/Protocol/ProtocolMCPServerTest.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
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"
2524
#include "llvm/ADT/StringRef.h"
2625
#include "llvm/Support/Error.h"
2726
#include "llvm/Support/JSON.h"
@@ -37,12 +36,12 @@ using namespace lldb_private;
3736
using namespace lldb_protocol::mcp;
3837

3938
namespace {
40-
class TestMCPTransport final : public lldb_protocol::mcp::Transport {
39+
class TestMCPTransport final : public MCPTransport {
4140
public:
4241
TestMCPTransport(lldb::IOObjectSP in, lldb::IOObjectSP out)
43-
: lldb_protocol::mcp::Transport(in, out, "unittest") {}
42+
: lldb_protocol::mcp::MCPTransport(in, out, "unittest") {}
4443

45-
using Transport::Write;
44+
using MCPTransport::Write;
4645

4746
void Log(llvm::StringRef message) override {
4847
log_messages.emplace_back(message);

0 commit comments

Comments
 (0)