LLDB mainline
Client.cpp
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
12#include "llvm/Support/FormatVariadic.h"
13
14using namespace llvm;
15using namespace lldb_protocol::mcp;
16
17Client::Client(std::unique_ptr<MCPTransport> transport,
18 LogCallback log_callback)
19 : m_transport(std::move(transport)),
20 m_binder(std::make_unique<MCPBinder>(*m_transport)),
21 m_log_callback(std::move(log_callback)) {
23 m_binder->Bind<InitializeResult, InitializeParams>("initialize");
24 m_tools_list = m_binder->Bind<ListToolsResult, void>("tools/list");
25 m_tools_call = m_binder->Bind<CallToolResult, CallToolParams>("tools/call");
27 m_binder->Bind<ListResourcesResult, void>("resources/list");
29 m_binder->Bind<ReadResourceResult, ReadResourceParams>("resources/read");
30 m_notify_initialized = m_binder->Bind<void>("notifications/initialized");
31}
32
33llvm::Error Client::Run() {
34 m_binder->OnDisconnect(&Client::HandleDisconnect, this);
35 m_binder->OnError(&Client::HandleError, this);
36 return m_transport->RegisterMessageHandler(*m_binder);
37}
38
39void Client::SetDisconnectHandler(llvm::unique_function<void()> handler) {
40 m_disconnect_handler = std::move(handler);
41}
42
43void Client::SetErrorHandler(llvm::unique_function<void(llvm::Error)> handler) {
44 m_error_handler = std::move(handler);
45}
46
51
52void Client::HandleError(llvm::Error error) {
54 m_error_handler(std::move(error));
55 else
56 consumeError(std::move(error));
57}
58
61 m_initialize(params, std::move(reply));
62}
63
65 m_tools_list(std::move(reply));
66}
67
70 m_tools_call(params, std::move(reply));
71}
72
76
79 m_resources_read(params, std::move(reply));
80}
81
83
84void Client::CancelPendingRequests(llvm::StringRef reason) {
85 m_binder->FailPendingRequests(reason);
86}
87
88void Client::Log(llvm::StringRef message) {
90 m_log_callback(message);
91}
static llvm::raw_ostream & error(Stream &strm)
llvm::unique_function< void()> m_disconnect_handler
Definition Client.h:80
LogCallback m_log_callback
Definition Client.h:78
llvm::unique_function< void(llvm::Error)> m_error_handler
Definition Client.h:81
void CancelPendingRequests(llvm::StringRef reason)
Fails every request still awaiting a response, invoking its reply with an error.
Definition Client.cpp:84
void Initialize(const InitializeParams &params, Reply< InitializeResult > reply)
Outgoing MCP requests to the server.
Definition Client.cpp:59
void ResourcesRead(const ReadResourceParams &params, Reply< ReadResourceResult > reply)
Definition Client.cpp:77
void Log(llvm::StringRef message)
Definition Client.cpp:88
void ToolsList(Reply< ListToolsResult > reply)
Definition Client.cpp:64
Client(std::unique_ptr< MCPTransport > transport, LogCallback log_callback={})
Definition Client.cpp:17
lldb_private::transport::OutgoingRequest< ListToolsResult, void > m_tools_list
Definition Client.h:85
llvm::Error Run()
Registers the client with the transport's MainLoop.
Definition Client.cpp:33
lldb_private::transport::OutgoingRequest< ListResourcesResult, void > m_resources_list
Definition Client.h:89
void ToolsCall(const CallToolParams &params, Reply< CallToolResult > reply)
Definition Client.cpp:68
std::unique_ptr< MCPTransport > m_transport
Definition Client.h:76
lldb_private::transport::Reply< T > Reply
Definition Client.h:27
void HandleDisconnect()
Trampolines registered with the binder, which forward to the move-only handlers below.
Definition Client.cpp:47
lldb_private::transport::OutgoingRequest< ReadResourceResult, ReadResourceParams > m_resources_read
Definition Client.h:92
void SetDisconnectHandler(llvm::unique_function< void()> handler)
Sets a handler invoked when the transport disconnects (EOF).
Definition Client.cpp:39
void SetErrorHandler(llvm::unique_function< void(llvm::Error)> handler)
Sets a handler invoked on a transport-level error.
Definition Client.cpp:43
lldb_private::transport::OutgoingRequest< InitializeResult, InitializeParams > m_initialize
Definition Client.h:84
void HandleError(llvm::Error error)
Definition Client.cpp:52
void ResourcesList(Reply< ListResourcesResult > reply)
Definition Client.cpp:73
lldb_private::transport::OutgoingEvent< void > m_notify_initialized
Definition Client.h:93
lldb_private::transport::OutgoingRequest< CallToolResult, CallToolParams > m_tools_call
Definition Client.h:87
void NotifyInitialized()
MCP notifications.
Definition Client.cpp:82
lldb_private::transport::Binder< ProtocolDescriptor > MCPBinder
Definition Transport.h:76
llvm::unique_function< void(llvm::StringRef message)> LogCallback
Generic logging callback, to allow the MCP server / client / transport layer to be independent of the...
Definition Transport.h:81
Used by the client to invoke a tool provided by the server.
Definition Protocol.h:296
The server’s response to a tool call.
Definition Protocol.h:304
After receiving an initialize request from the client, the server sends this response.
Definition Protocol.h:260
The server’s response to a resources/list request from the client.
Definition Protocol.h:131
The server's response to a tools/list request from the client.
Definition Protocol.h:285
Sent from the client to the server, to read a specific resource URI.
Definition Protocol.h:156
The server's response to a resources/read request from the client.
Definition Protocol.h:166