Skip to content

Commit 8b89bfa

Browse files
committed
[lldb] Adding structured types for existing MCP calls.
This adds or renames existing types to match the names of the types on https://modelcontextprotocol.io/specification/2025-06-18/schema for the existing calls. The new types are used in the unit tests and server implementation to remove the need for crafting various `llvm::json::Object` values by hand.
1 parent 72c04bb commit 8b89bfa

File tree

12 files changed

+548
-257
lines changed

12 files changed

+548
-257
lines changed

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

Lines changed: 227 additions & 61 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ResourceProvider {
2020
virtual ~ResourceProvider() = default;
2121

2222
virtual std::vector<lldb_protocol::mcp::Resource> GetResources() const = 0;
23-
virtual llvm::Expected<lldb_protocol::mcp::ResourceResult>
23+
virtual llvm::Expected<lldb_protocol::mcp::ReadResourceResult>
2424
ReadResource(llvm::StringRef uri) const = 0;
2525
};
2626

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Server : public MCPTransport::MessageHandler {
5858
llvm::Error Run();
5959

6060
protected:
61-
Capabilities GetCapabilities();
61+
ServerCapabilities GetCapabilities();
6262

6363
using RequestHandler =
6464
std::function<llvm::Expected<Response>(const Request &)>;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLDB_PROTOCOL_MCP_TOOL_H
1111

1212
#include "lldb/Protocol/MCP/Protocol.h"
13+
#include "llvm/Support/Error.h"
1314
#include "llvm/Support/JSON.h"
1415
#include <string>
1516

@@ -20,7 +21,7 @@ class Tool {
2021
Tool(std::string name, std::string description);
2122
virtual ~Tool() = default;
2223

23-
virtual llvm::Expected<lldb_protocol::mcp::TextResult>
24+
virtual llvm::Expected<lldb_protocol::mcp::CallToolResult>
2425
Call(const lldb_protocol::mcp::ToolArguments &args) = 0;
2526

2627
virtual std::optional<llvm::json::Value> GetSchema() const {

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "lldb/Core/Debugger.h"
99
#include "lldb/Core/Module.h"
1010
#include "lldb/Protocol/MCP/MCPError.h"
11-
#include "lldb/Target/Platform.h"
1211

1312
using namespace lldb_private;
1413
using namespace lldb_private::mcp;
@@ -124,7 +123,7 @@ DebuggerResourceProvider::GetResources() const {
124123
return resources;
125124
}
126125

127-
llvm::Expected<lldb_protocol::mcp::ResourceResult>
126+
llvm::Expected<lldb_protocol::mcp::ReadResourceResult>
128127
DebuggerResourceProvider::ReadResource(llvm::StringRef uri) const {
129128

130129
auto [protocol, path] = uri.split("://");
@@ -161,7 +160,7 @@ DebuggerResourceProvider::ReadResource(llvm::StringRef uri) const {
161160
return ReadDebuggerResource(uri, debugger_idx);
162161
}
163162

164-
llvm::Expected<lldb_protocol::mcp::ResourceResult>
163+
llvm::Expected<lldb_protocol::mcp::ReadResourceResult>
165164
DebuggerResourceProvider::ReadDebuggerResource(llvm::StringRef uri,
166165
lldb::user_id_t debugger_id) {
167166
lldb::DebuggerSP debugger_sp = Debugger::FindDebuggerWithID(debugger_id);
@@ -173,17 +172,17 @@ DebuggerResourceProvider::ReadDebuggerResource(llvm::StringRef uri,
173172
debugger_resource.name = debugger_sp->GetInstanceName();
174173
debugger_resource.num_targets = debugger_sp->GetTargetList().GetNumTargets();
175174

176-
lldb_protocol::mcp::ResourceContents contents;
175+
lldb_protocol::mcp::TextResourceContents contents;
177176
contents.uri = uri;
178177
contents.mimeType = kMimeTypeJSON;
179178
contents.text = llvm::formatv("{0}", toJSON(debugger_resource));
180179

181-
lldb_protocol::mcp::ResourceResult result;
180+
lldb_protocol::mcp::ReadResourceResult result;
182181
result.contents.push_back(contents);
183182
return result;
184183
}
185184

186-
llvm::Expected<lldb_protocol::mcp::ResourceResult>
185+
llvm::Expected<lldb_protocol::mcp::ReadResourceResult>
187186
DebuggerResourceProvider::ReadTargetResource(llvm::StringRef uri,
188187
lldb::user_id_t debugger_id,
189188
size_t target_idx) {
@@ -209,12 +208,12 @@ DebuggerResourceProvider::ReadTargetResource(llvm::StringRef uri,
209208
if (lldb::PlatformSP platform_sp = target_sp->GetPlatform())
210209
target_resource.platform = platform_sp->GetName();
211210

212-
lldb_protocol::mcp::ResourceContents contents;
211+
lldb_protocol::mcp::TextResourceContents contents;
213212
contents.uri = uri;
214213
contents.mimeType = kMimeTypeJSON;
215214
contents.text = llvm::formatv("{0}", toJSON(target_resource));
216215

217-
lldb_protocol::mcp::ResourceResult result;
216+
lldb_protocol::mcp::ReadResourceResult result;
218217
result.contents.push_back(contents);
219218
return result;
220219
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
#include "lldb/Protocol/MCP/Protocol.h"
1313
#include "lldb/Protocol/MCP/Resource.h"
14-
#include "lldb/lldb-private.h"
14+
#include "lldb/lldb-forward.h"
15+
#include "lldb/lldb-types.h"
16+
#include "llvm/ADT/StringRef.h"
17+
#include "llvm/Support/Error.h"
18+
#include <cstddef>
1519
#include <vector>
1620

1721
namespace lldb_private::mcp {
@@ -21,19 +25,18 @@ class DebuggerResourceProvider : public lldb_protocol::mcp::ResourceProvider {
2125
using ResourceProvider::ResourceProvider;
2226
virtual ~DebuggerResourceProvider() = default;
2327

24-
virtual std::vector<lldb_protocol::mcp::Resource>
25-
GetResources() const override;
26-
virtual llvm::Expected<lldb_protocol::mcp::ResourceResult>
28+
std::vector<lldb_protocol::mcp::Resource> GetResources() const override;
29+
llvm::Expected<lldb_protocol::mcp::ReadResourceResult>
2730
ReadResource(llvm::StringRef uri) const override;
2831

2932
private:
3033
static lldb_protocol::mcp::Resource GetDebuggerResource(Debugger &debugger);
3134
static lldb_protocol::mcp::Resource GetTargetResource(size_t target_idx,
3235
Target &target);
3336

34-
static llvm::Expected<lldb_protocol::mcp::ResourceResult>
37+
static llvm::Expected<lldb_protocol::mcp::ReadResourceResult>
3538
ReadDebuggerResource(llvm::StringRef uri, lldb::user_id_t debugger_id);
36-
static llvm::Expected<lldb_protocol::mcp::ResourceResult>
39+
static llvm::Expected<lldb_protocol::mcp::ReadResourceResult>
3740
ReadTargetResource(llvm::StringRef uri, lldb::user_id_t debugger_id,
3841
size_t target_idx);
3942
};

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

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

99
#include "Tool.h"
10-
#include "lldb/Core/Module.h"
1110
#include "lldb/Interpreter/CommandInterpreter.h"
1211
#include "lldb/Interpreter/CommandReturnObject.h"
12+
#include "lldb/Protocol/MCP/Protocol.h"
1313

1414
using namespace lldb_private;
1515
using namespace lldb_protocol;
@@ -29,10 +29,10 @@ bool fromJSON(const llvm::json::Value &V, CommandToolArguments &A,
2929
O.mapOptional("arguments", A.arguments);
3030
}
3131

32-
/// Helper function to create a TextResult from a string output.
33-
static lldb_protocol::mcp::TextResult createTextResult(std::string output,
34-
bool is_error = false) {
35-
lldb_protocol::mcp::TextResult text_result;
32+
/// Helper function to create a CallToolResult from a string output.
33+
static lldb_protocol::mcp::CallToolResult
34+
createTextResult(std::string output, bool is_error = false) {
35+
lldb_protocol::mcp::CallToolResult text_result;
3636
text_result.content.emplace_back(
3737
lldb_protocol::mcp::TextContent{{std::move(output)}});
3838
text_result.isError = is_error;
@@ -41,7 +41,7 @@ static lldb_protocol::mcp::TextResult createTextResult(std::string output,
4141

4242
} // namespace
4343

44-
llvm::Expected<lldb_protocol::mcp::TextResult>
44+
llvm::Expected<lldb_protocol::mcp::CallToolResult>
4545
CommandTool::Call(const lldb_protocol::mcp::ToolArguments &args) {
4646
if (!std::holds_alternative<json::Value>(args))
4747
return createStringError("CommandTool requires arguments");

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
#ifndef LLDB_PLUGINS_PROTOCOL_MCP_TOOL_H
1010
#define LLDB_PLUGINS_PROTOCOL_MCP_TOOL_H
1111

12-
#include "lldb/Core/Debugger.h"
1312
#include "lldb/Protocol/MCP/Protocol.h"
1413
#include "lldb/Protocol/MCP/Tool.h"
14+
#include "llvm/Support/Error.h"
1515
#include "llvm/Support/JSON.h"
16-
#include <string>
16+
#include <optional>
1717

1818
namespace lldb_private::mcp {
1919

@@ -22,10 +22,10 @@ class CommandTool : public lldb_protocol::mcp::Tool {
2222
using lldb_protocol::mcp::Tool::Tool;
2323
~CommandTool() = default;
2424

25-
virtual llvm::Expected<lldb_protocol::mcp::TextResult>
25+
llvm::Expected<lldb_protocol::mcp::CallToolResult>
2626
Call(const lldb_protocol::mcp::ToolArguments &args) override;
2727

28-
virtual std::optional<llvm::json::Value> GetSchema() const override;
28+
std::optional<llvm::json::Value> GetSchema() const override;
2929
};
3030

3131
} // namespace lldb_private::mcp

0 commit comments

Comments
 (0)