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
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
71
77 virtual void wait_until_ready() {}
78};
79
80using ClientTransport = Transport<RoleClient>;
81using ServerTransport = Transport<RoleServer>;
82
83} // 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 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:77
virtual core::Result< core::Unit > send(TxMessage message)=0
Sends one JSON-RPC message to the peer.
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.
std::variant< JsonRpcRequest, JsonRpcResponse, JsonRpcNotification > JsonRpcMessage
Variant over the JSON-RPC message shapes accepted by MCP transports.
Definition types.hpp:148
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.
Message types exchanged by a role-generic transport.
Definition transport.hpp:19