Skip to content

Commit 6ed18d8

Browse files
authored
[lldb][mcp] Get the running MCP server connection information (#162752)
Currently AFAICT we don't have a way to get the MCP server socket after it started. So this change introduces a new `protocol-server` subcommand that allows us to query the location of a running server: ``` (lldb) protocol-server start MCP listen://localhost:0 MCP server started with connection listeners: connection://[::1]:36051, connection://[127.0.0.1]:36051 (lldb) protocol-server get MCP MCP server connection listeners: connection://[::1]:36051, connection://[127.0.0.1]:36051 (lldb) protocol-server stop MCP (lldb) protocol-server get MCP error: MCP server is not running ```
1 parent 10021c7 commit 6ed18d8

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

lldb/source/Commands/CommandObjectProtocolServer.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,57 @@ class CommandObjectProtocolServerStop : public CommandObjectParsed {
131131
}
132132
};
133133

134+
class CommandObjectProtocolServerGet : public CommandObjectParsed {
135+
public:
136+
CommandObjectProtocolServerGet(CommandInterpreter &interpreter)
137+
: CommandObjectParsed(interpreter, "protocol-server get",
138+
"get protocol server connection information",
139+
"protocol-server get <protocol>") {
140+
AddSimpleArgumentList(lldb::eArgTypeProtocol, eArgRepeatPlain);
141+
}
142+
143+
~CommandObjectProtocolServerGet() override = default;
144+
145+
protected:
146+
void DoExecute(Args &args, CommandReturnObject &result) override {
147+
if (args.GetArgumentCount() < 1) {
148+
result.AppendError("no protocol specified");
149+
return;
150+
}
151+
152+
llvm::StringRef protocol = args.GetArgumentAtIndex(0);
153+
ProtocolServer *server = ProtocolServer::GetOrCreate(protocol);
154+
if (!server) {
155+
result.AppendErrorWithFormatv(
156+
"unsupported protocol: {0}. Supported protocols are: {1}", protocol,
157+
llvm::join(ProtocolServer::GetSupportedProtocols(), ", "));
158+
return;
159+
}
160+
161+
Socket *socket = server->GetSocket();
162+
if (!socket) {
163+
result.AppendErrorWithFormatv("{0} server is not running", protocol);
164+
return;
165+
}
166+
167+
std::string address = llvm::join(socket->GetListeningConnectionURI(), ", ");
168+
result.AppendMessageWithFormatv("{0} server connection listeners: {1}",
169+
protocol, address);
170+
result.SetStatus(eReturnStatusSuccessFinishNoResult);
171+
}
172+
};
173+
134174
CommandObjectProtocolServer::CommandObjectProtocolServer(
135175
CommandInterpreter &interpreter)
136176
: CommandObjectMultiword(interpreter, "protocol-server",
137-
"Start and stop a protocol server.",
177+
"Start, stop, and query protocol servers.",
138178
"protocol-server") {
139179
LoadSubCommand("start", CommandObjectSP(new CommandObjectProtocolServerStart(
140180
interpreter)));
141181
LoadSubCommand("stop", CommandObjectSP(
142182
new CommandObjectProtocolServerStop(interpreter)));
183+
LoadSubCommand(
184+
"get", CommandObjectSP(new CommandObjectProtocolServerGet(interpreter)));
143185
}
144186

145187
CommandObjectProtocolServer::~CommandObjectProtocolServer() = default;

lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ llvm::Error ProtocolServerMCP::Stop() {
144144

145145
m_server.reset(nullptr);
146146
m_server_info_handle.Remove();
147+
m_listener.reset();
147148

148149
return llvm::Error::success();
149150
}

lldb/test/API/commands/protocol/TestMCPUnixSocket.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,16 @@ def test_unix_socket(self):
3232
startstr="MCP server started with connection listeners:",
3333
substrs=[f"unix-connect://{socket_file}"],
3434
)
35+
36+
self.expect(
37+
"protocol-server get MCP",
38+
startstr="MCP server connection listeners:",
39+
substrs=[f"unix-connect://{socket_file}"],
40+
)
41+
42+
self.runCmd("protocol-server stop MCP", check=False)
43+
self.expect(
44+
"protocol-server get MCP",
45+
error=True,
46+
substrs=["MCP server is not running"],
47+
)

0 commit comments

Comments
 (0)