Skip to content

Commit 3ad4d2e

Browse files
committed
fixed headers for consistancy
1 parent fe0e10a commit 3ad4d2e

7 files changed

Lines changed: 294 additions & 21 deletions

File tree

include/env/EnvVars.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@
55
// Purpose: Cross-platform helpers to read environment variables safely.
66
//==========================================================================================================
77
#pragma once
8-
#include <string>
98
#include <cstdlib>
9+
#include <string>
1010

11+
//==========================================================================================================
12+
// GetEnvOrDefault
13+
// Purpose: Returns the value of the environment variable or a provided default when unset/empty.
14+
// Args:
15+
// name: C-string name of the environment variable. When null or empty, returns defaultValue.
16+
// defaultValue: Value to return when the variable is not set.
17+
// Returns:
18+
// std::string with the environment value (when set) or defaultValue otherwise.
19+
//==========================================================================================================
1120
inline std::string GetEnvOrDefault(const char* name, const std::string& defaultValue) {
1221
if (name == nullptr || *name == '\0') {
1322
return defaultValue;

include/mcp/HTTPServer.hpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//==========================================================================================================
22
// SPDX-License-Identifier: MIT
33
// Copyright (c) 2025 Vinny Parla
4-
// File: include/mcp/HTTPServer.hpp
4+
// File: HTTPServer.hpp
55
// Purpose: Coroutine-based HTTP/HTTPS JSON-RPC server using Boost.Beast (TLS 1.3 only for HTTPS)
66
//==========================================================================================================
77

@@ -18,6 +18,17 @@ namespace mcp {
1818

1919
class HTTPServer {
2020
public:
21+
//==========================================================================================================
22+
// Options
23+
// Purpose: Configuration for bind address/port, JSON-RPC paths, and TLS files.
24+
// Fields:
25+
// address: Bind address (default: 0.0.0.0)
26+
// port: Listen port (default: 9443)
27+
// rpcPath: JSON-RPC request path
28+
// notifyPath: JSON-RPC notification path
29+
// scheme: "http" or "https" (TLS 1.3 only for https)
30+
// certFile/keyFile: PEM files required when scheme == https
31+
//==========================================================================================================
2132
struct Options {
2233
std::string address{"0.0.0.0"};
2334
std::string port{"9443"};
@@ -35,15 +46,43 @@ class HTTPServer {
3546
explicit HTTPServer(const Options& opts);
3647
~HTTPServer();
3748

49+
//==========================================================================================================
50+
// Starts the server accept loop on a background I/O thread.
51+
// Returns:
52+
// Future that becomes ready once the I/O context is running.
53+
//==========================================================================================================
3854
std::future<void> Start();
55+
56+
//==========================================================================================================
57+
// Stops the server: closes acceptor, stops I/O context, and joins background thread.
58+
// Returns:
59+
// Future that completes when shutdown has finished.
60+
//==========================================================================================================
3961
std::future<void> Stop();
4062

63+
//==========================================================================================================
64+
// Sets the request handler (JSON-RPC request -> response).
65+
// Args:
66+
// handler: Callback invoked per request; may return an error response.
67+
//==========================================================================================================
4168
void SetRequestHandler(RequestHandler handler);
69+
70+
//==========================================================================================================
71+
// Sets the notification handler (no response expected).
72+
// Args:
73+
// handler: Callback invoked per notification with ownership of the notification object.
74+
//==========================================================================================================
4275
void SetNotificationHandler(NotificationHandler handler);
76+
77+
//==========================================================================================================
78+
// Sets the error handler for transport/server errors.
79+
// Args:
80+
// handler: Callback invoked with error strings.
81+
//==========================================================================================================
4382
void SetErrorHandler(ErrorHandler handler);
4483

4584
private:
46-
struct Impl;
85+
class Impl;
4786
std::unique_ptr<Impl> pImpl;
4887
};
4988

include/mcp/HTTPTransport.hpp

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//==========================================================================================================
22
// SPDX-License-Identifier: MIT
33
// Copyright (c) 2025 Vinny Parla
4-
// File: include/mcp/HTTPTransport.hpp
4+
// File: HTTPTransport.hpp
55
// Purpose: Coroutine-based HTTP/HTTPS JSON-RPC client transport using Boost.Beast (TLS 1.3 only for HTTPS)
66
//==========================================================================================================
77

@@ -17,35 +17,78 @@
1717

1818
namespace mcp {
1919

20+
//==========================================================================================================
21+
// HTTPTransport
22+
// Purpose: Concrete HTTP/HTTPS transport implementing ITransport using Boost.Beast coroutines.
23+
//==========================================================================================================
2024
class HTTPTransport : public ITransport {
2125
public:
26+
//==========================================================================================================
27+
// Options
28+
// Purpose: Configuration for HTTP/HTTPS endpoints and TLS verification.
29+
// Fields:
30+
// scheme: "http" or "https" (default: https)
31+
// host: Server hostname or IP (default: localhost)
32+
// port: Service port (default: 9443)
33+
// rpcPath: JSON-RPC request path
34+
// notifyPath: JSON-RPC notification path
35+
// serverName: TLS SNI and hostname verification name (when https)
36+
// caFile/caPath: Optional CA bundle/path for trust store
37+
// connectTimeoutMs: Connect timeout in milliseconds
38+
// readTimeoutMs: Read timeout in milliseconds
39+
//==========================================================================================================
2240
struct Options {
23-
std::string scheme{"https"}; // "http" or "https"
41+
std::string scheme{"https"};
2442
std::string host{"localhost"};
2543
std::string port{"9443"};
2644
std::string rpcPath{"/mcp/rpc"};
2745
std::string notifyPath{"/mcp/notify"};
28-
std::string serverName; // SNI / hostname verification for TLS
29-
std::string caFile; // optional
30-
std::string caPath; // optional
46+
std::string serverName;
47+
std::string caFile;
48+
std::string caPath;
3149
unsigned int connectTimeoutMs{10000};
3250
unsigned int readTimeoutMs{30000};
3351
};
3452

3553
explicit HTTPTransport(const Options& opts);
3654
~HTTPTransport() override;
3755

38-
// ITransport
56+
////////////////////////////////////////// ITransport //////////////////////////////////////////
57+
//==========================================================================================================
58+
// Starts the transport I/O loop. Returns when worker is ready to accept requests.
59+
//==========================================================================================================
3960
std::future<void> Start() override;
61+
62+
//==========================================================================================================
63+
// Closes the transport and stops the I/O loop.
64+
//==========================================================================================================
4065
std::future<void> Close() override;
66+
67+
//==========================================================================================================
68+
// Indicates whether the transport session is currently connected.
69+
//==========================================================================================================
4170
bool IsConnected() const override;
71+
72+
//==========================================================================================================
73+
// Returns a transport session identifier for diagnostics.
74+
//==========================================================================================================
4275
std::string GetSessionId() const override;
4376

77+
//==========================================================================================================
78+
// Sends a JSON-RPC request and returns a future for the response.
79+
//==========================================================================================================
4480
std::future<std::unique_ptr<JSONRPCResponse>> SendRequest(
4581
std::unique_ptr<JSONRPCRequest> request) override;
82+
83+
//==========================================================================================================
84+
// Sends a JSON-RPC notification (no response expected).
85+
//==========================================================================================================
4686
std::future<void> SendNotification(
4787
std::unique_ptr<JSONRPCNotification> notification) override;
4888

89+
//==========================================================================================================
90+
// Registers handlers for incoming notifications, requests (unused for client), and errors.
91+
//==========================================================================================================
4992
void SetNotificationHandler(NotificationHandler handler) override;
5093
void SetRequestHandler(RequestHandler handler) override;
5194
void SetErrorHandler(ErrorHandler handler) override;

include/mcp/InMemoryTransport.hpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,64 @@
1212

1313
namespace mcp {
1414

15+
//==========================================================================================================
16+
// InMemoryTransport
17+
// Purpose: Zero-copy, in-process transport used for tests and embedding. Implements ITransport and
18+
// delivers messages to a paired transport instance without networking or I/O.
19+
//==========================================================================================================
1520
class InMemoryTransport : public ITransport {
1621
public:
1722
InMemoryTransport();
1823
virtual ~InMemoryTransport();
1924

20-
// Create paired transports for testing
25+
//==========================================================================================================
26+
// CreatePair
27+
// Purpose: Creates two paired transports wired to each other in-memory.
28+
// Returns:
29+
// pair(left,right) where sending on one delivers to the other.
30+
//==========================================================================================================
2131
static std::pair<std::unique_ptr<InMemoryTransport>, std::unique_ptr<InMemoryTransport>> CreatePair();
2232

23-
// ITransport implementation
33+
////////////////////////////////////////// ITransport //////////////////////////////////////////
34+
//==========================================================================================================
35+
// Starts the in-memory transport (no-op wiring setup).
36+
// Returns:
37+
// Future that completes when ready to send/receive.
38+
//==========================================================================================================
2439
std::future<void> Start() override;
40+
41+
//==========================================================================================================
42+
// Closes the in-memory transport and fails any pending requests.
43+
// Returns:
44+
// Future that completes when closed.
45+
//==========================================================================================================
2546
std::future<void> Close() override;
47+
48+
//==========================================================================================================
49+
// Indicates whether this transport is logically connected to its peer.
50+
//==========================================================================================================
2651
bool IsConnected() const override;
52+
53+
//==========================================================================================================
54+
// Returns a diagnostic session identifier.
55+
//==========================================================================================================
2756
std::string GetSessionId() const override;
2857

58+
//==========================================================================================================
59+
// Sends a JSON-RPC request to the paired transport and returns its response.
60+
//==========================================================================================================
2961
std::future<std::unique_ptr<JSONRPCResponse>> SendRequest(
3062
std::unique_ptr<JSONRPCRequest> request) override;
63+
64+
//==========================================================================================================
65+
// Sends a JSON-RPC notification to the paired transport.
66+
//==========================================================================================================
3167
std::future<void> SendNotification(
3268
std::unique_ptr<JSONRPCNotification> notification) override;
3369

70+
//==========================================================================================================
71+
// Registers handlers for incoming notifications and errors.
72+
//==========================================================================================================
3473
void SetNotificationHandler(NotificationHandler handler) override;
3574
void SetErrorHandler(ErrorHandler handler) override;
3675
void SetRequestHandler(RequestHandler handler) override;

include/mcp/JSONRPCTypes.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616

1717
namespace mcp {
1818

19+
//==========================================================================================================
20+
// JSONValue
21+
// Purpose: Simplified JSON representation backed by std::variant and shared_ptr graphs.
22+
// Fields:
23+
// Array: vector<shared_ptr<JSONValue>> representing a JSON array.
24+
// Object: unordered_map<string, shared_ptr<JSONValue>> representing a JSON object.
25+
// value: variant holding nullptr, bool, int64_t, double, string, Array, or Object.
26+
//==========================================================================================================
1927
// JSON value type - simplified JSON representation using std library only
2028
struct JSONValue {
2129
using Array = std::vector<std::shared_ptr<JSONValue>>;
@@ -57,9 +65,20 @@ struct JSONValue {
5765
const auto& get() const { return value; }
5866
};
5967

68+
//==========================================================================================================
69+
// JSONRPCId
70+
// Purpose: JSON-RPC 2.0 id variant (per spec): string, integer, or null.
71+
//==========================================================================================================
6072
// JSON-RPC 2.0 ID type
6173
using JSONRPCId = std::variant<std::string, int64_t, std::nullptr_t>;
6274

75+
//==========================================================================================================
76+
// JSONRPCMessage
77+
// Purpose: Abstract base for JSON-RPC 2.0 messages providing serialization APIs.
78+
// Methods:
79+
// Serialize(): Returns canonical JSON string for the message.
80+
// Deserialize(json): Parses JSON string into this object; returns true on success.
81+
//==========================================================================================================
6382
// Base JSON-RPC message
6483
class JSONRPCMessage {
6584
public:
@@ -70,6 +89,15 @@ class JSONRPCMessage {
7089
virtual bool Deserialize(const std::string& json) = 0;
7190
};
7291

92+
//==========================================================================================================
93+
// JSONRPCRequest
94+
// Purpose: JSON-RPC 2.0 request message with id, method, and optional params.
95+
// Ctors:
96+
// JSONRPCRequest(id, method, params?): Initializes fields (params optional).
97+
// Methods:
98+
// Serialize(): JSON string.
99+
// Deserialize(json): Returns true when parsed successfully.
100+
//==========================================================================================================
73101
// JSON-RPC Request
74102
class JSONRPCRequest : public JSONRPCMessage {
75103
public:
@@ -85,6 +113,16 @@ class JSONRPCRequest : public JSONRPCMessage {
85113
bool Deserialize(const std::string& json) override;
86114
};
87115

116+
//==========================================================================================================
117+
// JSONRPCResponse
118+
// Purpose: JSON-RPC 2.0 response message carrying either result or error.
119+
// Ctors:
120+
// JSONRPCResponse(id, result): Success response with result set.
121+
// JSONRPCResponse(id, error, /*isError*/): Error response with error set.
122+
// Methods:
123+
// Serialize()/Deserialize(json)
124+
// IsError(): True when error is present.
125+
//==========================================================================================================
88126
// JSON-RPC Response
89127
class JSONRPCResponse : public JSONRPCMessage {
90128
public:
@@ -104,6 +142,14 @@ class JSONRPCResponse : public JSONRPCMessage {
104142
bool IsError() const { return error.has_value(); }
105143
};
106144

145+
//==========================================================================================================
146+
// JSONRPCNotification
147+
// Purpose: JSON-RPC 2.0 notification (no id, no response).
148+
// Ctors:
149+
// JSONRPCNotification(method, params?): Initializes method and optional params.
150+
// Methods:
151+
// Serialize()/Deserialize(json)
152+
//==========================================================================================================
107153
// JSON-RPC Notification
108154
class JSONRPCNotification : public JSONRPCMessage {
109155
public:
@@ -118,6 +164,12 @@ class JSONRPCNotification : public JSONRPCMessage {
118164
bool Deserialize(const std::string& json) override;
119165
};
120166

167+
//==========================================================================================================
168+
// JSONRPCErrorCodes
169+
// Purpose: Standard JSON-RPC error codes plus MCP-specific codes for SDK ergonomics.
170+
// Notes:
171+
// Values are stable and align with JSON-RPC 2.0 spec; MCP codes are negative values in the -320xx range.
172+
//==========================================================================================================
121173
// JSON-RPC Error codes (standard + MCP specific)
122174
namespace JSONRPCErrorCodes {
123175
constexpr int ParseError = -32700;
@@ -134,10 +186,31 @@ namespace JSONRPCErrorCodes {
134186
constexpr int PromptNotFound = -32004;
135187
}
136188

189+
//==========================================================================================================
190+
// CreateErrorObject
191+
// Purpose: Build a JSON error object with shape { code, message, data? }.
192+
// Args:
193+
// code: Integer error code (standard or MCP-specific).
194+
// message: Human-readable description.
195+
// data: Optional structured payload.
196+
// Returns:
197+
// JSONValue object representing the error.
198+
//==========================================================================================================
137199
// Utility functions for JSON-RPC error responses
138200
JSONValue CreateErrorObject(int code, const std::string& message,
139201
const std::optional<JSONValue>& data = std::nullopt);
140202

203+
//==========================================================================================================
204+
// CreateErrorResponse
205+
// Purpose: Convenience to wrap an error object into a JSONRPCResponse with the given id.
206+
// Args:
207+
// id: Request id to echo in the response (string | int64 | null).
208+
// code: Integer error code.
209+
// message: Error message.
210+
// data: Optional structured payload.
211+
// Returns:
212+
// unique_ptr<JSONRPCResponse> with error populated.
213+
//==========================================================================================================
141214
std::unique_ptr<JSONRPCResponse> CreateErrorResponse(
142215
const JSONRPCId& id, int code, const std::string& message,
143216
const std::optional<JSONValue>& data = std::nullopt);

0 commit comments

Comments
 (0)