LLDB mainline
ProtocolServerMCP.h
Go to the documentation of this file.
1//===- ProtocolServerMCP.h ------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_SOURCE_PLUGINS_PROTOCOL_MCP_PROTOCOLSERVERMCP_H
10#define LLDB_SOURCE_PLUGINS_PROTOCOL_MCP_PROTOCOLSERVERMCP_H
11
13#include "lldb/Host/MainLoop.h"
14#include "lldb/Host/Socket.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/Error.h"
18#include <cstddef>
19#include <memory>
20#include <mutex>
21#include <thread>
22#include <vector>
23
25
26/// Installs the standard LLDB tool and resource-provider set on \p server.
27/// Sharing one installer keeps every MCP server consistent, whether it runs in
28/// the plugin or is hosted in-process by an embedder.
30
32
33 using ServerUP = std::unique_ptr<lldb_protocol::mcp::Server>;
34
36
37public:
39 ~ProtocolServerMCP() override;
40
41 llvm::Error Start(ProtocolServer::Connection connection) override;
42 llvm::Error Stop() override;
43
44 static void Initialize();
45 static void Terminate();
46
47 static llvm::StringRef GetPluginNameStatic() { return "MCP"; }
48 static llvm::StringRef GetPluginDescriptionStatic();
49
51
52 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
53
54 Socket *GetSocket() const override { return m_listener.get(); }
55
56protected:
57 // This adds tools and resource providers that
58 // are specific to this server. Overridable by the unit tests.
59 virtual void Extend(lldb_protocol::mcp::Server &server) const;
60
61private:
62 void AcceptCallback(std::unique_ptr<Socket> socket);
63
64 bool m_running = false;
65
67 std::thread m_loop_thread;
68 std::mutex m_mutex;
69 size_t m_client_count = 0;
70
71 std::unique_ptr<Socket> m_listener;
72 std::vector<ReadHandleUP> m_accept_handles;
73
76};
77
78} // namespace lldb_private::mcp
79
80#endif
std::unique_ptr< ReadHandle > ReadHandleUP
std::unique_ptr< Socket > m_listener
virtual void Extend(lldb_protocol::mcp::Server &server) const
llvm::Error Start(ProtocolServer::Connection connection) override
Socket * GetSocket() const override
std::unique_ptr< lldb_protocol::mcp::Server > ServerUP
std::vector< ReadHandleUP > m_accept_handles
static lldb::ProtocolServerUP CreateInstance()
llvm::StringRef GetPluginName() override
void AcceptCallback(std::unique_ptr< Socket > socket)
static llvm::StringRef GetPluginNameStatic()
lldb_protocol::mcp::ServerInfoHandle m_server_info_handle
static llvm::StringRef GetPluginDescriptionStatic()
A handle that tracks the server info on disk and cleans up the disk record once it is no longer refer...
Definition Server.h:109
void PopulateServer(lldb_protocol::mcp::Server &server)
Installs the standard LLDB tool and resource-provider set on server.
MainLoopPosix MainLoop
Definition MainLoop.h:20
std::unique_ptr< lldb_private::ProtocolServer > ProtocolServerUP