-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHTTPTransport.hpp
More file actions
159 lines (140 loc) · 8.34 KB
/
HTTPTransport.hpp
File metadata and controls
159 lines (140 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//==========================================================================================================
// SPDX-License-Identifier: MIT
// Copyright (c) 2025 Vinny Parla
// File: include/mcp/HTTPTransport.hpp
// Purpose: Coroutine-based HTTP/HTTPS JSON-RPC client transport using Boost.Beast (TLS 1.3 only for HTTPS)
//==========================================================================================================
#pragma once
#include <memory>
#include <string>
#include <future>
#include <atomic>
#include <functional>
#include <optional>
#include <vector>
#include "mcp/Transport.h"
#include "mcp/auth/IAuth.hpp"
namespace mcp {
//==========================================================================================================
// HTTPTransport
// Purpose: Concrete HTTP/HTTPS transport implementing ITransport using Boost.Beast coroutines.
//==========================================================================================================
class HTTPTransport : public ITransport {
public:
//==========================================================================================================
// HttpResponseInfo
// Purpose: Exposes HTTP status and headers from the last completed HTTP request. Used for auth discovery
// (e.g., parsing WWW-Authenticate on 401/403).
//==========================================================================================================
struct HttpResponseInfo {
int status{0};
std::vector<mcp::auth::HeaderKV> headers;
std::string wwwAuthenticate; // convenience copy of WWW-Authenticate if present
};
//==========================================================================================================
// Options
// Purpose: Configuration for HTTP/HTTPS endpoints and TLS verification.
// Fields:
// scheme: "http" or "https" (default: https)
// host: Server hostname or IP (default: localhost)
// port: Service port (default: 9443)
// endpointPath: Streamable HTTP endpoint path (single POST + GET SSE endpoint). Empty keeps legacy mode.
// streamPath: Optional override for the GET SSE path. Defaults to endpointPath when empty.
// rpcPath: Legacy JSON-RPC request path when endpointPath is empty.
// notifyPath: Legacy JSON-RPC notification path when endpointPath is empty.
// serverName: TLS SNI and hostname verification name (when https)
// caFile/caPath: Optional CA bundle/path for trust store
// connectTimeoutMs: Connect timeout in milliseconds
// readTimeoutMs: Read timeout in milliseconds
//==========================================================================================================
struct Options {
std::string scheme{"https"};
std::string host{"localhost"};
std::string port{"9443"};
std::string endpointPath;
std::string streamPath;
std::string rpcPath{"/mcp/rpc"};
std::string notifyPath{"/mcp/notify"};
std::string serverName;
std::string caFile;
std::string caPath;
unsigned int connectTimeoutMs{10000};
unsigned int readTimeoutMs{30000};
unsigned int sseReconnectDelayMs{250};
bool enableGetStream{true};
bool enableDeleteOnClose{true};
//======================================================================================================
// Authentication (optional)
// Purpose: Configure HTTP Authorization header injection and OAuth2 client-credentials flow.
// Fields:
// auth: "none" | "bearer" | "oauth2". Default: "none" (no Authorization header added).
// bearerToken: Static Bearer token value when auth=="bearer".
// oauthTokenUrl: Full URL to the OAuth2 token endpoint (e.g., https://auth.example.com/oauth2/token).
// clientId/clientSecret: OAuth2 client credentials for client-credentials grant.
// scope: Optional space-delimited scopes for the token request.
// tokenRefreshSkewSeconds: Seconds to subtract from expires_in to proactively refresh before expiry.
//======================================================================================================
std::string auth{"none"};
std::string bearerToken;
std::string oauthTokenUrl;
std::string clientId;
std::string clientSecret;
std::string scope;
unsigned int tokenRefreshSkewSeconds{60};
};
explicit HTTPTransport(const Options& opts);
~HTTPTransport() override;
////////////////////////////////////////// ITransport //////////////////////////////////////////
//==========================================================================================================
// Starts the transport I/O loop. Returns when worker is ready to accept requests.
//==========================================================================================================
std::future<void> Start() override;
//==========================================================================================================
// Closes the transport and stops the I/O loop.
//==========================================================================================================
std::future<void> Close() override;
//==========================================================================================================
// Indicates whether the transport session is currently connected.
//==========================================================================================================
bool IsConnected() const override;
//==========================================================================================================
// Returns a transport session identifier for diagnostics.
//==========================================================================================================
std::string GetSessionId() const override;
//==========================================================================================================
// Sends a JSON-RPC request and returns a future for the response.
//==========================================================================================================
std::future<std::unique_ptr<JSONRPCResponse>> SendRequest(
std::unique_ptr<JSONRPCRequest> request) override;
//==========================================================================================================
// Sends a JSON-RPC notification (no response expected).
//==========================================================================================================
std::future<void> SendNotification(
std::unique_ptr<JSONRPCNotification> notification) override;
//==========================================================================================================
// Registers handlers for incoming notifications, requests (unused for client), and errors.
//==========================================================================================================
void SetNotificationHandler(NotificationHandler handler) override;
void SetRequestHandler(RequestHandler handler) override;
void SetErrorHandler(ErrorHandler handler) override;
void SetAuth(mcp::auth::IAuth& auth);
void SetAuth(std::shared_ptr<mcp::auth::IAuth> auth);
//==========================================================================================================
// TryGetLastHttpResponse
// Purpose: Copies the last observed HTTP response info (if any). Returns true on success.
// Thread-safe; returns a snapshot and does not clear the stored value.
//==========================================================================================================
bool QueryLastHttpResponse(HttpResponseInfo& out) const;
private:
class Impl;
std::unique_ptr<Impl> pImpl;
};
//==========================================================================================================
// HTTPTransportFactory
// Purpose: Factory for creating HTTP/HTTPS transports.
//==========================================================================================================
class HTTPTransportFactory : public ITransportFactory {
public:
std::unique_ptr<ITransport> CreateTransport(const std::string& config) override;
};
} // namespace mcp