diff --git a/lldb/source/Plugins/Protocol/MCP/MCPError.h b/lldb/include/lldb/Protocol/MCP/MCPError.h similarity index 85% rename from lldb/source/Plugins/Protocol/MCP/MCPError.h rename to lldb/include/lldb/Protocol/MCP/MCPError.h index c93e95957493..2bdbb9b7a687 100644 --- a/lldb/source/Plugins/Protocol/MCP/MCPError.h +++ b/lldb/include/lldb/Protocol/MCP/MCPError.h @@ -1,4 +1,4 @@ -//===-- MCPError.h --------------------------------------------------------===// +//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,12 +6,14 @@ // //===----------------------------------------------------------------------===// +#ifndef LLDB_PROTOCOL_MCP_MCPERROR_H +#define LLDB_PROTOCOL_MCP_MCPERROR_H + #include "lldb/Protocol/MCP/Protocol.h" #include "llvm/Support/Error.h" -#include "llvm/Support/FormatVariadic.h" #include -namespace lldb_private::mcp { +namespace lldb_protocol::mcp { class MCPError : public llvm::ErrorInfo { public: @@ -47,4 +49,6 @@ private: std::string m_uri; }; -} // namespace lldb_private::mcp +} // namespace lldb_protocol::mcp + +#endif diff --git a/lldb/include/lldb/Protocol/MCP/Resource.h b/lldb/include/lldb/Protocol/MCP/Resource.h new file mode 100644 index 000000000000..4835d340cd4c --- /dev/null +++ b/lldb/include/lldb/Protocol/MCP/Resource.h @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_PROTOCOL_MCP_RESOURCE_H +#define LLDB_PROTOCOL_MCP_RESOURCE_H + +#include "lldb/Protocol/MCP/Protocol.h" +#include + +namespace lldb_protocol::mcp { + +class ResourceProvider { +public: + ResourceProvider() = default; + virtual ~ResourceProvider() = default; + + virtual std::vector GetResources() const = 0; + virtual llvm::Expected + ReadResource(llvm::StringRef uri) const = 0; +}; + +} // namespace lldb_protocol::mcp + +#endif diff --git a/lldb/include/lldb/Protocol/MCP/Tool.h b/lldb/include/lldb/Protocol/MCP/Tool.h new file mode 100644 index 000000000000..96669d135716 --- /dev/null +++ b/lldb/include/lldb/Protocol/MCP/Tool.h @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_PROTOCOL_MCP_TOOL_H +#define LLDB_PROTOCOL_MCP_TOOL_H + +#include "lldb/Protocol/MCP/Protocol.h" +#include "llvm/Support/JSON.h" +#include + +namespace lldb_protocol::mcp { + +class Tool { +public: + Tool(std::string name, std::string description); + virtual ~Tool() = default; + + virtual llvm::Expected + Call(const lldb_protocol::mcp::ToolArguments &args) = 0; + + virtual std::optional GetSchema() const { + return llvm::json::Object{{"type", "object"}}; + } + + lldb_protocol::mcp::ToolDefinition GetDefinition() const; + + const std::string &GetName() { return m_name; } + +private: + std::string m_name; + std::string m_description; +}; + +} // namespace lldb_protocol::mcp + +#endif diff --git a/lldb/source/Plugins/Protocol/MCP/CMakeLists.txt b/lldb/source/Plugins/Protocol/MCP/CMakeLists.txt index 2740c0825a8c..87565e693158 100644 --- a/lldb/source/Plugins/Protocol/MCP/CMakeLists.txt +++ b/lldb/source/Plugins/Protocol/MCP/CMakeLists.txt @@ -1,5 +1,4 @@ add_lldb_library(lldbPluginProtocolServerMCP PLUGIN - MCPError.cpp ProtocolServerMCP.cpp Resource.cpp Tool.cpp diff --git a/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp b/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp index 4d517ee8158c..c9fe474d45c4 100644 --- a/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp +++ b/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp @@ -7,8 +7,11 @@ //===----------------------------------------------------------------------===// #include "ProtocolServerMCP.h" -#include "MCPError.h" +#include "Resource.h" +#include "Tool.h" #include "lldb/Core/PluginManager.h" +#include "lldb/Protocol/MCP/MCPError.h" +#include "lldb/Protocol/MCP/Tool.h" #include "lldb/Utility/LLDBLog.h" #include "lldb/Utility/Log.h" #include "llvm/ADT/StringExtras.h" @@ -18,6 +21,7 @@ using namespace lldb_private; using namespace lldb_private::mcp; +using namespace lldb_protocol::mcp; using namespace llvm; LLDB_PLUGIN_DEFINE(ProtocolServerMCP) @@ -112,7 +116,7 @@ void ProtocolServerMCP::AcceptCallback(std::unique_ptr socket) { auto read_handle_up = m_loop.RegisterReadObject( io_sp, [this, client](MainLoopBase &loop) { - if (Error error = ReadCallback(*client)) { + if (llvm::Error error = ReadCallback(*client)) { LLDB_LOG_ERROR(GetLog(LLDBLog::Host), std::move(error), "{0}"); client->read_handle_up.reset(); } diff --git a/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.h b/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.h index 611e62af5c8c..2ea9585a2334 100644 --- a/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.h +++ b/lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.h @@ -9,12 +9,12 @@ #ifndef LLDB_PLUGINS_PROTOCOL_MCP_PROTOCOLSERVERMCP_H #define LLDB_PLUGINS_PROTOCOL_MCP_PROTOCOLSERVERMCP_H -#include "Resource.h" -#include "Tool.h" #include "lldb/Core/ProtocolServer.h" #include "lldb/Host/MainLoop.h" #include "lldb/Host/Socket.h" #include "lldb/Protocol/MCP/Protocol.h" +#include "lldb/Protocol/MCP/Resource.h" +#include "lldb/Protocol/MCP/Tool.h" #include "llvm/ADT/StringMap.h" #include @@ -47,8 +47,9 @@ protected: using NotificationHandler = std::function; - void AddTool(std::unique_ptr tool); - void AddResourceProvider(std::unique_ptr resource_provider); + void AddTool(std::unique_ptr tool); + void AddResourceProvider( + std::unique_ptr resource_provider); void AddRequestHandler(llvm::StringRef method, RequestHandler handler); void AddNotificationHandler(llvm::StringRef method, @@ -99,8 +100,9 @@ private: std::vector> m_clients; std::mutex m_server_mutex; - llvm::StringMap> m_tools; - std::vector> m_resource_providers; + llvm::StringMap> m_tools; + std::vector> + m_resource_providers; llvm::StringMap m_request_handlers; llvm::StringMap m_notification_handlers; diff --git a/lldb/source/Plugins/Protocol/MCP/Resource.cpp b/lldb/source/Plugins/Protocol/MCP/Resource.cpp index bc39e1b0c0ed..e94d2cdd65e0 100644 --- a/lldb/source/Plugins/Protocol/MCP/Resource.cpp +++ b/lldb/source/Plugins/Protocol/MCP/Resource.cpp @@ -5,13 +5,14 @@ //===----------------------------------------------------------------------===// #include "Resource.h" -#include "MCPError.h" #include "lldb/Core/Debugger.h" #include "lldb/Core/Module.h" +#include "lldb/Protocol/MCP/MCPError.h" #include "lldb/Target/Platform.h" using namespace lldb_private; using namespace lldb_private::mcp; +using namespace lldb_protocol::mcp; namespace { struct DebuggerResource { diff --git a/lldb/source/Plugins/Protocol/MCP/Resource.h b/lldb/source/Plugins/Protocol/MCP/Resource.h index 0066f2f8e1b0..e2382a74f796 100644 --- a/lldb/source/Plugins/Protocol/MCP/Resource.h +++ b/lldb/source/Plugins/Protocol/MCP/Resource.h @@ -10,22 +10,13 @@ #define LLDB_PLUGINS_PROTOCOL_MCP_RESOURCE_H #include "lldb/Protocol/MCP/Protocol.h" +#include "lldb/Protocol/MCP/Resource.h" #include "lldb/lldb-private.h" #include namespace lldb_private::mcp { -class ResourceProvider { -public: - ResourceProvider() = default; - virtual ~ResourceProvider() = default; - - virtual std::vector GetResources() const = 0; - virtual llvm::Expected - ReadResource(llvm::StringRef uri) const = 0; -}; - -class DebuggerResourceProvider : public ResourceProvider { +class DebuggerResourceProvider : public lldb_protocol::mcp::ResourceProvider { public: using ResourceProvider::ResourceProvider; virtual ~DebuggerResourceProvider() = default; diff --git a/lldb/source/Plugins/Protocol/MCP/Tool.cpp b/lldb/source/Plugins/Protocol/MCP/Tool.cpp index 9df5ce843e0f..143470702a6f 100644 --- a/lldb/source/Plugins/Protocol/MCP/Tool.cpp +++ b/lldb/source/Plugins/Protocol/MCP/Tool.cpp @@ -41,20 +41,6 @@ static lldb_protocol::mcp::TextResult createTextResult(std::string output, } // namespace -Tool::Tool(std::string name, std::string description) - : m_name(std::move(name)), m_description(std::move(description)) {} - -lldb_protocol::mcp::ToolDefinition Tool::GetDefinition() const { - lldb_protocol::mcp::ToolDefinition definition; - definition.name = m_name; - definition.description = m_description; - - if (std::optional input_schema = GetSchema()) - definition.inputSchema = *input_schema; - - return definition; -} - llvm::Expected CommandTool::Call(const lldb_protocol::mcp::ToolArguments &args) { if (!std::holds_alternative(args)) diff --git a/lldb/source/Plugins/Protocol/MCP/Tool.h b/lldb/source/Plugins/Protocol/MCP/Tool.h index ec1d83a51096..b7b1756eb38d 100644 --- a/lldb/source/Plugins/Protocol/MCP/Tool.h +++ b/lldb/source/Plugins/Protocol/MCP/Tool.h @@ -11,35 +11,15 @@ #include "lldb/Core/Debugger.h" #include "lldb/Protocol/MCP/Protocol.h" +#include "lldb/Protocol/MCP/Tool.h" #include "llvm/Support/JSON.h" #include namespace lldb_private::mcp { -class Tool { +class CommandTool : public lldb_protocol::mcp::Tool { public: - Tool(std::string name, std::string description); - virtual ~Tool() = default; - - virtual llvm::Expected - Call(const lldb_protocol::mcp::ToolArguments &args) = 0; - - virtual std::optional GetSchema() const { - return llvm::json::Object{{"type", "object"}}; - } - - lldb_protocol::mcp::ToolDefinition GetDefinition() const; - - const std::string &GetName() { return m_name; } - -private: - std::string m_name; - std::string m_description; -}; - -class CommandTool : public mcp::Tool { -public: - using mcp::Tool::Tool; + using lldb_protocol::mcp::Tool::Tool; ~CommandTool() = default; virtual llvm::Expected diff --git a/lldb/source/Protocol/MCP/CMakeLists.txt b/lldb/source/Protocol/MCP/CMakeLists.txt index b197dc4c0430..f1b1098e064a 100644 --- a/lldb/source/Protocol/MCP/CMakeLists.txt +++ b/lldb/source/Protocol/MCP/CMakeLists.txt @@ -1,8 +1,11 @@ add_lldb_library(lldbProtocolMCP NO_PLUGIN_DEPENDENCIES + MCPError.cpp Protocol.cpp + Tool.cpp LINK_COMPONENTS Support LINK_LIBS lldbUtility ) + diff --git a/lldb/source/Plugins/Protocol/MCP/MCPError.cpp b/lldb/source/Protocol/MCP/MCPError.cpp similarity index 89% rename from lldb/source/Plugins/Protocol/MCP/MCPError.cpp rename to lldb/source/Protocol/MCP/MCPError.cpp index 1e358058fa86..c610e882abf5 100644 --- a/lldb/source/Plugins/Protocol/MCP/MCPError.cpp +++ b/lldb/source/Protocol/MCP/MCPError.cpp @@ -1,4 +1,4 @@ -//===-- MCPError.cpp ------------------------------------------------------===// +//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,12 +6,12 @@ // //===----------------------------------------------------------------------===// -#include "MCPError.h" +#include "lldb/Protocol/MCP/MCPError.h" #include "llvm/Support/Error.h" #include "llvm/Support/raw_ostream.h" #include -using namespace lldb_private::mcp; +using namespace lldb_protocol::mcp; char MCPError::ID; char UnsupportedURI::ID; diff --git a/lldb/source/Protocol/MCP/Tool.cpp b/lldb/source/Protocol/MCP/Tool.cpp new file mode 100644 index 000000000000..8e01f2bd5908 --- /dev/null +++ b/lldb/source/Protocol/MCP/Tool.cpp @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// 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 "lldb/Protocol/MCP/Tool.h" + +using namespace lldb_protocol::mcp; + +Tool::Tool(std::string name, std::string description) + : m_name(std::move(name)), m_description(std::move(description)) {} + +lldb_protocol::mcp::ToolDefinition Tool::GetDefinition() const { + lldb_protocol::mcp::ToolDefinition definition; + definition.name = m_name; + definition.description = m_description; + + if (std::optional input_schema = GetSchema()) + definition.inputSchema = *input_schema; + + return definition; +} diff --git a/lldb/unittests/ProtocolServer/ProtocolMCPServerTest.cpp b/lldb/unittests/ProtocolServer/ProtocolMCPServerTest.cpp index b1cc21a5b0c3..dc3b63ab6546 100644 --- a/lldb/unittests/ProtocolServer/ProtocolMCPServerTest.cpp +++ b/lldb/unittests/ProtocolServer/ProtocolMCPServerTest.cpp @@ -7,15 +7,16 @@ //===----------------------------------------------------------------------===// #include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h" -#include "Plugins/Protocol/MCP/MCPError.h" #include "Plugins/Protocol/MCP/ProtocolServerMCP.h" #include "TestingSupport/Host/SocketTestUtilities.h" #include "TestingSupport/SubsystemRAII.h" +#include "lldb/Core/Debugger.h" #include "lldb/Core/ProtocolServer.h" #include "lldb/Host/FileSystem.h" #include "lldb/Host/HostInfo.h" #include "lldb/Host/JSONTransport.h" #include "lldb/Host/Socket.h" +#include "lldb/Protocol/MCP/MCPError.h" #include "lldb/Protocol/MCP/Protocol.h" #include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" @@ -44,9 +45,9 @@ public: }; /// Test tool that returns it argument as text. -class TestTool : public mcp::Tool { +class TestTool : public Tool { public: - using mcp::Tool::Tool; + using Tool::Tool; virtual llvm::Expected Call(const ToolArguments &args) override { std::string argument; @@ -63,8 +64,8 @@ public: } }; -class TestResourceProvider : public mcp::ResourceProvider { - using mcp::ResourceProvider::ResourceProvider; +class TestResourceProvider : public ResourceProvider { + using ResourceProvider::ResourceProvider; virtual std::vector GetResources() const override { std::vector resources; @@ -82,7 +83,7 @@ class TestResourceProvider : public mcp::ResourceProvider { virtual llvm::Expected ReadResource(llvm::StringRef uri) const override { if (uri != "lldb://foo/bar") - return llvm::make_error(uri.str()); + return llvm::make_error(uri.str()); ResourceContents contents; contents.uri = "lldb://foo/bar"; @@ -96,9 +97,9 @@ class TestResourceProvider : public mcp::ResourceProvider { }; /// Test tool that returns an error. -class ErrorTool : public mcp::Tool { +class ErrorTool : public Tool { public: - using mcp::Tool::Tool; + using Tool::Tool; virtual llvm::Expected Call(const ToolArguments &args) override { return llvm::createStringError("error"); @@ -106,9 +107,9 @@ public: }; /// Test tool that fails but doesn't return an error. -class FailTool : public mcp::Tool { +class FailTool : public Tool { public: - using mcp::Tool::Tool; + using Tool::Tool; virtual llvm::Expected Call(const ToolArguments &args) override { TextResult text_result;