LLDB mainline
Client.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_CLIENT_H
10#define LLDB_PROTOCOL_MCP_CLIENT_H
11
14#include "llvm/ADT/FunctionExtras.h"
15#include "llvm/Support/Error.h"
16#include <memory>
17#include <string>
18
20
21/// An MCP client: the counterpart to Server. It speaks the MCP protocol to a
22/// remote server over a transport and exposes the protocol's requests as typed,
23/// asynchronous calls. Each request completes by invoking its Reply on the
24/// transport's MainLoop.
25class Client {
26public:
27 template <typename T> using Reply = lldb_private::transport::Reply<T>;
28
29 Client(std::unique_ptr<MCPTransport> transport,
30 LogCallback log_callback = {});
31 ~Client() = default;
32
33 Client(const Client &) = delete;
34 Client &operator=(const Client &) = delete;
35
36 /// Registers the client with the transport's MainLoop. Must be called before
37 /// issuing any request.
38 llvm::Error Run();
39
40 /// Sets a handler invoked when the transport disconnects (EOF).
41 void SetDisconnectHandler(llvm::unique_function<void()> handler);
42 /// Sets a handler invoked on a transport-level error.
43 void SetErrorHandler(llvm::unique_function<void(llvm::Error)> handler);
44
45 /// Outgoing MCP requests to the server. Each sends its request and invokes
46 /// its Reply asynchronously when the response arrives.
47 /// @{
48 void Initialize(const InitializeParams &params,
51 void ToolsCall(const CallToolParams &params, Reply<CallToolResult> reply);
53 void ResourcesRead(const ReadResourceParams &params,
55 /// @}
56
57 /// MCP notifications.
58 void NotifyInitialized();
59
60 /// Fails every request still awaiting a response, invoking its reply with an
61 /// error. Use before teardown so abandoned replies are satisfied rather than
62 /// destroyed unanswered.
63 void CancelPendingRequests(llvm::StringRef reason);
64
65private:
66 void Log(llvm::StringRef message);
67
68 /// Trampolines registered with the binder, which forward to the move-only
69 /// handlers below. The binder requires copyable callables, so the handlers
70 /// cannot be registered with it directly.
71 /// @{
72 void HandleDisconnect();
73 void HandleError(llvm::Error error);
74 /// @}
75
76 std::unique_ptr<MCPTransport> m_transport;
79
80 llvm::unique_function<void()> m_disconnect_handler;
81 llvm::unique_function<void(llvm::Error)> m_error_handler;
82
94};
95
96} // namespace lldb_protocol::mcp
97
98#endif
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
Client & operator=(const Client &)=delete
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
Client(const Client &)=delete
lldb_private::transport::OutgoingRequest< CallToolResult, CallToolParams > m_tools_call
Definition Client.h:87
void NotifyInitialized()
MCP notifications.
Definition Client.cpp:82
std::conditional_t< std::is_void_v< T >, llvm::unique_function< void(llvm::Error)>, llvm::unique_function< void(llvm::Expected< T >)> > Reply
A handler for the response to an outgoing request.
typename detail::event_t< P >::type OutgoingEvent
A function to send an outgoing event.
typename detail::request_t< R, P >::type OutgoingRequest
std::unique_ptr< MCPBinder > MCPBinderUP
Definition Transport.h:77
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
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