16#include "llvm/ADT/StringExtras.h"
17#include "llvm/Support/Error.h"
18#include "llvm/Support/Threading.h"
28static constexpr
llvm::StringLiteral
kName =
"lldb-mcp";
46 return std::make_unique<ProtocolServerMCP>();
55 std::make_unique<CommandTool>(
"command",
"Run an lldb command."));
56 server.
AddTool(std::make_unique<DebuggerListTool>(
57 "debugger_list",
"List debugger instances with their debugger_id."));
58 server.
AddTool(std::make_unique<DebuggerCreateTool>(
59 "debugger_create",
"Create a new debugger instance and return its URI."));
60 server.
AddTool(std::make_unique<DebuggerDeleteTool>(
61 "debugger_delete",
"Destroy a debugger instance by id or URI."));
71 std::string client_name = llvm::formatv(
"client_{0}", ++
m_client_count);
72 LLDB_LOG(log,
"New MCP client connected: {0}", client_name);
75 auto transport_up = std::make_unique<lldb_protocol::mcp::Transport>(
76 m_loop, io_sp, io_sp, [client_name](llvm::StringRef message) {
85 std::lock_guard<std::mutex> guard(
m_mutex);
88 return llvm::createStringError(
"the MCP server is already running");
101 this, std::placeholders::_1));
102 if (llvm::Error
error = handles.takeError())
105 auto listening_uris =
m_listener->GetListeningConnectionURI();
106 if (listening_uris.empty())
107 return createStringError(
"failed to get listening connections");
111 if (!server_info_handle)
112 return server_info_handle.takeError();
115 m_server = std::make_unique<lldb_protocol::mcp::Server>(
117 [](StringRef message) {
126 llvm::set_thread_name(
"protocol-server.mcp");
130 return llvm::Error::success();
135 std::lock_guard<std::mutex> guard(
m_mutex);
137 return createStringError(
"the MCP sever is not running");
142 bool addition_succeeded =
m_loop.AddPendingCallback(
156 return llvm::Error::success();
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
#define LLDB_LOG_ERROR(log, error,...)
#define LLDB_PLUGIN_DEFINE(PluginName)
static constexpr llvm::StringLiteral kName
virtual void RequestTermination()
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
static llvm::Error Terminate()
static std::unique_ptr< Socket > Create(const SocketProtocol protocol, Status &error)
bool Fail() const
Test for error condition.
std::unique_ptr< Socket > m_listener
virtual void Extend(lldb_protocol::mcp::Server &server) const
std::thread m_loop_thread
llvm::Error Start(ProtocolServer::Connection connection) override
std::vector< ReadHandleUP > m_accept_handles
static lldb::ProtocolServerUP CreateInstance()
llvm::Error Stop() override
void AcceptCallback(std::unique_ptr< Socket > socket)
static llvm::StringRef GetPluginNameStatic()
lldb_protocol::mcp::ServerInfoHandle m_server_info_handle
static llvm::StringRef GetPluginDescriptionStatic()
~ProtocolServerMCP() override
lldb_private::MainLoop m_loop
void AddTool(std::unique_ptr< Tool > tool)
void AddResourceProvider(std::unique_ptr< ResourceProvider > resource_provider)
void PopulateServer(lldb_protocol::mcp::Server &server)
Installs the standard LLDB tool and resource-provider set on server.
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
llvm::StringLiteral GetServerVersion()
Version reported by LLDB's MCP server.
std::shared_ptr< lldb_private::IOObject > IOObjectSP
std::unique_ptr< lldb_private::ProtocolServer > ProtocolServerUP
Socket::SocketProtocol protocol
Information about this instance of lldb's MCP server for lldb-mcp to use to coordinate connecting an ...
static llvm::Expected< ServerInfoHandle > Write(const ServerInfo &)
Writes the server info into a unique file in ~/.lldb.