cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
mcp::server::StdioTransport Class Referencefinal

Blocking stdio transport for local MCP clients. More...

#include <stdio_transport.hpp>

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

Public Member Functions

 StdioTransport ()
 Construct a transport using std::cin and std::cout.
 
 StdioTransport (std::istream &input, std::ostream &output)
 Construct a transport over caller-owned streams.
 
core::Result< core::Unitstart (RequestHandler handler, NotificationHandler notification_handler={}) override
 Run the stdio message loop.
 
std::optional< protocol::ClientCapabilitiesclient_capabilities () const override
 Return capabilities from the most recent initialize request.
 
core::Result< core::Unitsend_notification (const protocol::JsonRpcNotification &notification) override
 Write an outbound server notification to the output stream.
 
void stop () noexcept override
 Stop the read loop after the current blocking read completes.
 
std::string_view name () const noexcept override
 Return the diagnostic transport name "stdio".
 
- Public Member Functions inherited from mcp::server::Transport
std::weak_ptr< void > lifetime_token () const noexcept
 Weak lifetime token for SessionContext and ClientPeer guards.
 
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_for_session (std::string_view session_id) const
 Return capabilities learned for a specific logical session.
 
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 wait_until_ready ()
 Blocks until the transport is ready to process messages.
 

Detailed Description

Blocking stdio transport for local MCP clients.

StdioTransport reads one JSON-RPC message per line from its input stream and writes responses and outbound notifications to its output stream. start() owns the read loop and blocks until EOF, stop(), or an unrecoverable read, write, parse, or callback error.

The transport does not own custom streams passed to the constructor; callers must keep them alive for the whole transport lifetime. Outbound notification writes are mutex-protected so a handler can call SessionContext::client() while the read loop is active.

This concrete transport is a compatibility convenience for simple local servers. Because it is built on caller-owned std::istream/std::ostream objects, stop() only prevents the next loop iteration; it cannot portably interrupt a thread already blocked inside std::getline(). Applications that need a cancellable service loop should prefer the role-generic mcp::transport::ServerStdioTransport with ServerPeer/Service, or a platform-owned process/pipe transport.

The server stdio loop handles one inbound line at a time and writes the corresponding response before reading the next request. It therefore has no concrete in-flight request registry; duplicate in-flight request-id validation is enforced by asynchronous peer/native/adapter layers.

Constructor & Destructor Documentation

◆ StdioTransport()

mcp::server::StdioTransport::StdioTransport ( std::istream &  input,
std::ostream &  output 
)

Construct a transport over caller-owned streams.

Parameters
inputStream read by start(); must outlive the transport.
outputStream used for responses and notifications; must outlive the transport.

Member Function Documentation

◆ client_capabilities()

std::optional< protocol::ClientCapabilities > mcp::server::StdioTransport::client_capabilities ( ) const
overridevirtual

Return capabilities from the most recent initialize request.

Reimplemented from mcp::server::Transport.

◆ name()

std::string_view mcp::server::StdioTransport::name ( ) const
overridevirtualnoexcept

Return the diagnostic transport name "stdio".

Implements mcp::server::Transport.

◆ send_notification()

core::Result< core::Unit > mcp::server::StdioTransport::send_notification ( const protocol::JsonRpcNotification notification)
overridevirtual

Write an outbound server notification to the output stream.

Parameters
notificationNotification to serialize as a single line.
Returns
core::Unit on success, or a core::Error when serialization or writing fails.

Implements mcp::server::Transport.

◆ start()

core::Result< core::Unit > mcp::server::StdioTransport::start ( RequestHandler  handler,
NotificationHandler  notification_handler = {} 
)
overridevirtual

Run the stdio message loop.

Parameters
handlerRequired request handler invoked synchronously on the start() caller's thread.
notification_handlerOptional notification handler invoked synchronously on the start() caller's thread.
Returns
core::Unit after clean EOF or stop(), or a core::Error for invalid configuration, stream failures, serialization failures, or notification handler errors.
Note
Request handler errors are encoded as JSON-RPC error responses and do not by themselves stop the loop.

Implements mcp::server::Transport.

◆ stop()

void mcp::server::StdioTransport::stop ( )
overridevirtualnoexcept

Stop the read loop after the current blocking read completes.

Note
This does not interrupt a std::getline() call already blocked on a caller-owned stream.

Implements mcp::server::Transport.


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