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";
47 return std::make_unique<ProtocolServerMCP>();
56 std::make_unique<CommandTool>(
"command",
"Run an lldb command."));
57 server.
AddTool(std::make_unique<DebuggerListTool>(
58 "debugger_list",
"List debugger instances with their debugger_id."));
64 std::string client_name = llvm::formatv(
"client_{0}", ++
m_client_count);
65 LLDB_LOG(log,
"New MCP client connected: {0}", client_name);
68 auto transport_up = std::make_unique<lldb_protocol::mcp::Transport>(
69 io_sp, io_sp, [client_name](llvm::StringRef message) {
78 std::lock_guard<std::mutex> guard(
m_mutex);
81 return llvm::createStringError(
"the MCP server is already running");
94 this, std::placeholders::_1));
95 if (llvm::Error
error = handles.takeError())
98 auto listening_uris =
m_listener->GetListeningConnectionURI();
99 if (listening_uris.empty())
101 std::string address =
102 llvm::join(
m_listener->GetListeningConnectionURI(),
", ");
106 if (!server_info_handle)
107 return server_info_handle.takeError();
110 m_server = std::make_unique<lldb_protocol::mcp::Server>(
111 std::string(
kName), std::string(
kVersion), [](StringRef message) {
120 llvm::set_thread_name(
"protocol-server.mcp");
124 return llvm::Error::success();
129 std::lock_guard<std::mutex> guard(
m_mutex);
136 m_loop.AddPendingCallback(
149 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
static constexpr llvm::StringLiteral kVersion
static llvm::Error createStringError(const char *format, Args &&...args)
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.
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.