cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
transport.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
5#include <functional>
6#include <memory>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <unordered_map>
11
16
24
25namespace mcp::server {
26
27class Transport;
28class ClientPeer;
35
44 std::string session_id;
46 std::string remote_address;
48 std::unordered_map<std::string, std::string> headers;
50 std::optional<std::string> http_method;
52 std::optional<std::string> http_url;
54 std::optional<AuthIdentity> auth_identity;
56 Transport* transport = nullptr;
60 std::weak_ptr<void> transport_lifetime;
61
65 SessionClient client() const noexcept;
66};
67
73using RequestHandler = std::function<core::Result<protocol::JsonRpcResponse>(
74 const protocol::JsonRpcRequest&, const SessionContext&)>;
75
83using NotificationHandler = std::function<core::Result<core::Unit>(
84 const protocol::JsonRpcNotification&, const SessionContext&)>;
85
99class Transport {
100 public:
101 virtual ~Transport() = default;
102
104 std::weak_ptr<void> lifetime_token() const noexcept {
105 return lifetime_token_;
106 }
107
115 RequestHandler handler,
116 NotificationHandler notification_handler = {}) = 0;
117
125 const protocol::JsonRpcRequest& request) {
126 (void)request;
127 return mcp::core::unexpected(core::Error{
128 static_cast<int>(protocol::ErrorCode::MethodNotFound),
129 "transport does not support outbound requests",
130 {},
131 });
132 }
133
141 std::string_view session_id, const protocol::JsonRpcRequest& request) {
142 (void)session_id;
143 return send_request(request);
144 }
145
149 virtual std::optional<protocol::ClientCapabilities> client_capabilities()
150 const {
151 return std::nullopt;
152 }
153
159 virtual std::optional<protocol::ClientCapabilities>
161 (void)session_id;
162 return client_capabilities();
163 }
164
170 const protocol::JsonRpcNotification& notification) = 0;
171
178 std::string_view session_id,
179 const protocol::JsonRpcNotification& notification) {
180 (void)session_id;
181 return send_notification(notification);
182 }
183
188 virtual void stop() noexcept = 0;
189
192 virtual std::string_view name() const noexcept = 0;
193
199 virtual void wait_until_ready() {}
200
201 private:
202 std::shared_ptr<void> lifetime_token_ = std::make_shared<int>(0);
203};
204
205} // namespace mcp::server
MCP client and server capability declarations.
Non-owning handle for the client associated with a server session.
Definition peer.hpp:46
Abstract server transport for receiving client JSON-RPC messages.
Definition transport.hpp:99
std::weak_ptr< void > lifetime_token() const noexcept
Weak lifetime token for SessionContext and ClientPeer guards.
Definition transport.hpp:104
virtual core::Result< core::Unit > send_notification_to_session(std::string_view session_id, const protocol::JsonRpcNotification &notification)
Send a JSON-RPC notification to a specific logical session.
Definition transport.hpp:177
virtual core::Result< protocol::JsonRpcResponse > send_request_to_session(std::string_view session_id, const protocol::JsonRpcRequest &request)
Send a JSON-RPC request to a specific logical session.
Definition transport.hpp:140
virtual core::Result< core::Unit > start(RequestHandler handler, NotificationHandler notification_handler={})=0
Start accepting inbound messages and dispatch them to handlers.
virtual void stop() noexcept=0
Request transport shutdown.
virtual core::Result< core::Unit > send_notification(const protocol::JsonRpcNotification &notification)=0
Send a JSON-RPC notification from the server to the client.
virtual std::optional< protocol::ClientCapabilities > client_capabilities_for_session(std::string_view session_id) const
Return capabilities learned for a specific logical session.
Definition transport.hpp:160
virtual core::Result< protocol::JsonRpcResponse > send_request(const protocol::JsonRpcRequest &request)
Send a JSON-RPC request from the server to the connected client.
Definition transport.hpp:124
virtual std::optional< protocol::ClientCapabilities > client_capabilities() const
Return capabilities learned from the client's initialize request.
Definition transport.hpp:149
Shared JSON, JSON-RPC, error, cancellation, and progress model types.
Shared result and error primitives used by the public cxxmcp SDK.
tl::expected< T, Error > Result
Alias for the SDK result type.
Definition result.hpp:64
Authentication extension points for server transports.
std::function< core::Result< protocol::JsonRpcResponse >(const protocol::JsonRpcRequest &, const SessionContext &)> RequestHandler
Callback used by transports to dispatch inbound JSON-RPC requests.
Definition transport.hpp:74
std::function< core::Result< core::Unit >(const protocol::JsonRpcNotification &, const SessionContext &)> NotificationHandler
Callback used by transports to dispatch inbound JSON-RPC notifications.
Definition transport.hpp:84
Structured error returned by fallible SDK operations.
Definition result.hpp:35
JSON-RPC notification envelope for one-way MCP messages.
Definition types.hpp:137
JSON-RPC request envelope carrying an MCP method invocation.
Definition types.hpp:99
Per-message connection metadata supplied to server handlers.
Definition transport.hpp:42
std::optional< std::string > http_method
HTTP request method for HTTP-based transports.
Definition transport.hpp:50
std::optional< AuthIdentity > auth_identity
Authenticated principal produced by the configured AuthProvider.
Definition transport.hpp:54
std::optional< std::string > http_url
Absolute HTTP request URL for HTTP-based transports.
Definition transport.hpp:52
std::weak_ptr< void > transport_lifetime
Weak lifetime token supplied by the transport.
Definition transport.hpp:60
std::string remote_address
Best-effort remote endpoint description such as "stdio" or an address.
Definition transport.hpp:46
SessionClient client() const noexcept
Return a non-owning handle for the client on this session.
Definition peer.hpp:874
Transport * transport
Borrowed transport used to create a ClientPeer for outbound messages.
Definition transport.hpp:56
std::string session_id
Logical MCP session id when the transport has one.
Definition transport.hpp:44
std::unordered_map< std::string, std::string > headers
Transport-supplied request headers or metadata for this message.
Definition transport.hpp:48