|
cxxmcp 1.1.6
C++ MCP SDK
|
Blocking stdio transport for local MCP clients. More...
#include <stdio_transport.hpp>
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::Unit > | start (RequestHandler handler, NotificationHandler notification_handler={}) override |
| Run the stdio message loop. | |
| std::optional< protocol::ClientCapabilities > | client_capabilities () const override |
| Return capabilities from the most recent initialize request. | |
| core::Result< core::Unit > | send_notification (const protocol::JsonRpcNotification ¬ification) 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::JsonRpcResponse > | send_request (const protocol::JsonRpcRequest &request) |
| Send a JSON-RPC request from the server to the connected client. | |
| 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. | |
| virtual std::optional< protocol::ClientCapabilities > | client_capabilities_for_session (std::string_view session_id) const |
| Return capabilities learned for a specific logical session. | |
| 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. | |
| virtual void | wait_until_ready () |
| Blocks until the transport is ready to process messages. | |
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.
| mcp::server::StdioTransport::StdioTransport | ( | std::istream & | input, |
| std::ostream & | output | ||
| ) |
Construct a transport over caller-owned streams.
| input | Stream read by start(); must outlive the transport. |
| output | Stream used for responses and notifications; must outlive the transport. |
|
overridevirtual |
Return capabilities from the most recent initialize request.
Reimplemented from mcp::server::Transport.
|
overridevirtualnoexcept |
Return the diagnostic transport name "stdio".
Implements mcp::server::Transport.
|
overridevirtual |
Write an outbound server notification to the output stream.
| notification | Notification to serialize as a single line. |
Implements mcp::server::Transport.
|
overridevirtual |
Run the stdio message loop.
| handler | Required request handler invoked synchronously on the start() caller's thread. |
| notification_handler | Optional notification handler invoked synchronously on the start() caller's thread. |
Implements mcp::server::Transport.
|
overridevirtualnoexcept |
Stop the read loop after the current blocking read completes.
Implements mcp::server::Transport.