Skip to content

Commit 8469fb1

Browse files
JDevliegherekrishna2803
authored andcommitted
[lldb] Move the generic MCP code into Protocol/MCP (NFC) (llvm#152188)
This moves all the generic MCP code into the new ProtocolMCP library so it can be shared between the MCP implementation in LLDB (built on the private API) and lldb-mcp (built on the public API). This PR doesn't include the actual MCP server. The reason is that the transport mechanism will be different between the LLDB implementation (using sockets) and the lldb-mcp implementation (using stdio). The goal s to abstract away that difference by making the server use JSONTransport (again) but I'm saving that for a separate PR.
1 parent e6c24f2 commit 8469fb1

File tree

14 files changed

+141
-75
lines changed

14 files changed

+141
-75
lines changed

lldb/source/Plugins/Protocol/MCP/MCPError.h renamed to lldb/include/lldb/Protocol/MCP/MCPError.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
//===-- MCPError.h --------------------------------------------------------===//
1+
//===----------------------------------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#ifndef LLDB_PROTOCOL_MCP_MCPERROR_H
10+
#define LLDB_PROTOCOL_MCP_MCPERROR_H
11+
912
#include "lldb/Protocol/MCP/Protocol.h"
1013
#include "llvm/Support/Error.h"
11-
#include "llvm/Support/FormatVariadic.h"
1214
#include <string>
1315

14-
namespace lldb_private::mcp {
16+
namespace lldb_protocol::mcp {
1517

1618
class MCPError : public llvm::ErrorInfo<MCPError> {
1719
public:
@@ -47,4 +49,6 @@ class UnsupportedURI : public llvm::ErrorInfo<UnsupportedURI> {
4749
std::string m_uri;
4850
};
4951

50-
} // namespace lldb_private::mcp
52+
} // namespace lldb_protocol::mcp
53+
54+
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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_RESOURCE_H
10+
#define LLDB_PROTOCOL_MCP_RESOURCE_H
11+
12+
#include "lldb/Protocol/MCP/Protocol.h"
13+
#include <vector>
14+
15+
namespace lldb_protocol::mcp {
16+
17+
class ResourceProvider {
18+
public:
19+
ResourceProvider() = default;
20+
virtual ~ResourceProvider() = default;
21+
22+
virtual std::vector<lldb_protocol::mcp::Resource> GetResources() const = 0;
23+
virtual llvm::Expected<lldb_protocol::mcp::ResourceResult>
24+
ReadResource(llvm::StringRef uri) const = 0;
25+
};
26+
27+
} // namespace lldb_protocol::mcp
28+
29+
#endif

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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_TOOL_H
10+
#define LLDB_PROTOCOL_MCP_TOOL_H
11+
12+
#include "lldb/Protocol/MCP/Protocol.h"
13+
#include "llvm/Support/JSON.h"
14+
#include <string>
15+
16+
namespace lldb_protocol::mcp {
17+
18+
class Tool {
19+
public:
20+
Tool(std::string name, std::string description);
21+
virtual ~Tool() = default;
22+
23+
virtual llvm::Expected<lldb_protocol::mcp::TextResult>
24+
Call(const lldb_protocol::mcp::ToolArguments &args) = 0;
25+
26+
virtual std::optional<llvm::json::Value> GetSchema() const {
27+
return llvm::json::Object{{"type", "object"}};
28+
}
29+
30+
lldb_protocol::mcp::ToolDefinition GetDefinition() const;
31+
32+
const std::string &GetName() { return m_name; }
33+
34+
private:
35+
std::string m_name;
36+
std::string m_description;
37+
};
38+
39+
} // namespace lldb_protocol::mcp
40+
41+
#endif

lldb/source/Plugins/Protocol/MCP/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
add_lldb_library(lldbPluginProtocolServerMCP PLUGIN
2-
MCPError.cpp
32
ProtocolServerMCP.cpp
43
Resource.cpp
54
Tool.cpp

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "ProtocolServerMCP.h"
10-
#include "MCPError.h"
10+
#include "Resource.h"
11+
#include "Tool.h"
1112
#include "lldb/Core/PluginManager.h"
13+
#include "lldb/Protocol/MCP/MCPError.h"
14+
#include "lldb/Protocol/MCP/Tool.h"
1215
#include "lldb/Utility/LLDBLog.h"
1316
#include "lldb/Utility/Log.h"
1417
#include "llvm/ADT/StringExtras.h"
@@ -18,6 +21,7 @@
1821

1922
using namespace lldb_private;
2023
using namespace lldb_private::mcp;
24+
using namespace lldb_protocol::mcp;
2125
using namespace llvm;
2226

2327
LLDB_PLUGIN_DEFINE(ProtocolServerMCP)
@@ -112,7 +116,7 @@ void ProtocolServerMCP::AcceptCallback(std::unique_ptr<Socket> socket) {
112116
auto read_handle_up = m_loop.RegisterReadObject(
113117
io_sp,
114118
[this, client](MainLoopBase &loop) {
115-
if (Error error = ReadCallback(*client)) {
119+
if (llvm::Error error = ReadCallback(*client)) {
116120
LLDB_LOG_ERROR(GetLog(LLDBLog::Host), std::move(error), "{0}");
117121
client->read_handle_up.reset();
118122
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
#ifndef LLDB_PLUGINS_PROTOCOL_MCP_PROTOCOLSERVERMCP_H
1010
#define LLDB_PLUGINS_PROTOCOL_MCP_PROTOCOLSERVERMCP_H
1111

12-
#include "Resource.h"
13-
#include "Tool.h"
1412
#include "lldb/Core/ProtocolServer.h"
1513
#include "lldb/Host/MainLoop.h"
1614
#include "lldb/Host/Socket.h"
1715
#include "lldb/Protocol/MCP/Protocol.h"
16+
#include "lldb/Protocol/MCP/Resource.h"
17+
#include "lldb/Protocol/MCP/Tool.h"
1818
#include "llvm/ADT/StringMap.h"
1919
#include <thread>
2020

@@ -47,8 +47,9 @@ class ProtocolServerMCP : public ProtocolServer {
4747
using NotificationHandler =
4848
std::function<void(const lldb_protocol::mcp::Notification &)>;
4949

50-
void AddTool(std::unique_ptr<Tool> tool);
51-
void AddResourceProvider(std::unique_ptr<ResourceProvider> resource_provider);
50+
void AddTool(std::unique_ptr<lldb_protocol::mcp::Tool> tool);
51+
void AddResourceProvider(
52+
std::unique_ptr<lldb_protocol::mcp::ResourceProvider> resource_provider);
5253

5354
void AddRequestHandler(llvm::StringRef method, RequestHandler handler);
5455
void AddNotificationHandler(llvm::StringRef method,
@@ -99,8 +100,9 @@ class ProtocolServerMCP : public ProtocolServer {
99100
std::vector<std::unique_ptr<Client>> m_clients;
100101

101102
std::mutex m_server_mutex;
102-
llvm::StringMap<std::unique_ptr<Tool>> m_tools;
103-
std::vector<std::unique_ptr<ResourceProvider>> m_resource_providers;
103+
llvm::StringMap<std::unique_ptr<lldb_protocol::mcp::Tool>> m_tools;
104+
std::vector<std::unique_ptr<lldb_protocol::mcp::ResourceProvider>>
105+
m_resource_providers;
104106

105107
llvm::StringMap<RequestHandler> m_request_handlers;
106108
llvm::StringMap<NotificationHandler> m_notification_handlers;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
//===----------------------------------------------------------------------===//
66

77
#include "Resource.h"
8-
#include "MCPError.h"
98
#include "lldb/Core/Debugger.h"
109
#include "lldb/Core/Module.h"
10+
#include "lldb/Protocol/MCP/MCPError.h"
1111
#include "lldb/Target/Platform.h"
1212

1313
using namespace lldb_private;
1414
using namespace lldb_private::mcp;
15+
using namespace lldb_protocol::mcp;
1516

1617
namespace {
1718
struct DebuggerResource {

lldb/source/Plugins/Protocol/MCP/Resource.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,13 @@
1010
#define LLDB_PLUGINS_PROTOCOL_MCP_RESOURCE_H
1111

1212
#include "lldb/Protocol/MCP/Protocol.h"
13+
#include "lldb/Protocol/MCP/Resource.h"
1314
#include "lldb/lldb-private.h"
1415
#include <vector>
1516

1617
namespace lldb_private::mcp {
1718

18-
class ResourceProvider {
19-
public:
20-
ResourceProvider() = default;
21-
virtual ~ResourceProvider() = default;
22-
23-
virtual std::vector<lldb_protocol::mcp::Resource> GetResources() const = 0;
24-
virtual llvm::Expected<lldb_protocol::mcp::ResourceResult>
25-
ReadResource(llvm::StringRef uri) const = 0;
26-
};
27-
28-
class DebuggerResourceProvider : public ResourceProvider {
19+
class DebuggerResourceProvider : public lldb_protocol::mcp::ResourceProvider {
2920
public:
3021
using ResourceProvider::ResourceProvider;
3122
virtual ~DebuggerResourceProvider() = default;

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,6 @@ static lldb_protocol::mcp::TextResult createTextResult(std::string output,
4141

4242
} // namespace
4343

44-
Tool::Tool(std::string name, std::string description)
45-
: m_name(std::move(name)), m_description(std::move(description)) {}
46-
47-
lldb_protocol::mcp::ToolDefinition Tool::GetDefinition() const {
48-
lldb_protocol::mcp::ToolDefinition definition;
49-
definition.name = m_name;
50-
definition.description = m_description;
51-
52-
if (std::optional<llvm::json::Value> input_schema = GetSchema())
53-
definition.inputSchema = *input_schema;
54-
55-
return definition;
56-
}
57-
5844
llvm::Expected<lldb_protocol::mcp::TextResult>
5945
CommandTool::Call(const lldb_protocol::mcp::ToolArguments &args) {
6046
if (!std::holds_alternative<json::Value>(args))

lldb/source/Plugins/Protocol/MCP/Tool.h

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,15 @@
1111

1212
#include "lldb/Core/Debugger.h"
1313
#include "lldb/Protocol/MCP/Protocol.h"
14+
#include "lldb/Protocol/MCP/Tool.h"
1415
#include "llvm/Support/JSON.h"
1516
#include <string>
1617

1718
namespace lldb_private::mcp {
1819

19-
class Tool {
20+
class CommandTool : public lldb_protocol::mcp::Tool {
2021
public:
21-
Tool(std::string name, std::string description);
22-
virtual ~Tool() = default;
23-
24-
virtual llvm::Expected<lldb_protocol::mcp::TextResult>
25-
Call(const lldb_protocol::mcp::ToolArguments &args) = 0;
26-
27-
virtual std::optional<llvm::json::Value> GetSchema() const {
28-
return llvm::json::Object{{"type", "object"}};
29-
}
30-
31-
lldb_protocol::mcp::ToolDefinition GetDefinition() const;
32-
33-
const std::string &GetName() { return m_name; }
34-
35-
private:
36-
std::string m_name;
37-
std::string m_description;
38-
};
39-
40-
class CommandTool : public mcp::Tool {
41-
public:
42-
using mcp::Tool::Tool;
22+
using lldb_protocol::mcp::Tool::Tool;
4323
~CommandTool() = default;
4424

4525
virtual llvm::Expected<lldb_protocol::mcp::TextResult>

0 commit comments

Comments
 (0)