Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.h
213845 views
//===- ProtocolServerMCP.h ------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLDB_PLUGINS_PROTOCOL_MCP_PROTOCOLSERVERMCP_H9#define LLDB_PLUGINS_PROTOCOL_MCP_PROTOCOLSERVERMCP_H1011#include "Protocol.h"12#include "Resource.h"13#include "Tool.h"14#include "lldb/Core/ProtocolServer.h"15#include "lldb/Host/MainLoop.h"16#include "lldb/Host/Socket.h"17#include "llvm/ADT/StringMap.h"18#include <thread>1920namespace lldb_private::mcp {2122class ProtocolServerMCP : public ProtocolServer {23public:24ProtocolServerMCP();25virtual ~ProtocolServerMCP() override;2627virtual llvm::Error Start(ProtocolServer::Connection connection) override;28virtual llvm::Error Stop() override;2930static void Initialize();31static void Terminate();3233static llvm::StringRef GetPluginNameStatic() { return "MCP"; }34static llvm::StringRef GetPluginDescriptionStatic();3536static lldb::ProtocolServerUP CreateInstance();3738llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }3940Socket *GetSocket() const override { return m_listener.get(); }4142protected:43using RequestHandler = std::function<llvm::Expected<protocol::Response>(44const protocol::Request &)>;45using NotificationHandler =46std::function<void(const protocol::Notification &)>;4748void AddTool(std::unique_ptr<Tool> tool);49void AddResourceProvider(std::unique_ptr<ResourceProvider> resource_provider);5051void AddRequestHandler(llvm::StringRef method, RequestHandler handler);52void AddNotificationHandler(llvm::StringRef method,53NotificationHandler handler);5455private:56void AcceptCallback(std::unique_ptr<Socket> socket);5758llvm::Expected<std::optional<protocol::Message>>59HandleData(llvm::StringRef data);6061llvm::Expected<protocol::Response> Handle(protocol::Request request);62void Handle(protocol::Notification notification);6364llvm::Expected<protocol::Response>65InitializeHandler(const protocol::Request &);6667llvm::Expected<protocol::Response>68ToolsListHandler(const protocol::Request &);69llvm::Expected<protocol::Response>70ToolsCallHandler(const protocol::Request &);7172llvm::Expected<protocol::Response>73ResourcesListHandler(const protocol::Request &);74llvm::Expected<protocol::Response>75ResourcesReadHandler(const protocol::Request &);7677protocol::Capabilities GetCapabilities();7879llvm::StringLiteral kName = "lldb-mcp";80llvm::StringLiteral kVersion = "0.1.0";8182bool m_running = false;8384MainLoop m_loop;85std::thread m_loop_thread;8687std::unique_ptr<Socket> m_listener;88std::vector<MainLoopBase::ReadHandleUP> m_listen_handlers;8990struct Client {91lldb::IOObjectSP io_sp;92MainLoopBase::ReadHandleUP read_handle_up;93std::string buffer;94};95llvm::Error ReadCallback(Client &client);96std::vector<std::unique_ptr<Client>> m_clients;9798std::mutex m_server_mutex;99llvm::StringMap<std::unique_ptr<Tool>> m_tools;100std::vector<std::unique_ptr<ResourceProvider>> m_resource_providers;101102llvm::StringMap<RequestHandler> m_request_handlers;103llvm::StringMap<NotificationHandler> m_notification_handlers;104};105} // namespace lldb_private::mcp106107#endif108109110