cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
mcp::server::Transport Class Referenceabstract

Abstract server transport for receiving client JSON-RPC messages. More...

#include <transport.hpp>

Inheritance diagram for mcp::server::Transport:
mcp::server::ContractTransportAdapter mcp::server::HttpTransport mcp::server::StdioTransport

Public Member Functions

std::weak_ptr< void > lifetime_token () const noexcept
 Weak lifetime token for SessionContext and ClientPeer guards.
 
virtual core::Result< core::Unitstart (RequestHandler handler, NotificationHandler notification_handler={})=0
 Start accepting inbound messages and dispatch them to handlers.
 
virtual core::Result< protocol::JsonRpcResponsesend_request (const protocol::JsonRpcRequest &request)
 Send a JSON-RPC request from the server to the connected client.
 
virtual core::Result< protocol::JsonRpcResponsesend_request_to_session (std::string_view session_id, const protocol::JsonRpcRequest &request)
 Send a JSON-RPC request to a specific logical session.
 
virtual std::optional< protocol::ClientCapabilitiesclient_capabilities () const
 Return capabilities learned from the client's initialize request.
 
virtual std::optional< protocol::ClientCapabilitiesclient_capabilities_for_session (std::string_view session_id) const
 Return capabilities learned for a specific logical session.
 
virtual core::Result< core::Unitsend_notification (const protocol::JsonRpcNotification &notification)=0
 Send a JSON-RPC notification from the server to the client.
 
virtual core::Result< core::Unitsend_notification_to_session (std::string_view session_id, const protocol::JsonRpcNotification &notification)
 Send a JSON-RPC notification to a specific logical session.
 
virtual void stop () noexcept=0
 Request transport shutdown.
 
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.
 

Detailed Description

Abstract server transport for receiving client JSON-RPC messages.

A Transport is responsible for accepting inbound client messages, invoking the supplied handlers, serializing responses or errors, and sending server notifications back to the client. The server object owns or otherwise controls the transport lifetime; callbacks receive only borrowed references and must not store SessionContext::transport beyond the active session.

Implementations define their own threading model. StdioTransport runs its read loop inside start(), while HttpTransport serves callbacks from the HTTP server threads and uses an SSE stream for outbound traffic. Unless a derived transport documents stronger guarantees, callers should serialize calls that mutate shared server state from callbacks.

Member Function Documentation

◆ client_capabilities()

virtual std::optional< protocol::ClientCapabilities > mcp::server::Transport::client_capabilities ( ) const
inlinevirtual

Return capabilities learned from the client's initialize request.

Returns
Client capabilities when known; std::nullopt before initialize or when the transport cannot provide them.

Reimplemented in mcp::server::HttpTransport, mcp::server::StdioTransport, and mcp::server::ContractTransportAdapter.

◆ client_capabilities_for_session()

virtual std::optional< protocol::ClientCapabilities > mcp::server::Transport::client_capabilities_for_session ( std::string_view  session_id) const
inlinevirtual

Return capabilities learned for a specific logical session.

Parameters
session_idTransport-defined session identifier.
Returns
Client capabilities when known for the session.
Note
Transports without session routing delegate to client_capabilities().

Reimplemented in mcp::server::HttpTransport.

◆ name()

virtual std::string_view mcp::server::Transport::name ( ) const
pure virtualnoexcept

Human-readable transport name for diagnostics.

Returns
A static string view owned by the transport implementation.

Implemented in mcp::server::HttpTransport, mcp::server::StdioTransport, and mcp::server::ContractTransportAdapter.

◆ send_notification()

virtual core::Result< core::Unit > mcp::server::Transport::send_notification ( const protocol::JsonRpcNotification notification)
pure virtual

Send a JSON-RPC notification from the server to the client.

Parameters
notificationNotification to serialize and deliver.
Returns
core::Unit on success, or a core::Error for serialization, stopped-session, or I/O failures.

Implemented in mcp::server::HttpTransport, mcp::server::StdioTransport, and mcp::server::ContractTransportAdapter.

◆ send_notification_to_session()

virtual core::Result< core::Unit > mcp::server::Transport::send_notification_to_session ( std::string_view  session_id,
const protocol::JsonRpcNotification notification 
)
inlinevirtual

Send a JSON-RPC notification to a specific logical session.

Parameters
session_idTransport-defined session identifier.
notificationNotification to serialize and deliver.
Returns
core::Unit on success, or an error if the session is unavailable.
Note
Transports without session routing delegate to send_notification().

Reimplemented in mcp::server::HttpTransport.

◆ send_request()

virtual core::Result< protocol::JsonRpcResponse > mcp::server::Transport::send_request ( const protocol::JsonRpcRequest request)
inlinevirtual

Send a JSON-RPC request from the server to the connected client.

Parameters
requestRequest to serialize and deliver.
Returns
The client response, or an error if outbound requests are not supported or delivery fails.
Note
The base implementation returns MethodNotFound because not every transport can correlate outbound requests with client responses.

Reimplemented in mcp::server::HttpTransport, and mcp::server::ContractTransportAdapter.

◆ send_request_to_session()

virtual core::Result< protocol::JsonRpcResponse > mcp::server::Transport::send_request_to_session ( std::string_view  session_id,
const protocol::JsonRpcRequest request 
)
inlinevirtual

Send a JSON-RPC request to a specific logical session.

Parameters
session_idTransport-defined session identifier.
requestRequest to serialize and deliver.
Returns
The client response, or an error if the session is unavailable or outbound requests are unsupported.
Note
Transports without session routing delegate to send_request().

Reimplemented in mcp::server::HttpTransport.

◆ start()

virtual core::Result< core::Unit > mcp::server::Transport::start ( RequestHandler  handler,
NotificationHandler  notification_handler = {} 
)
pure virtual

Start accepting inbound messages and dispatch them to handlers.

Parameters
handlerRequired request handler.
notification_handlerOptional notification handler.
Returns
core::Unit when the transport stops cleanly, or a core::Error for setup, I/O, parse, serialization, or callback failures.
Note
Implementations may block until stop() is called or input ends.

Implemented in mcp::server::HttpTransport, mcp::server::StdioTransport, and mcp::server::ContractTransportAdapter.

◆ stop()

virtual void mcp::server::Transport::stop ( )
pure virtualnoexcept

Request transport shutdown.

stop() is best-effort and must not throw. Implementations should unblock any running start() call or pending outbound operation where possible.

Implemented in mcp::server::HttpTransport, mcp::server::StdioTransport, and mcp::server::ContractTransportAdapter.

◆ wait_until_ready()

virtual void mcp::server::Transport::wait_until_ready ( )
inlinevirtual

Blocks until the transport is ready to process messages.

The default is a no-op. Transports that need asynchronous startup (e.g. binding a socket) override this to block until the underlying resource is available.

Reimplemented in mcp::server::HttpTransport.


The documentation for this class was generated from the following file: