Skip to content

Commit 33bba7e

Browse files
committed
Address John's feedback
1 parent 5ed60a3 commit 33bba7e

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ using Message = std::variant<Request, Response, Notification, Error>;
123123
bool fromJSON(const llvm::json::Value &, Message &, llvm::json::Path);
124124
llvm::json::Value toJSON(const Message &);
125125

126+
using ToolArguments = std::variant<std::monostate, llvm::json::Value>;
127+
126128
} // namespace lldb_private::mcp::protocol
127129

128130
#endif

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,12 @@ ProtocolServerMCP::ToolsCallHandler(const protocol::Request &request) {
314314
if (it == m_tools.end())
315315
return llvm::createStringError(llvm::formatv("no tool \"{0}\"", tool_name));
316316

317-
const json::Value *args = param_obj->get("arguments");
318-
llvm::Expected<protocol::TextResult> text_result = it->second->Call(args);
317+
protocol::ToolArguments tool_args;
318+
if (const json::Value *args = param_obj->get("arguments"))
319+
tool_args = *args;
320+
321+
llvm::Expected<protocol::TextResult> text_result =
322+
it->second->Call(tool_args);
319323
if (!text_result)
320324
return text_result.takeError();
321325

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using namespace lldb_private::mcp;
1515
using namespace llvm;
1616

17+
namespace {
1718
struct CommandToolArguments {
1819
uint64_t debugger_id;
1920
std::string arguments;
@@ -26,6 +27,8 @@ bool fromJSON(const llvm::json::Value &V, CommandToolArguments &A,
2627
O.mapOptional("arguments", A.arguments);
2728
}
2829

30+
} // namespace
31+
2932
Tool::Tool(std::string name, std::string description)
3033
: m_name(std::move(name)), m_description(std::move(description)) {}
3134

@@ -41,14 +44,14 @@ protocol::ToolDefinition Tool::GetDefinition() const {
4144
}
4245

4346
llvm::Expected<protocol::TextResult>
44-
CommandTool::Call(const llvm::json::Value *args) {
45-
if (!args)
46-
return createStringError("no tool arguments");
47+
CommandTool::Call(const protocol::ToolArguments &args) {
48+
if (!std::holds_alternative<json::Value>(args))
49+
return createStringError("CommandTool requires arguments");
4750

48-
llvm::json::Path::Root root;
51+
json::Path::Root root;
4952

5053
CommandToolArguments arguments;
51-
if (!fromJSON(*args, arguments, root))
54+
if (!fromJSON(std::get<json::Value>(args), arguments, root))
5255
return root.getError();
5356

5457
lldb::DebuggerSP debugger_sp =
@@ -93,9 +96,22 @@ std::optional<llvm::json::Value> CommandTool::GetSchema() const {
9396
}
9497

9598
llvm::Expected<protocol::TextResult>
96-
DebuggerListTool::Call(const llvm::json::Value *args) {
99+
DebuggerListTool::Call(const protocol::ToolArguments &args) {
100+
if (!std::holds_alternative<std::monostate>(args))
101+
return createStringError("DebuggerListTool takes no arguments");
102+
97103
llvm::json::Path::Root root;
98104

105+
// Return a nested Markdown list with debuggers and target.
106+
// Example output:
107+
//
108+
// - debugger 0
109+
// - target 0 /path/to/foo
110+
// - target 1
111+
// - debugger 1
112+
// - target 0 /path/to/bar
113+
//
114+
// FIXME: Use Structured Content when we adopt protocol version 2025-06-18.
99115
std::string output;
100116
llvm::raw_string_ostream os(output);
101117

@@ -114,8 +130,10 @@ DebuggerListTool::Call(const llvm::json::Value *args) {
114130
if (!target_sp)
115131
continue;
116132
os << " - target " << j;
133+
// Append the module path if we have one.
117134
if (Module *exe_module = target_sp->GetExecutableModulePointer())
118135
os << " " << exe_module->GetFileSpec().GetPath();
136+
os << '\n';
119137
}
120138
}
121139

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Tool {
2222
virtual ~Tool() = default;
2323

2424
virtual llvm::Expected<protocol::TextResult>
25-
Call(const llvm::json::Value *args) = 0;
25+
Call(const protocol::ToolArguments &args) = 0;
2626

2727
virtual std::optional<llvm::json::Value> GetSchema() const {
2828
return llvm::json::Object{{"type", "object"}};
@@ -43,7 +43,7 @@ class CommandTool : public mcp::Tool {
4343
~CommandTool() = default;
4444

4545
virtual llvm::Expected<protocol::TextResult>
46-
Call(const llvm::json::Value *args) override;
46+
Call(const protocol::ToolArguments &args) override;
4747

4848
virtual std::optional<llvm::json::Value> GetSchema() const override;
4949
};
@@ -54,7 +54,7 @@ class DebuggerListTool : public mcp::Tool {
5454
~DebuggerListTool() = default;
5555

5656
virtual llvm::Expected<protocol::TextResult>
57-
Call(const llvm::json::Value *args) override;
57+
Call(const protocol::ToolArguments &args) override;
5858
};
5959

6060
} // namespace lldb_private::mcp

0 commit comments

Comments
 (0)