From cf719895cfd59ce55d349d8f27ae60ad13a3f96d Mon Sep 17 00:00:00 2001 From: Alexandre Perez Date: Thu, 9 Oct 2025 16:23:46 -0700 Subject: [PATCH 1/2] [lldb][mcp] Add protocol-server subcommand to get the running MCP server connection information --- .../Commands/CommandObjectProtocolServer.cpp | 45 ++++++++++++++++++- .../Protocol/MCP/ProtocolServerMCP.cpp | 1 + .../commands/protocol/TestMCPUnixSocket.py | 13 ++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lldb/source/Commands/CommandObjectProtocolServer.cpp b/lldb/source/Commands/CommandObjectProtocolServer.cpp index c5ab9e9f05bec..ff9d38c84ed2d 100644 --- a/lldb/source/Commands/CommandObjectProtocolServer.cpp +++ b/lldb/source/Commands/CommandObjectProtocolServer.cpp @@ -131,15 +131,58 @@ class CommandObjectProtocolServerStop : public CommandObjectParsed { } }; +class CommandObjectProtocolServerGet : public CommandObjectParsed { +public: + CommandObjectProtocolServerGet(CommandInterpreter &interpreter) + : CommandObjectParsed(interpreter, "protocol-server get", + "get protocol server connection information", + "protocol-server get ") { + AddSimpleArgumentList(lldb::eArgTypeProtocol, eArgRepeatPlain); + } + + ~CommandObjectProtocolServerGet() 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); + ProtocolServer *server = ProtocolServer::GetOrCreate(protocol); + if (!server) { + result.AppendErrorWithFormatv( + "unsupported protocol: {0}. Supported protocols are: {1}", protocol, + llvm::join(ProtocolServer::GetSupportedProtocols(), ", ")); + return; + } + + Socket *socket = server->GetSocket(); + if (!socket) { + result.AppendErrorWithFormatv("{0} server is not running", protocol); + return; + } + + std::string address = + llvm::join(socket->GetListeningConnectionURI(), ", "); + result.AppendMessageWithFormatv( + "{0} server connection listeners: {1}", protocol, address); + result.SetStatus(eReturnStatusSuccessFinishNoResult); + } +}; + CommandObjectProtocolServer::CommandObjectProtocolServer( CommandInterpreter &interpreter) : CommandObjectMultiword(interpreter, "protocol-server", - "Start and stop a protocol server.", + "Start, stop, and query protocol servers.", "protocol-server") { LoadSubCommand("start", CommandObjectSP(new CommandObjectProtocolServerStart( interpreter))); LoadSubCommand("stop", CommandObjectSP( new CommandObjectProtocolServerStop(interpreter))); + LoadSubCommand("get", CommandObjectSP( + new CommandObjectProtocolServerGet(interpreter))); } CommandObjectProtocolServer::~CommandObjectProtocolServer() = default; diff --git a/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp b/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp index 33bdd5eec3644..390cf3eeb16a5 100644 --- a/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp +++ b/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp @@ -144,6 +144,7 @@ llvm::Error ProtocolServerMCP::Stop() { m_server.reset(nullptr); m_server_info_handle.Remove(); + m_listener.reset(); return llvm::Error::success(); } diff --git a/lldb/test/API/commands/protocol/TestMCPUnixSocket.py b/lldb/test/API/commands/protocol/TestMCPUnixSocket.py index ea9255cc60ef5..9edb97eb791b9 100644 --- a/lldb/test/API/commands/protocol/TestMCPUnixSocket.py +++ b/lldb/test/API/commands/protocol/TestMCPUnixSocket.py @@ -32,3 +32,16 @@ def test_unix_socket(self): startstr="MCP server started with connection listeners:", substrs=[f"unix-connect://{socket_file}"], ) + + self.expect( + "protocol-server get MCP", + startstr="MCP server connection listeners:", + substrs=[f"unix-connect://{socket_file}"], + ) + + self.runCmd("protocol-server stop MCP", check=False) + self.expect( + "protocol-server get MCP", + error=True, + substrs=["MCP server is not running"], + ) From da1c9154eef40ab8cf8b7ddfc20318036ba783d8 Mon Sep 17 00:00:00 2001 From: Alexandre Perez Date: Thu, 9 Oct 2025 16:37:33 -0700 Subject: [PATCH 2/2] Fix formatting --- lldb/source/Commands/CommandObjectProtocolServer.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lldb/source/Commands/CommandObjectProtocolServer.cpp b/lldb/source/Commands/CommandObjectProtocolServer.cpp index ff9d38c84ed2d..1a950899ea1c0 100644 --- a/lldb/source/Commands/CommandObjectProtocolServer.cpp +++ b/lldb/source/Commands/CommandObjectProtocolServer.cpp @@ -164,10 +164,9 @@ class CommandObjectProtocolServerGet : public CommandObjectParsed { return; } - std::string address = - llvm::join(socket->GetListeningConnectionURI(), ", "); - result.AppendMessageWithFormatv( - "{0} server connection listeners: {1}", protocol, address); + std::string address = llvm::join(socket->GetListeningConnectionURI(), ", "); + result.AppendMessageWithFormatv("{0} server connection listeners: {1}", + protocol, address); result.SetStatus(eReturnStatusSuccessFinishNoResult); } }; @@ -181,8 +180,8 @@ CommandObjectProtocolServer::CommandObjectProtocolServer( interpreter))); LoadSubCommand("stop", CommandObjectSP( new CommandObjectProtocolServerStop(interpreter))); - LoadSubCommand("get", CommandObjectSP( - new CommandObjectProtocolServerGet(interpreter))); + LoadSubCommand( + "get", CommandObjectSP(new CommandObjectProtocolServerGet(interpreter))); } CommandObjectProtocolServer::~CommandObjectProtocolServer() = default;