LLDB mainline
Transport.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_TRANSPORT_H
10#define LLDB_PROTOCOL_MCP_TRANSPORT_H
11
15#include "lldb/lldb-forward.h"
16#include "llvm/ADT/FunctionExtras.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/Error.h"
19#include <sys/types.h>
20
21namespace lldb_protocol::mcp {
22
24 using Id = int64_t;
25 using Req = Request;
26 using Resp = Response;
28
29 static inline Id InitialId() { return 0; }
30 static inline Request Make(Id id, llvm::StringRef method,
31 std::optional<llvm::json::Value> params) {
32 return Request{id, method.str(), params};
33 }
34 static inline Notification Make(llvm::StringRef method,
35 std::optional<llvm::json::Value> params) {
36 return Notification{method.str(), params};
37 }
38 static inline Response Make(Req req, llvm::Error error) {
39 lldb_protocol::mcp::Error protocol_error;
40 llvm::handleAllErrors(
41 std::move(error), [&](const llvm::ErrorInfoBase &err) {
42 std::error_code cerr = err.convertToErrorCode();
43 protocol_error.code =
44 cerr == llvm::inconvertibleErrorCode()
46 : cerr.value();
47 protocol_error.message = err.message();
48 });
49
50 return Response{req.id, std::move(protocol_error)};
51 }
52 static inline Response Make(Req req,
53 std::optional<llvm::json::Value> result) {
54 return Response{req.id, std::move(result)};
55 }
56 static inline Id KeyFor(Response r) { return std::get<Id>(r.id); }
57 static inline std::string KeyFor(Request r) { return r.method; }
58 static inline std::string KeyFor(Notification n) { return n.method; }
59 static inline std::optional<llvm::json::Value> Extract(Request r) {
60 return r.params;
61 }
62 static inline llvm::Expected<llvm::json::Value> Extract(Response r) {
64 std::get_if<lldb_protocol::mcp::Error>(&r.result))
65 return llvm::make_error<lldb_protocol::mcp::MCPError>(error->message,
66 error->code);
67 return std::get<llvm::json::Value>(r.result);
68 }
69 static inline std::optional<llvm::json::Value> Extract(Notification n) {
70 return n.params;
71 }
72};
73
74/// Generic transport that uses the MCP protocol.
77using MCPBinderUP = std::unique_ptr<MCPBinder>;
78
79/// Generic logging callback, to allow the MCP server / client / transport layer
80/// to be independent of the lldb log implementation.
81using LogCallback = llvm::unique_function<void(llvm::StringRef message)>;
82
83class Transport final
84 : public lldb_private::transport::JSONRPCTransport<ProtocolDescriptor> {
85public:
87 LogCallback log_callback = {});
88 virtual ~Transport() = default;
89
90 /// Transport is not copyable.
91 /// @{
92 Transport(const Transport &) = delete;
93 void operator=(const Transport &) = delete;
94 /// @}
95
96 void Log(llvm::StringRef message) override;
97
98private:
100};
101
102} // namespace lldb_protocol::mcp
103
104#endif
static llvm::raw_ostream & error(Stream &strm)
Binder collects a table of functions that handle calls.
A transport class for JSON RPC.
A transport is responsible for maintaining the connection to a client application,...
void Log(llvm::StringRef message) override
Definition Transport.cpp:20
Transport(lldb::IOObjectSP in, lldb::IOObjectSP out, LogCallback log_callback={})
Definition Transport.cpp:16
virtual ~Transport()=default
void operator=(const Transport &)=delete
Transport(const Transport &)=delete
Transport is not copyable.
std::unique_ptr< MCPBinder > MCPBinderUP
Definition Transport.h:77
lldb_private::transport::Binder< ProtocolDescriptor > MCPBinder
Definition Transport.h:76
@ eErrorCodeInternalError
Internal JSON-RPC error.
Definition Protocol.h:58
lldb_private::transport::JSONTransport< ProtocolDescriptor > MCPTransport
Generic transport that uses the MCP protocol.
Definition Transport.h:75
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
std::shared_ptr< lldb_private::IOObject > IOObjectSP
std::string message
A short description of the error.
Definition Protocol.h:66
int64_t code
The error type that occurred.
Definition Protocol.h:63
A notification which does not expect a response.
Definition Protocol.h:89
std::optional< llvm::json::Value > params
The notification's params.
Definition Protocol.h:93
std::string method
The method to be invoked.
Definition Protocol.h:91
static llvm::Expected< llvm::json::Value > Extract(Response r)
Definition Transport.h:62
static std::optional< llvm::json::Value > Extract(Request r)
Definition Transport.h:59
static Response Make(Req req, llvm::Error error)
Definition Transport.h:38
static std::string KeyFor(Notification n)
Definition Transport.h:58
static Notification Make(llvm::StringRef method, std::optional< llvm::json::Value > params)
Definition Transport.h:34
static std::optional< llvm::json::Value > Extract(Notification n)
Definition Transport.h:69
static Response Make(Req req, std::optional< llvm::json::Value > result)
Definition Transport.h:52
static Request Make(Id id, llvm::StringRef method, std::optional< llvm::json::Value > params)
Definition Transport.h:30
static std::string KeyFor(Request r)
Definition Transport.h:57
A request that expects a response.
Definition Protocol.h:35
std::optional< llvm::json::Value > params
The method's params.
Definition Protocol.h:41
std::string method
The method to be invoked.
Definition Protocol.h:39
Id id
The request id.
Definition Protocol.h:37
A response to a request, either an error or a result.
Definition Protocol.h:77
Id id
The request id.
Definition Protocol.h:79
std::variant< Error, llvm::json::Value > result
The result of the request, either an Error or the JSON value of the response.
Definition Protocol.h:82