18#include "llvm/ADT/StringExtras.h"
19#include "llvm/Support/Error.h"
20#include "llvm/Support/Threading.h"
30static constexpr
llvm::StringLiteral
kName =
"lldb-mcp";
49 return std::make_unique<ProtocolServerMCP>();
60 "MCP initialization complete");
63 std::make_unique<CommandTool>(
"lldb_command",
"Run an lldb command."));
69 std::string client_name = llvm::formatv(
"client_{0}",
m_instances.size() + 1);
70 LLDB_LOG(log,
"New MCP client connected: {0}", client_name);
73 auto transport_up = std::make_unique<lldb_protocol::mcp::Transport>(
74 io_sp, io_sp, [client_name](llvm::StringRef message) {
77 auto instance_up = std::make_unique<lldb_protocol::mcp::Server>(
78 std::string(
kName), std::string(
kVersion), std::move(transport_up),
81 llvm::Error
error = instance_up->Run();
90 std::lock_guard<std::mutex> guard(
m_mutex);
93 return llvm::createStringError(
"the MCP server is already running");
106 this, std::placeholders::_1));
107 if (llvm::Error
error = handles.takeError())
110 auto listening_uris =
m_listener->GetListeningConnectionURI();
111 if (listening_uris.empty())
113 std::string address =
114 llvm::join(
m_listener->GetListeningConnectionURI(),
", ");
119 return handle.takeError();
125 llvm::set_thread_name(
"protocol-server.mcp");
129 return llvm::Error::success();
134 std::lock_guard<std::mutex> guard(
m_mutex);
141 m_loop.AddPendingCallback(
152 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
virtual llvm::Error Start(ProtocolServer::Connection connection) override
std::vector< MainLoopBase::ReadHandleUP > m_listen_handlers
std::vector< std::unique_ptr< lldb_protocol::mcp::Server > > m_instances
static lldb::ProtocolServerUP CreateInstance()
virtual 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()
virtual ~ProtocolServerMCP() override
lldb_private::MainLoop m_loop
A handle that tracks the server info on disk and cleans up the disk record once it is no longer refer...
void AddTool(std::unique_ptr< Tool > tool)
void AddResourceProvider(std::unique_ptr< ResourceProvider > resource_provider)
void AddNotificationHandler(llvm::StringRef method, NotificationHandler handler)
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
A notification which does not expect a response.
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.