10#include <unordered_map>
26namespace mcp::server {
49 std::unordered_map<std::string, std::string>
headers;
84inline std::string_view request_wire_version(
85 const protocol::JsonRpcRequest& request,
87 if (request.meta.has_value() && request.meta->is_object() &&
88 request.meta->contains(
"io.modelcontextprotocol/protocolVersion") &&
89 request.meta->at(
"io.modelcontextprotocol/protocolVersion").is_string()) {
90 return request.meta->at(
"io.modelcontextprotocol/protocolVersion")
91 .get_ref<
const std::string&>();
93 if (request.protocol_version_override.has_value()) {
94 return *request.protocol_version_override;
96 for (
const auto& [name, value] : context.headers) {
97 if (name.size() == 20) {
99 for (std::size_t i = 0; i < name.size(); ++i) {
100 const char c = name[i];
101 lower[i] =
static_cast<char>(c >=
'A' && c <=
'Z' ? c -
'A' +
'a' : c);
103 lower[name.size()] =
'\0';
104 if (std::string_view(lower) ==
"mcp-protocol-version") {
109 return protocol::McpProtocolVersion;
115 std::string_view version)
noexcept {
116 return version == protocol::McpProtocolVersion2026_07_28 ||
117 version ==
"DRAFT-2026-v1";
123 return method == protocol::ServerDiscoverMethod ||
124 method == protocol::ToolsListMethod ||
125 method == protocol::PromptsListMethod ||
126 method == protocol::ResourcesListMethod ||
127 method == protocol::ResourcesTemplatesListMethod ||
128 method == protocol::ResourcesReadMethod;
160 return lifetime_token_;
183 static_cast<int>(protocol::ErrorCode::MethodNotFound),
184 "transport does not support outbound requests",
198 return send_request(request);
214 virtual std::optional<protocol::ClientCapabilities>
217 return client_capabilities();
233 std::string_view session_id,
236 return send_notification(notification);
259 virtual void stop() noexcept = 0;
263 virtual std::string_view name() const noexcept = 0;
270 virtual
void wait_until_ready() {}
273 std::shared_ptr<void> lifetime_token_ = std::make_shared<int>(0);
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:154
std::weak_ptr< void > lifetime_token() const noexcept
Weak lifetime token for SessionContext and ClientPeer guards.
Definition transport.hpp:159
virtual core::Result< core::Unit > send_notification_to_session(std::string_view session_id, const protocol::JsonRpcNotification ¬ification)
Send a JSON-RPC notification to a specific logical session.
Definition transport.hpp:232
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:195
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 ¬ification)=0
Send a JSON-RPC notification from the server to the client.
virtual void publish_subscription_notification(std::string_view method, protocol::Json params)
Publish a notification to open subscriptions/listen streams (SEP-2575).
Definition transport.hpp:249
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:215
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:179
virtual std::optional< protocol::ClientCapabilities > client_capabilities() const
Return capabilities learned from the client's initialize request.
Definition transport.hpp:204
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
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
JSON-RPC method names and message construction/parsing helpers.
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:75
bool wire_version_requires_result_type(std::string_view version) noexcept
True when the wire version requires resultType on result objects (SEP-2322 / MRTR,...
Definition transport.hpp:114
bool method_requires_cache_hints(std::string_view method) noexcept
True when a result for method must carry the SEP-2549 caching hints (ttlMs, cacheScope) on the statel...
Definition transport.hpp:122
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:139
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:43
std::optional< std::string > http_method
HTTP request method for HTTP-based transports.
Definition transport.hpp:51
std::optional< AuthIdentity > auth_identity
Authenticated principal produced by the configured AuthProvider.
Definition transport.hpp:55
std::optional< std::string > http_url
Absolute HTTP request URL for HTTP-based transports.
Definition transport.hpp:53
std::weak_ptr< void > transport_lifetime
Weak lifetime token supplied by the transport.
Definition transport.hpp:61
std::string remote_address
Best-effort remote endpoint description such as "stdio" or an address.
Definition transport.hpp:47
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:57
std::string session_id
Logical MCP session id when the transport has one.
Definition transport.hpp:45
std::unordered_map< std::string, std::string > headers
Transport-supplied request headers or metadata for this message.
Definition transport.hpp:49