Skip to content

Commit 71a065e

Browse files
authored
[lldb] NFC Moving mcp::Transport into its own file. (#155711)
Moving `lldb_protocol::mcp::MCPTransport` out of Server.h and into its own file and simplifying the name to `Transport`.
1 parent 6166fda commit 71a065e

File tree

9 files changed

+90
-38
lines changed

9 files changed

+90
-38
lines changed

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

Lines changed: 1 addition & 4 deletions
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 = kInternalError);
22+
MCPError(std::string message, int64_t error_code = eErrorCodeInternalError);
2323

2424
void log(llvm::raw_ostream &OS) const override;
2525
std::error_code convertToErrorCode() const override;
@@ -28,9 +28,6 @@ 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-
3431
private:
3532
std::string m_message;
3633
int64_t m_error_code;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ 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,
5863
};
5964

6065
struct Error {

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

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

12-
#include "lldb/Host/JSONTransport.h"
1312
#include "lldb/Host/MainLoop.h"
1413
#include "lldb/Protocol/MCP/Protocol.h"
1514
#include "lldb/Protocol/MCP/Resource.h"
1615
#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>
2019

2120
namespace lldb_protocol::mcp {
2221

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 {
22+
class Server : public Transport::MessageHandler {
4523
public:
4624
Server(std::string name, std::string version,
47-
std::unique_ptr<MCPTransport> transport_up,
48-
lldb_private::MainLoop &loop);
25+
std::unique_ptr<Transport> transport_up, lldb_private::MainLoop &loop);
4926
~Server() = default;
5027

5128
using NotificationHandler = std::function<void(const Notification &)>;
@@ -92,7 +69,7 @@ class Server : public MCPTransport::MessageHandler {
9269
const std::string m_name;
9370
const std::string m_version;
9471

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

9875
llvm::StringMap<std::unique_ptr<Tool>> m_tools;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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/StringRef.h"
16+
#include <functional>
17+
#include <string>
18+
19+
namespace lldb_protocol::mcp {
20+
21+
class Transport
22+
: public lldb_private::JSONRPCTransport<Request, Response, Notification> {
23+
public:
24+
using LogCallback = std::function<void(llvm::StringRef message)>;
25+
26+
Transport(lldb::IOObjectSP in, lldb::IOObjectSP out, std::string client_name,
27+
LogCallback log_callback = {});
28+
virtual ~Transport() = default;
29+
30+
void Log(llvm::StringRef message) override;
31+
32+
private:
33+
std::string m_client_name;
34+
LogCallback m_log_callback;
35+
};
36+
37+
} // namespace lldb_protocol::mcp
38+
39+
#endif

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::MCPTransport>(
70+
auto transport_up = std::make_unique<lldb_protocol::mcp::Transport>(
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: 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

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<MCPTransport> transport_up,
18+
std::unique_ptr<Transport> 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-
MCPError::kResourceNotFound);
183+
eErrorCodeResourceNotFound);
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 = MCPError::kInternalError;
222+
protocol_error.code = eErrorCodeInternalError;
223223
protocol_error.message = err.message();
224224
});
225225
Response error_response;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 "lldb/Host/JSONTransport.h"
11+
#include "lldb/lldb-forward.h"
12+
#include "llvm/ADT/StringRef.h"
13+
#include "llvm/Support/FormatVariadic.h"
14+
#include <string>
15+
#include <utility>
16+
17+
using namespace llvm;
18+
using namespace lldb;
19+
20+
namespace lldb_protocol::mcp {
21+
22+
Transport::Transport(IOObjectSP in, IOObjectSP out, std::string client_name,
23+
LogCallback log_callback)
24+
: JSONRPCTransport(in, out), m_client_name(std::move(client_name)),
25+
m_log_callback(log_callback) {}
26+
27+
void Transport::Log(StringRef message) {
28+
if (m_log_callback)
29+
m_log_callback(formatv("{0}: {1}", m_client_name, message).str());
30+
}
31+
32+
} // namespace lldb_protocol::mcp

lldb/unittests/Protocol/ProtocolMCPServerTest.cpp

Lines changed: 4 additions & 3 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,12 +37,12 @@ using namespace lldb_private;
3637
using namespace lldb_protocol::mcp;
3738

3839
namespace {
39-
class TestMCPTransport final : public MCPTransport {
40+
class TestMCPTransport final : public lldb_protocol::mcp::Transport {
4041
public:
4142
TestMCPTransport(lldb::IOObjectSP in, lldb::IOObjectSP out)
42-
: lldb_protocol::mcp::MCPTransport(in, out, "unittest") {}
43+
: lldb_protocol::mcp::Transport(in, out, "unittest") {}
4344

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

4647
void Log(llvm::StringRef message) override {
4748
log_messages.emplace_back(message);

0 commit comments

Comments
 (0)