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."));
63 std::string client_name = llvm::formatv(
"client_{0}", ++
m_client_count);
64 LLDB_LOG(log,
"New MCP client connected: {0}", client_name);
67 auto transport_up = std::make_unique<lldb_protocol::mcp::Transport>(
68 m_loop, io_sp, io_sp, [client_name](llvm::StringRef message) {
77 std::lock_guard<std::mutex> guard(
m_mutex);
80 return llvm::createStringError(
"the MCP server is already running");
93 this, std::placeholders::_1));
94 if (llvm::Error
error = handles.takeError())
97 auto listening_uris =
m_listener->GetListeningConnectionURI();
98 if (listening_uris.empty())
99 return createStringError(
"failed to get listening connections");
103 if (!server_info_handle)
104 return server_info_handle.takeError();
107 m_server = std::make_unique<lldb_protocol::mcp::Server>(
109 [](StringRef message) {
118 llvm::set_thread_name(
"protocol-server.mcp");
122 return llvm::Error::success();
127 std::lock_guard<std::mutex> guard(
m_mutex);
129 return createStringError(
"the MCP sever is not running");
134 bool addition_succeeded =
m_loop.AddPendingCallback(
148 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)
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.