cxxmcp 1.3.0
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
7
8#include <optional>
9#include <string_view>
10
13#include "cxxmcp/roles.hpp"
14
15namespace mcp::transport {
16
18template <class Role>
20 using TxMessage = protocol::JsonRpcMessage;
21 using RxMessage = protocol::JsonRpcMessage;
22};
23
37template <class Role>
38class Transport {
39 public:
40 using TxMessage = typename MessageTraits<Role>::TxMessage;
41 using RxMessage = typename MessageTraits<Role>::RxMessage;
42
43 virtual ~Transport() = default;
44
46 virtual std::string_view name() const noexcept = 0;
47
53 virtual protocol::Json diagnostics() const {
54 return protocol::Json::object();
55 }
56
61 virtual core::Result<core::Unit> send(TxMessage message) = 0;
62
68
85 const protocol::RequestId& id) {
86 (void)id;
87 return mcp::core::unexpected(
88 core::Error{static_cast<int>(protocol::ErrorCode::MethodNotFound),
89 "transport does not route reverse responses directly",
90 {}});
91 }
92
98 virtual void publish_subscription_notification(std::string_view method,
99 protocol::Json params) {
100 (void)method;
101 (void)params;
102 }
103
106
112 virtual void wait_until_ready() {}
113};
114
115using ClientTransport = Transport<RoleClient>;
116using ServerTransport = Transport<RoleServer>;
117
118} // namespace mcp::transport
Minimal message-level transport contract shared by MCP roles.
Definition transport.hpp:38
virtual core::Result< std::optional< RxMessage > > receive()=0
Receives the next JSON-RPC message from the peer.
virtual core::Result< std::optional< RxMessage > > receive_response(const protocol::RequestId &id)
Waits for the response to one specific outbound request.
Definition transport.hpp:84
virtual std::string_view name() const noexcept=0
Human-readable transport name for diagnostics.
virtual void wait_until_ready()
Blocks until the transport is ready to process messages.
Definition transport.hpp:112
virtual core::Result< core::Unit > send(TxMessage message)=0
Sends one JSON-RPC message to the peer.
virtual void publish_subscription_notification(std::string_view method, protocol::Json params)
Publishes a notification to active subscription listen streams.
Definition transport.hpp:98
virtual core::Result< core::Unit > close()=0
Closes the transport and unblocks receive() where possible.
virtual protocol::Json diagnostics() const
Structured implementation diagnostics.
Definition transport.hpp:53
Shared JSON, JSON-RPC, error, cancellation, and progress model types.
nlohmann::json Json
JSON value type used by all protocol DTOs.
Definition types.hpp:28
std::variant< JsonRpcRequest, JsonRpcResponse, JsonRpcNotification > JsonRpcMessage
Variant over the JSON-RPC message shapes accepted by MCP transports.
Definition types.hpp:148
std::variant< std::int64_t, std::string > RequestId
JSON-RPC request or response identifier.
Definition types.hpp:56
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
Role markers shared by peer, service, and transport SDK boundaries.
Structured error returned by fallible SDK operations.
Definition result.hpp:35
Message types exchanged by a role-generic transport.
Definition transport.hpp:19