19namespace mcp::transport {
37 using TxMessage =
typename Transport<Role>::TxMessage;
38 using RxMessage =
typename Transport<Role>::RxMessage;
41 : input_(&input), output_(&output) {}
43 std::string_view
name() const noexcept
override {
return "stdio"; }
46 std::lock_guard<std::mutex> lock(mutex_);
54 const auto serialized = protocol::serialize_message(message);
56 return mcp::core::unexpected(serialized.error());
59 std::lock_guard<std::mutex> lock(mutex_);
61 return mcp::core::unexpected(transport_error(
62 protocol::ErrorCode::InvalidRequest,
"transport is closed"));
64 (*output_) << *serialized <<
'\n';
67 return mcp::core::unexpected(
68 transport_error(protocol::ErrorCode::InternalError,
69 "failed to write transport message"));
76 std::lock_guard<std::mutex> lock(mutex_);
83 if (!std::getline(*input_, line)) {
87 return mcp::core::unexpected(transport_error(
88 protocol::ErrorCode::ParseError,
"empty transport message"));
91 const auto parsed = protocol::parse_message(line);
93 return mcp::core::unexpected(parsed.error());
95 return RxMessage{*parsed};
99 std::lock_guard<std::mutex> lock(mutex_);
106 std::string message) {
108 static_cast<int>(code),
115 std::istream* input_;
116 std::ostream* output_;
117 mutable std::mutex mutex_;
118 bool closed_ =
false;
121using ClientStdioTransport = StdioTransport<RoleClient>;
122using ServerStdioTransport = StdioTransport<RoleServer>;
Message-level stdio transport for either MCP role.
Definition stdio_transport.hpp:35
core::Result< core::Unit > send(TxMessage message) override
Sends one JSON-RPC message to the peer.
Definition stdio_transport.hpp:53
core::Result< std::optional< RxMessage > > receive() override
Receives the next JSON-RPC message from the peer.
Definition stdio_transport.hpp:74
std::string_view name() const noexcept override
Human-readable transport name for diagnostics.
Definition stdio_transport.hpp:43
core::Result< core::Unit > close() override
Closes the transport and unblocks receive() where possible.
Definition stdio_transport.hpp:98
protocol::Json diagnostics() const override
Structured implementation diagnostics.
Definition stdio_transport.hpp:45
Minimal message-level transport contract shared by MCP roles.
Definition transport.hpp:38
ErrorCode
JSON-RPC and MCP-specific error codes used in ErrorObject.
Definition types.hpp:65
nlohmann::json Json
JSON value type used by all protocol DTOs.
Definition types.hpp:28
std::monostate Unit
Success value for operations that only need to report failure.
Definition result.hpp:55
tl::expected< T, Error > Result
Alias for the SDK result type.
Definition result.hpp:64
JSON-RPC method names and message construction/parsing helpers.
Structured error returned by fallible SDK operations.
Definition result.hpp:35
Role-generic MCP transport contract for SDK peer/service layers.