cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
stdio_transport.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
5#include <atomic>
6#include <iosfwd>
7#include <mutex>
8#include <optional>
9#include <string_view>
10
12
15
16namespace mcp::server {
17
42class StdioTransport final : public Transport {
43 public:
46
51 StdioTransport(std::istream& input, std::ostream& output);
52
64 RequestHandler handler,
65 NotificationHandler notification_handler = {}) override;
66
68 std::optional<protocol::ClientCapabilities> client_capabilities()
69 const override;
70
76 const protocol::JsonRpcNotification& notification) override;
77
81 void stop() noexcept override;
82
84 std::string_view name() const noexcept override;
85
86 private:
87 std::istream* input_;
88 std::ostream* output_;
89 std::mutex output_mutex_;
90 mutable std::mutex client_capabilities_mutex_;
91 std::atomic_bool running_{false};
92 bool initialized_ = false;
93 std::optional<protocol::ClientCapabilities> client_capabilities_;
94};
95
96} // namespace mcp::server
Blocking stdio transport for local MCP clients.
Definition stdio_transport.hpp:42
std::string_view name() const noexcept override
Return the diagnostic transport name "stdio".
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.
void stop() noexcept override
Stop the read loop after the current blocking read completes.
core::Result< core::Unit > send_notification(const protocol::JsonRpcNotification &notification) override
Write an outbound server notification to the output stream.
std::optional< protocol::ClientCapabilities > client_capabilities() const override
Return capabilities from the most recent initialize request.
StdioTransport()
Construct a transport using std::cin and std::cout.
Abstract server transport for receiving client JSON-RPC messages.
Definition transport.hpp:99
tl::expected< T, Error > Result
Alias for the SDK result type.
Definition result.hpp:64
Server-side transport abstraction for MCP JSON-RPC traffic.
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:74
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:84
JSON-RPC notification envelope for one-way MCP messages.
Definition types.hpp:137