cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
http_transport.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
8
9#include <chrono>
10#include <functional>
11#include <memory>
12#include <optional>
13#include <string>
14#include <unordered_map>
15
17
18namespace mcp::client {
19
23 int status_code = 0;
25 std::string method;
27 std::unordered_map<std::string, std::string> headers;
29 std::optional<std::string> www_authenticate;
30};
31
37 std::string uri;
38
40 std::string host;
41
43 int port = 80;
44
46 std::string path = "/";
47
50 std::unordered_map<std::string, std::string> headers;
51
55 std::optional<std::string> auth_header;
56
60 HttpAuthRefreshHandler auth_refresh_handler;
61
63 std::chrono::milliseconds timeout{30000};
64};
65
71class HttpTransport final : public Transport {
72 public:
76
77 ~HttpTransport() override;
78
81 const protocol::JsonRpcRequest& request) override;
82
85 const protocol::JsonRpcNotification& notification) override;
86
91 TransportRequestHandler request_handler,
92 TransportNotificationHandler notification_handler = {}) override;
93
95 void stop() noexcept override;
96
100 void set_negotiated_protocol_version(std::string version);
101
102 private:
103 struct Impl;
104 std::unique_ptr<Impl> impl_;
105};
106
107} // namespace mcp::client
Client transport that exchanges MCP JSON-RPC messages over HTTP.
Definition http_transport.hpp:71
core::Result< protocol::JsonRpcResponse > send(const protocol::JsonRpcRequest &request) override
Sends one JSON-RPC request to the HTTP endpoint.
core::Result< core::Unit > start(TransportRequestHandler request_handler, TransportNotificationHandler notification_handler={}) override
Starts receive-side handling for server-initiated messages.
void stop() noexcept override
Stops background receive activity and releases transport resources.
HttpTransport(HttpTransportOptions options)
Creates an HTTP transport from endpoint options.
void set_negotiated_protocol_version(std::string version)
Updates the negotiated protocol version for subsequent requests.
core::Result< core::Unit > send_notification(const protocol::JsonRpcNotification &notification) override
Sends a JSON-RPC notification to the HTTP endpoint.
Abstract client transport used by Client to exchange JSON-RPC messages.
Definition client.hpp:83
Core client compatibility API and transport interface for MCP clients.
std::function< core::Result< core::Unit >(const protocol::JsonRpcNotification &)> TransportNotificationHandler
Handles JSON-RPC notifications sent by the server to this client.
Definition client.hpp:75
std::function< core::Result< protocol::JsonRpcResponse >(const protocol::JsonRpcRequest &)> TransportRequestHandler
Handles JSON-RPC requests sent by the server to this client.
Definition client.hpp:68
tl::expected< T, Error > Result
Alias for the SDK result type.
Definition result.hpp:64
HTTP auth challenge observed by the client transport.
Definition http_transport.hpp:21
std::unordered_map< std::string, std::string > headers
Response headers copied from the failed HTTP response.
Definition http_transport.hpp:27
int status_code
HTTP status code, normally 401 or 403.
Definition http_transport.hpp:23
std::string method
Request method name that received the challenge.
Definition http_transport.hpp:25
std::optional< std::string > www_authenticate
First WWW-Authenticate header value, when present.
Definition http_transport.hpp:29
Configuration for the client HTTP transport.
Definition http_transport.hpp:33
int port
Remote TCP port.
Definition http_transport.hpp:43
std::string host
Remote host name or IP address.
Definition http_transport.hpp:40
HttpAuthRefreshHandler auth_refresh_handler
Optional refresh hook invoked once for a 401 response before surfacing the auth failure.
Definition http_transport.hpp:60
std::chrono::milliseconds timeout
Connect, read, and write timeout used by the transport.
Definition http_transport.hpp:63
std::string uri
Full HTTP or HTTPS URI for the MCP endpoint.
Definition http_transport.hpp:37
std::string path
HTTP path used for MCP requests.
Definition http_transport.hpp:46
std::optional< std::string > auth_header
Optional bearer token inserted as Authorization: Bearer <token> on every outbound HTTP request.
Definition http_transport.hpp:55
std::unordered_map< std::string, std::string > headers
Extra request headers sent on every outbound HTTP request, including Streamable HTTP POST,...
Definition http_transport.hpp:50
JSON-RPC notification envelope for one-way MCP messages.
Definition types.hpp:137
JSON-RPC request envelope carrying an MCP method invocation.
Definition types.hpp:99