-
Notifications
You must be signed in to change notification settings - Fork 14k
[lldb] Add Model Context Protocol (MCP) support to LLDB #143628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JDevlieghere
wants to merge
1
commit into
llvm:main
Choose a base branch
from
JDevlieghere:MCP
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,788
−23
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//===-- ProtocolServer.h --------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLDB_CORE_PROTOCOLSERVER_H | ||
#define LLDB_CORE_PROTOCOLSERVER_H | ||
|
||
#include "lldb/Core/PluginInterface.h" | ||
#include "lldb/Host/Socket.h" | ||
#include "lldb/lldb-private-interfaces.h" | ||
|
||
namespace lldb_private { | ||
|
||
class ProtocolServer : public PluginInterface { | ||
public: | ||
ProtocolServer() = default; | ||
virtual ~ProtocolServer() = default; | ||
|
||
static lldb::ProtocolServerSP Create(llvm::StringRef name, | ||
Debugger &debugger); | ||
|
||
struct Connection { | ||
Socket::SocketProtocol protocol; | ||
std::string name; | ||
}; | ||
|
||
virtual llvm::Error Start(Connection connection) = 0; | ||
virtual llvm::Error Stop() = 0; | ||
|
||
virtual Socket *GetSocket() const = 0; | ||
}; | ||
|
||
} // namespace lldb_private | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
//===-- CommandObjectProtocolServer.cpp | ||
//----------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "CommandObjectProtocolServer.h" | ||
#include "lldb/Core/PluginManager.h" | ||
#include "lldb/Core/ProtocolServer.h" | ||
#include "lldb/Host/Socket.h" | ||
#include "lldb/Interpreter/CommandInterpreter.h" | ||
#include "lldb/Interpreter/CommandReturnObject.h" | ||
#include "lldb/Utility/UriParser.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/Support/FormatAdapters.h" | ||
|
||
using namespace llvm; | ||
using namespace lldb; | ||
using namespace lldb_private; | ||
|
||
#define LLDB_OPTIONS_mcp | ||
#include "CommandOptions.inc" | ||
|
||
static std::vector<llvm::StringRef> GetSupportedProtocols() { | ||
std::vector<llvm::StringRef> supported_protocols; | ||
size_t i = 0; | ||
|
||
for (llvm::StringRef protocol_name = | ||
PluginManager::GetProtocolServerPluginNameAtIndex(i++); | ||
!protocol_name.empty(); | ||
protocol_name = PluginManager::GetProtocolServerPluginNameAtIndex(i++)) { | ||
supported_protocols.push_back(protocol_name); | ||
} | ||
|
||
return supported_protocols; | ||
} | ||
|
||
static llvm::Expected<std::pair<Socket::SocketProtocol, std::string>> | ||
validateConnection(llvm::StringRef conn) { | ||
auto uri = lldb_private::URI::Parse(conn); | ||
|
||
if (uri && (uri->scheme == "tcp" || uri->scheme == "connect" || | ||
!uri->hostname.empty() || uri->port)) { | ||
return std::make_pair( | ||
Socket::ProtocolTcp, | ||
formatv("[{0}]:{1}", uri->hostname.empty() ? "0.0.0.0" : uri->hostname, | ||
uri->port.value_or(0))); | ||
} | ||
|
||
if (uri && (uri->scheme == "unix" || uri->scheme == "unix-connect" || | ||
uri->path != "/")) { | ||
return std::make_pair(Socket::ProtocolUnixDomain, uri->path.str()); | ||
} | ||
|
||
return llvm::createStringError( | ||
"Unsupported connection specifier, expected 'unix-connect:///path' or " | ||
"'connect://[host]:port', got '%s'.", | ||
conn.str().c_str()); | ||
} | ||
|
||
class CommandObjectProtocolServerStart : public CommandObjectParsed { | ||
public: | ||
CommandObjectProtocolServerStart(CommandInterpreter &interpreter) | ||
: CommandObjectParsed(interpreter, "protocol-server start", | ||
"start protocol server", | ||
"protocol-server start <protocol> <connection>") { | ||
AddSimpleArgumentList(lldb::eArgTypeProtocol, eArgRepeatPlain); | ||
AddSimpleArgumentList(lldb::eArgTypeConnectURL, eArgRepeatPlain); | ||
} | ||
|
||
~CommandObjectProtocolServerStart() override = default; | ||
|
||
protected: | ||
void DoExecute(Args &args, CommandReturnObject &result) override { | ||
if (args.GetArgumentCount() < 1) { | ||
result.AppendError("no protocol specified"); | ||
return; | ||
} | ||
|
||
llvm::StringRef protocol = args.GetArgumentAtIndex(0); | ||
std::vector<llvm::StringRef> supported_protocols = GetSupportedProtocols(); | ||
if (llvm::find(supported_protocols, protocol) == | ||
supported_protocols.end()) { | ||
result.AppendErrorWithFormatv( | ||
"unsupported protocol: {0}. Supported protocols are: {1}", protocol, | ||
llvm::join(GetSupportedProtocols(), ", ")); | ||
return; | ||
} | ||
|
||
if (args.GetArgumentCount() < 2) { | ||
result.AppendError("no connection specified"); | ||
return; | ||
} | ||
llvm::StringRef connection_uri = args.GetArgumentAtIndex(1); | ||
|
||
ProtocolServerSP server_sp = GetDebugger().GetProtocolServer(protocol); | ||
if (!server_sp) | ||
server_sp = ProtocolServer::Create(protocol, GetDebugger()); | ||
|
||
auto maybeProtoclAndName = validateConnection(connection_uri); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo? (I think saving one char isn't worth it) |
||
if (auto error = maybeProtoclAndName.takeError()) { | ||
result.AppendErrorWithFormatv("{0}", llvm::fmt_consume(std::move(error))); | ||
return; | ||
} | ||
|
||
ProtocolServer::Connection connection; | ||
std::tie(connection.protocol, connection.name) = *maybeProtoclAndName; | ||
|
||
if (llvm::Error error = server_sp->Start(connection)) { | ||
result.AppendErrorWithFormatv("{0}", llvm::fmt_consume(std::move(error))); | ||
return; | ||
} | ||
|
||
GetDebugger().AddProtocolServer(server_sp); | ||
|
||
if (Socket *socket = server_sp->GetSocket()) { | ||
std::string address = | ||
llvm::join(socket->GetListeningConnectionURI(), ", "); | ||
result.AppendMessageWithFormatv( | ||
"{0} server started with connection listeners: {1}", protocol, | ||
address); | ||
} | ||
} | ||
}; | ||
|
||
class CommandObjectProtocolServerStop : public CommandObjectParsed { | ||
public: | ||
CommandObjectProtocolServerStop(CommandInterpreter &interpreter) | ||
: CommandObjectParsed(interpreter, "protocol-server stop", | ||
"stop protocol server", | ||
"protocol-server stop <protocol>") { | ||
AddSimpleArgumentList(lldb::eArgTypeProtocol, eArgRepeatPlain); | ||
} | ||
|
||
~CommandObjectProtocolServerStop() override = default; | ||
|
||
protected: | ||
void DoExecute(Args &args, CommandReturnObject &result) override { | ||
if (args.GetArgumentCount() < 1) { | ||
result.AppendError("no protocol specified"); | ||
return; | ||
} | ||
|
||
llvm::StringRef protocol = args.GetArgumentAtIndex(0); | ||
std::vector<llvm::StringRef> supported_protocols = GetSupportedProtocols(); | ||
if (llvm::find(supported_protocols, protocol) == | ||
supported_protocols.end()) { | ||
result.AppendErrorWithFormatv( | ||
"unsupported protocol: {0}. Supported protocols are: {1}", protocol, | ||
llvm::join(GetSupportedProtocols(), ", ")); | ||
return; | ||
} | ||
|
||
Debugger &debugger = GetDebugger(); | ||
|
||
ProtocolServerSP server_sp = debugger.GetProtocolServer(protocol); | ||
if (!server_sp) { | ||
result.AppendError( | ||
llvm::formatv("no {0} protocol server running", protocol).str()); | ||
return; | ||
} | ||
|
||
if (llvm::Error error = server_sp->Stop()) { | ||
result.AppendErrorWithFormatv("{0}", llvm::fmt_consume(std::move(error))); | ||
return; | ||
} | ||
|
||
debugger.RemoveProtocolServer(server_sp); | ||
} | ||
}; | ||
|
||
CommandObjectProtocolServer::CommandObjectProtocolServer( | ||
CommandInterpreter &interpreter) | ||
: CommandObjectMultiword(interpreter, "protocol-server", | ||
"Start and stop a protocol server.", | ||
"protocol-server") { | ||
LoadSubCommand("start", CommandObjectSP(new CommandObjectProtocolServerStart( | ||
interpreter))); | ||
LoadSubCommand("stop", CommandObjectSP( | ||
new CommandObjectProtocolServerStop(interpreter))); | ||
} | ||
|
||
CommandObjectProtocolServer::~CommandObjectProtocolServer() = default; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//===-- CommandObjectProtocolServer.h | ||
//------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLDB_SOURCE_COMMANDS_COMMANDOBJECTPROTOCOLSERVER_H | ||
#define LLDB_SOURCE_COMMANDS_COMMANDOBJECTPROTOCOLSERVER_H | ||
|
||
#include "lldb/Interpreter/CommandObjectMultiword.h" | ||
|
||
namespace lldb_private { | ||
|
||
class CommandObjectProtocolServer : public CommandObjectMultiword { | ||
public: | ||
CommandObjectProtocolServer(CommandInterpreter &interpreter); | ||
~CommandObjectProtocolServer() override; | ||
}; | ||
|
||
} // namespace lldb_private | ||
|
||
#endif // LLDB_SOURCE_COMMANDS_COMMANDOBJECTMCP_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm troubled by the inconsistency of this function w.r.t rest of lldb (actually, I'm also somewhat troubled by its existence).
Currently if you do something like
platform connect connect://localhost:1234
, lldb will connect to the remote server at that address, whileplatform connect listen://localhost:1234
will cause lldb to wait for a connection from the remote side. Similarly for unix-connect and unix-accept.Here you seem to be defining "connect" to mean "wait for a connection". I suppose that active (outgoing) connections aren't going to be particularly useful, but in principle I don't see why they couldn't work (for a protocol/server that only serves a single connection), and this seems like it could cause confusion down the line. Could we stick to the protocols as defined in ConnectionFileDescriptor::Connect (ideally, we would reuse that function, but I suspect that won't be very easy)?
I think I'd be fine with also accepting host:port as a shorthand for
listen://host:port
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied this from DAP where I think @ashgti wrote it originally. Happy to settle on something different and share that across the two.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather them be consistent if we need to change lldb-dap as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would much rather change lldb-dap (which has this syntax for how many months?) that live with an inconsistent url specification forever. While it would be nice if it was consistent, the inconsistency with between lldb and lldb-dap is not as troubling for me. "platform connect" and "protocol-server start" make a much more blatant inconsistency, since they are both CLI commands directly implemented by lldb.