================
@@ -0,0 +1,280 @@
+//===- ProtocolServerMCP.cpp 
----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "ProtocolServerMCP.h"
+#include "MCPError.h"
+#include "lldb/Core/PluginManager.h"
+#include "lldb/Utility/LLDBLog.h"
+#include "lldb/Utility/Log.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/Threading.h"
+#include <thread>
+
+using namespace lldb_private;
+using namespace lldb_private::mcp;
+using namespace llvm;
+
+LLDB_PLUGIN_DEFINE(ProtocolServerMCP)
+
+ProtocolServerMCP::ProtocolServerMCP(Debugger &debugger)
+    : ProtocolServer(), m_debugger(debugger) {
+  AddHandler("initialize", std::bind(&ProtocolServerMCP::InitializeHandler,
+                                     this, std::placeholders::_1));
+  AddHandler("tools/list", std::bind(&ProtocolServerMCP::ToolsListHandler, 
this,
+                                     std::placeholders::_1));
+  AddHandler("tools/call", std::bind(&ProtocolServerMCP::ToolsCallHandler, 
this,
+                                     std::placeholders::_1));
+  AddTool(std::make_unique<LLDBCommandTool>(
+      "lldb_command", "Run an lldb command.", m_debugger));
+}
+
+ProtocolServerMCP::~ProtocolServerMCP() { llvm::consumeError(Stop()); }
+
+void ProtocolServerMCP::Initialize() {
+  PluginManager::RegisterPlugin(GetPluginNameStatic(),
+                                GetPluginDescriptionStatic(), CreateInstance);
+}
+
+void ProtocolServerMCP::Terminate() {
+  PluginManager::UnregisterPlugin(CreateInstance);
+}
+
+lldb::ProtocolServerSP ProtocolServerMCP::CreateInstance(Debugger &debugger) {
+  return std::make_shared<ProtocolServerMCP>(debugger);
+}
+
+llvm::StringRef ProtocolServerMCP::GetPluginDescriptionStatic() {
+  return "MCP Server.";
+}
+
+llvm::Expected<protocol::Response>
+ProtocolServerMCP::Handle(protocol::Request request) {
+  auto it = m_handlers.find(request.method);
+  if (it != m_handlers.end())
+    return it->second(request);
+
+  return make_error<MCPError>(
+      llvm::formatv("no handler for request: {0}", request.method).str(), 1);
+}
+
+llvm::Error ProtocolServerMCP::Start(ProtocolServer::Connection connection) {
+  std::lock_guard<std::mutex> guard(m_server_mutex);
+
+  if (m_running)
+    return llvm::createStringError("server already running");
+
+  Status status;
+  m_listener = Socket::Create(connection.protocol, status);
+  if (status.Fail())
+    return status.takeError();
+
+  status = m_listener->Listen(connection.name, /*backlog=*/5);
+  if (status.Fail())
+    return status.takeError();
+
+  std::string address =
+      llvm::join(m_listener->GetListeningConnectionURI(), ", ");
+  Log *log = GetLog(LLDBLog::Host);
+  LLDB_LOG(log, "MCP server started with connection listeners: {0}", address);
+
+  auto handles = m_listener->Accept(m_loop, [=](std::unique_ptr<Socket> sock) {
+    std::lock_guard<std::mutex> guard(m_server_mutex);
+
+    const std::string client_name =
+        llvm::formatv("client-{0}", m_clients.size() + 1).str();
+    LLDB_LOG(log, "client {0} connected", client_name);
+
+    lldb::IOObjectSP io(std::move(sock));
+
+    m_clients.emplace_back(io, [=]() {
+      llvm::set_thread_name(client_name + "-runloop");
+      if (auto Err = Run(std::make_unique<JSONRPCTransport>(io, io)))
+        LLDB_LOG_ERROR(GetLog(LLDBLog::Host), std::move(Err), "MCP Error: 
{0}");
+    });
----------------
ashgti wrote:

I think it looks like we're only supporting connecting via sockets.

Instead of spawning a thread per-client, should we have the clients register 
their handles to the RunLoop and handle the requests that way? I think it would 
result in fewer threads. I think all the clients are sharing a connection to 
the same debugger instance and this may help prevent races between clients.

https://github.com/llvm/llvm-project/pull/143628
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to