LLDB mainline
Server.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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_PROTOCOL_MCP_SERVER_H
10#define LLDB_PROTOCOL_MCP_SERVER_H
11
13#include "lldb/Host/MainLoop.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/Support/Error.h"
19#include <mutex>
20
21namespace lldb_protocol::mcp {
22
24 : public lldb_private::JSONRPCTransport<Request, Response, Notification> {
25public:
26 using LogCallback = std::function<void(llvm::StringRef message)>;
27
29 std::string client_name, LogCallback log_callback = {})
30 : JSONRPCTransport(in, out), m_client_name(std::move(client_name)),
31 m_log_callback(log_callback) {}
32 virtual ~MCPTransport() = default;
33
34 void Log(llvm::StringRef message) override {
36 m_log_callback(llvm::formatv("{0}: {1}", m_client_name, message).str());
37 }
38
39private:
40 std::string m_client_name;
42};
43
44/// Information about this instance of lldb's MCP server for lldb-mcp to use to
45/// coordinate connecting an lldb-mcp client.
46struct ServerInfo {
47 std::string connection_uri;
49};
50llvm::json::Value toJSON(const ServerInfo &);
51bool fromJSON(const llvm::json::Value &, ServerInfo &, llvm::json::Path);
52
54public:
55 Server(std::string name, std::string version,
56 std::unique_ptr<MCPTransport> transport_up,
58 ~Server() = default;
59
60 using NotificationHandler = std::function<void(const Notification &)>;
61
62 void AddTool(std::unique_ptr<Tool> tool);
63 void AddResourceProvider(std::unique_ptr<ResourceProvider> resource_provider);
64 void AddNotificationHandler(llvm::StringRef method,
65 NotificationHandler handler);
66
67 llvm::Error Run();
68
69protected:
71
73 std::function<llvm::Expected<Response>(const Request &)>;
74
75 void AddRequestHandlers();
76
77 void AddRequestHandler(llvm::StringRef method, RequestHandler handler);
78
79 llvm::Expected<std::optional<Message>> HandleData(llvm::StringRef data);
80
81 llvm::Expected<Response> Handle(const Request &request);
82 void Handle(const Notification &notification);
83
84 llvm::Expected<Response> InitializeHandler(const Request &);
85
86 llvm::Expected<Response> ToolsListHandler(const Request &);
87 llvm::Expected<Response> ToolsCallHandler(const Request &);
88
89 llvm::Expected<Response> ResourcesListHandler(const Request &);
90 llvm::Expected<Response> ResourcesReadHandler(const Request &);
91
92 void Received(const Request &) override;
93 void Received(const Response &) override;
94 void Received(const Notification &) override;
95 void OnError(llvm::Error) override;
96 void OnClosed() override;
97
98 void TerminateLoop();
99
100private:
101 const std::string m_name;
102 const std::string m_version;
103
104 std::unique_ptr<MCPTransport> m_transport_up;
106
107 llvm::StringMap<std::unique_ptr<Tool>> m_tools;
108 std::vector<std::unique_ptr<ResourceProvider>> m_resource_providers;
109
110 llvm::StringMap<RequestHandler> m_request_handlers;
111 llvm::StringMap<NotificationHandler> m_notification_handlers;
112};
113
114} // namespace lldb_protocol::mcp
115
116#endif
A transport class for JSON RPC.
Implemented to handle incoming messages. (See Run() below).
Definition: JSONTransport.h:73
void Log(llvm::StringRef message) override
Definition: Server.h:34
MCPTransport(lldb::IOObjectSP in, lldb::IOObjectSP out, std::string client_name, LogCallback log_callback={})
Definition: Server.h:28
std::function< void(llvm::StringRef message)> LogCallback
Definition: Server.h:26
std::function< void(const Notification &)> NotificationHandler
Definition: Server.h:60
llvm::Expected< Response > ToolsCallHandler(const Request &)
Definition: Server.cpp:117
const std::string m_version
Definition: Server.h:102
const std::string m_name
Definition: Server.h:101
void AddTool(std::unique_ptr< Tool > tool)
Definition: Server.cpp:72
void AddResourceProvider(std::unique_ptr< ResourceProvider > resource_provider)
Definition: Server.cpp:78
lldb_private::MainLoop & m_loop
Definition: Server.h:105
llvm::StringMap< std::unique_ptr< Tool > > m_tools
Definition: Server.h:107
llvm::Expected< std::optional< Message > > HandleData(llvm::StringRef data)
std::vector< std::unique_ptr< ResourceProvider > > m_resource_providers
Definition: Server.h:108
llvm::StringMap< NotificationHandler > m_notification_handlers
Definition: Server.h:111
llvm::StringMap< RequestHandler > m_request_handlers
Definition: Server.h:110
void OnError(llvm::Error) override
Called when an error occurs while reading from the transport.
Definition: Server.cpp:251
llvm::Expected< Response > ResourcesListHandler(const Request &)
Definition: Server.cpp:148
llvm::Expected< Response > ResourcesReadHandler(const Request &)
Definition: Server.cpp:162
llvm::Expected< Response > Handle(const Request &request)
Definition: Server.cpp:50
llvm::Expected< Response > ToolsListHandler(const Request &)
Definition: Server.cpp:105
void AddRequestHandler(llvm::StringRef method, RequestHandler handler)
Definition: Server.cpp:85
std::unique_ptr< MCPTransport > m_transport_up
Definition: Server.h:104
std::function< llvm::Expected< Response >(const Request &)> RequestHandler
Definition: Server.h:73
void Received(const Request &) override
Definition: Server.cpp:219
void OnClosed() override
Called on EOF or client disconnect.
Definition: Server.cpp:256
ServerCapabilities GetCapabilities()
Definition: Server.cpp:198
void AddNotificationHandler(llvm::StringRef method, NotificationHandler handler)
Definition: Server.cpp:89
llvm::Expected< Response > InitializeHandler(const Request &)
Definition: Server.cpp:94
llvm::json::Value toJSON(const Request &)
Definition: Protocol.cpp:66
bool fromJSON(const llvm::json::Value &, Request &, llvm::json::Path)
Definition: Protocol.cpp:74
std::shared_ptr< lldb_private::IOObject > IOObjectSP
Definition: lldb-forward.h:365
uint64_t pid_t
Definition: lldb-types.h:83
A notification which does not expect a response.
Definition: Protocol.h:88
A request that expects a response.
Definition: Protocol.h:34
A response to a request, either an error or a result.
Definition: Protocol.h:76
Capabilities that a server may support.
Definition: Protocol.h:224
Information about this instance of lldb's MCP server for lldb-mcp to use to coordinate connecting an ...
Definition: Server.h:46
std::string connection_uri
Definition: Server.h:47