Skip to content

[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

Merged
merged 1 commit into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lldb/cmake/modules/LLDBConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ add_optional_dependency(LLDB_ENABLE_FBSDVMCORE "Enable libfbsdvmcore support in

option(LLDB_USE_ENTITLEMENTS "When codesigning, use entitlements if available" ON)
option(LLDB_BUILD_FRAMEWORK "Build LLDB.framework (Darwin only)" OFF)
option(LLDB_ENABLE_PROTOCOL_SERVERS "Enable protocol servers (e.g. MCP) in LLDB" ON)
option(LLDB_NO_INSTALL_DEFAULT_RPATH "Disable default RPATH settings in binaries" OFF)
option(LLDB_USE_SYSTEM_DEBUGSERVER "Use the system's debugserver for testing (Darwin only)." OFF)
option(LLDB_SKIP_STRIP "Whether to skip stripping of binaries when installing lldb." OFF)
Expand Down
6 changes: 6 additions & 0 deletions lldb/include/lldb/Core/Debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,10 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
void FlushProcessOutput(Process &process, bool flush_stdout,
bool flush_stderr);

void AddProtocolServer(lldb::ProtocolServerSP protocol_server_sp);
void RemoveProtocolServer(lldb::ProtocolServerSP protocol_server_sp);
lldb::ProtocolServerSP GetProtocolServer(llvm::StringRef protocol) const;

SourceManager::SourceFileCache &GetSourceFileCache() {
return m_source_file_cache;
}
Expand Down Expand Up @@ -772,6 +776,8 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
mutable std::mutex m_progress_reports_mutex;
/// @}

llvm::SmallVector<lldb::ProtocolServerSP> m_protocol_servers;

std::mutex m_destroy_callback_mutex;
lldb::callback_token_t m_destroy_callback_next_token = 0;
struct DestroyCallbackInfo {
Expand Down
11 changes: 11 additions & 0 deletions lldb/include/lldb/Core/PluginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,17 @@ class PluginManager {
static void AutoCompleteProcessName(llvm::StringRef partial_name,
CompletionRequest &request);

// Protocol
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description,
ProtocolServerCreateInstance create_callback);

static bool UnregisterPlugin(ProtocolServerCreateInstance create_callback);

static llvm::StringRef GetProtocolServerPluginNameAtIndex(uint32_t idx);

static ProtocolServerCreateInstance
GetProtocolCreateCallbackForPluginName(llvm::StringRef name);

// Register Type Provider
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description,
RegisterTypeBuilderCreateInstance create_callback);
Expand Down
39 changes: 39 additions & 0 deletions lldb/include/lldb/Core/ProtocolServer.h
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
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ static constexpr CommandObject::ArgumentTableEntry g_argument_table[] = {
{ lldb::eArgTypeCPUName, "cpu-name", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The name of a CPU." },
{ lldb::eArgTypeCPUFeatures, "cpu-features", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The CPU feature string." },
{ lldb::eArgTypeManagedPlugin, "managed-plugin", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "Plugins managed by the PluginManager" },
{ lldb::eArgTypeProtocol, "protocol", lldb::CompletionType::eNoCompletion, {}, { nullptr, false }, "The name of the protocol." },
// clang-format on
};

Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/lldb-enumerations.h
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ enum CommandArgumentType {
eArgTypeCPUName,
eArgTypeCPUFeatures,
eArgTypeManagedPlugin,
eArgTypeProtocol,
eArgTypeLastArg // Always keep this entry as the last entry in this
// enumeration!!
};
Expand Down
3 changes: 2 additions & 1 deletion lldb/include/lldb/lldb-forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ class PersistentExpressionState;
class Platform;
class Process;
class ProcessAttachInfo;
class ProcessLaunchInfo;
class ProcessInfo;
class ProcessInstanceInfo;
class ProcessInstanceInfoMatch;
class ProcessLaunchInfo;
class ProcessModID;
class Property;
class ProtocolServer;
class Queue;
class QueueImpl;
class QueueItem;
Expand Down Expand Up @@ -391,6 +391,7 @@ typedef std::shared_ptr<lldb_private::Platform> PlatformSP;
typedef std::shared_ptr<lldb_private::Process> ProcessSP;
typedef std::shared_ptr<lldb_private::ProcessAttachInfo> ProcessAttachInfoSP;
typedef std::shared_ptr<lldb_private::ProcessLaunchInfo> ProcessLaunchInfoSP;
typedef std::shared_ptr<lldb_private::ProtocolServer> ProtocolServerSP;
typedef std::weak_ptr<lldb_private::Process> ProcessWP;
typedef std::shared_ptr<lldb_private::RegisterCheckpoint> RegisterCheckpointSP;
typedef std::shared_ptr<lldb_private::RegisterContext> RegisterContextSP;
Expand Down
2 changes: 2 additions & 0 deletions lldb/include/lldb/lldb-private-interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ typedef lldb::PlatformSP (*PlatformCreateInstance)(bool force,
typedef lldb::ProcessSP (*ProcessCreateInstance)(
lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
const FileSpec *crash_file_path, bool can_connect);
typedef lldb::ProtocolServerSP (*ProtocolServerCreateInstance)(
Debugger &debugger);
typedef lldb::RegisterTypeBuilderSP (*RegisterTypeBuilderCreateInstance)(
Target &target);
typedef lldb::ScriptInterpreterSP (*ScriptInterpreterCreateInstance)(
Expand Down
1 change: 1 addition & 0 deletions lldb/source/Commands/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ add_lldb_library(lldbCommands NO_PLUGIN_DEPENDENCIES
CommandObjectPlatform.cpp
CommandObjectPlugin.cpp
CommandObjectProcess.cpp
CommandObjectProtocolServer.cpp
CommandObjectQuit.cpp
CommandObjectRegexCommand.cpp
CommandObjectRegister.cpp
Expand Down
176 changes: 176 additions & 0 deletions lldb/source/Commands/CommandObjectProtocolServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
//===-- 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;
}

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());

const char *connection_error =
"unsupported connection specifier, expected 'accept:///path' or "
"'listen://[host]:port', got '{0}'.";
auto uri = lldb_private::URI::Parse(connection_uri);
if (!uri) {
result.AppendErrorWithFormatv(connection_error, connection_uri);
return;
}

std::optional<Socket::ProtocolModePair> protocol_and_mode =
Socket::GetProtocolAndMode(uri->scheme);
if (!protocol_and_mode || protocol_and_mode->second != Socket::ModeAccept) {
result.AppendErrorWithFormatv(connection_error, connection_uri);
return;
}

ProtocolServer::Connection connection;
connection.protocol = protocol_and_mode->first;
connection.name =
formatv("[{0}]:{1}", uri->hostname.empty() ? "0.0.0.0" : uri->hostname,
uri->port.value_or(0));

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;
25 changes: 25 additions & 0 deletions lldb/source/Commands/CommandObjectProtocolServer.h
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
1 change: 1 addition & 0 deletions lldb/source/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ add_lldb_library(lldbCore NO_PLUGIN_DEPENDENCIES
Opcode.cpp
PluginManager.cpp
Progress.cpp
ProtocolServer.cpp
Statusline.cpp
RichManglingContext.cpp
SearchFilter.cpp
Expand Down
24 changes: 24 additions & 0 deletions lldb/source/Core/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Progress.h"
#include "lldb/Core/ProtocolServer.h"
#include "lldb/Core/StreamAsynchronousIO.h"
#include "lldb/Core/Telemetry.h"
#include "lldb/DataFormatters/DataVisualization.h"
Expand Down Expand Up @@ -2375,3 +2376,26 @@ llvm::ThreadPoolInterface &Debugger::GetThreadPool() {
"Debugger::GetThreadPool called before Debugger::Initialize");
return *g_thread_pool;
}

void Debugger::AddProtocolServer(lldb::ProtocolServerSP protocol_server_sp) {
assert(protocol_server_sp &&
GetProtocolServer(protocol_server_sp->GetPluginName()) == nullptr);
m_protocol_servers.push_back(protocol_server_sp);
}

void Debugger::RemoveProtocolServer(lldb::ProtocolServerSP protocol_server_sp) {
auto it = llvm::find(m_protocol_servers, protocol_server_sp);
if (it != m_protocol_servers.end())
m_protocol_servers.erase(it);
}

lldb::ProtocolServerSP
Debugger::GetProtocolServer(llvm::StringRef protocol) const {
for (ProtocolServerSP protocol_server_sp : m_protocol_servers) {
if (!protocol_server_sp)
continue;
if (protocol_server_sp->GetPluginName() == protocol)
return protocol_server_sp;
}
return nullptr;
}
Loading
Loading