|
| 1 | +#pragma once |
| 2 | +#include "fastmcpp/server/session.hpp" |
| 3 | +#include "fastmcpp/types.hpp" |
| 4 | + |
| 5 | +#include <atomic> |
| 6 | +#include <condition_variable> |
| 7 | +#include <deque> |
| 8 | +#include <functional> |
| 9 | +#include <httplib.h> |
| 10 | +#include <memory> |
| 11 | +#include <mutex> |
| 12 | +#include <string> |
| 13 | +#include <thread> |
| 14 | +#include <unordered_map> |
| 15 | + |
| 16 | +namespace fastmcpp::server |
| 17 | +{ |
| 18 | + |
| 19 | +/** |
| 20 | + * Streamable HTTP MCP server wrapper. |
| 21 | + * |
| 22 | + * This transport implements the Streamable HTTP protocol for MCP communication |
| 23 | + * per MCP spec version 2025-03-26: |
| 24 | + * - Single POST endpoint (default: /mcp) |
| 25 | + * - Session ID management via Mcp-Session-Id header |
| 26 | + * - Responses can be JSON or SSE stream |
| 27 | + * |
| 28 | + * This is a simpler transport than SSE with a single endpoint. |
| 29 | + * Clients send JSON-RPC requests via POST and receive responses in the |
| 30 | + * same HTTP response (either as JSON or SSE stream for long-running operations). |
| 31 | + * |
| 32 | + * Usage: |
| 33 | + * auto handler = fastmcpp::mcp::make_mcp_handler("myserver", "1.0.0", tools); |
| 34 | + * StreamableHttpServerWrapper server(handler); |
| 35 | + * server.start(); // Non-blocking - runs in background thread |
| 36 | + * // ... server runs ... |
| 37 | + * server.stop(); // Graceful shutdown |
| 38 | + * |
| 39 | + * Reference: https://spec.modelcontextprotocol.io/specification/2025-03-26/basic/transports/ |
| 40 | + */ |
| 41 | +class StreamableHttpServerWrapper |
| 42 | +{ |
| 43 | + public: |
| 44 | + using McpHandler = std::function<fastmcpp::Json(const fastmcpp::Json&)>; |
| 45 | + |
| 46 | + /** |
| 47 | + * Construct a Streamable HTTP server with an MCP handler. |
| 48 | + * |
| 49 | + * @param handler Function that processes JSON-RPC requests and returns responses |
| 50 | + * @param host Host address to bind to (default: "127.0.0.1") |
| 51 | + * @param port Port to listen on (default: 18080) |
| 52 | + * @param mcp_path Path for the MCP POST endpoint (default: "/mcp") |
| 53 | + * @param auth_token Optional auth token for Bearer authentication (empty = no auth required) |
| 54 | + * @param cors_origin Optional CORS origin to allow (empty = no CORS header, use "*" for |
| 55 | + * wildcard) |
| 56 | + */ |
| 57 | + explicit StreamableHttpServerWrapper(McpHandler handler, std::string host = "127.0.0.1", |
| 58 | + int port = 18080, std::string mcp_path = "/mcp", |
| 59 | + std::string auth_token = "", std::string cors_origin = ""); |
| 60 | + |
| 61 | + ~StreamableHttpServerWrapper(); |
| 62 | + |
| 63 | + /** |
| 64 | + * Start the server in background (non-blocking). |
| 65 | + * |
| 66 | + * Launches a background thread that runs the HTTP server. |
| 67 | + * Use stop() to terminate. |
| 68 | + * |
| 69 | + * @return true if server started successfully |
| 70 | + */ |
| 71 | + bool start(); |
| 72 | + |
| 73 | + /** |
| 74 | + * Stop the server. |
| 75 | + * |
| 76 | + * Signals the server to stop and joins the background thread. |
| 77 | + * Safe to call multiple times. |
| 78 | + */ |
| 79 | + void stop(); |
| 80 | + |
| 81 | + /** |
| 82 | + * Check if server is currently running. |
| 83 | + */ |
| 84 | + bool running() const |
| 85 | + { |
| 86 | + return running_.load(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get the port the server is listening on. |
| 91 | + */ |
| 92 | + int port() const |
| 93 | + { |
| 94 | + return port_; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get the host address the server is bound to. |
| 99 | + */ |
| 100 | + const std::string& host() const |
| 101 | + { |
| 102 | + return host_; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Get the MCP endpoint path. |
| 107 | + */ |
| 108 | + const std::string& mcp_path() const |
| 109 | + { |
| 110 | + return mcp_path_; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Get the ServerSession for a given session ID. |
| 115 | + * |
| 116 | + * This allows server-initiated requests (sampling, elicitation) via |
| 117 | + * the session's bidirectional transport. |
| 118 | + * |
| 119 | + * @param session_id The session to get |
| 120 | + * @return Shared pointer to ServerSession, or nullptr if not found |
| 121 | + */ |
| 122 | + std::shared_ptr<ServerSession> get_session(const std::string& session_id) const |
| 123 | + { |
| 124 | + std::lock_guard<std::mutex> lock(sessions_mutex_); |
| 125 | + auto it = sessions_.find(session_id); |
| 126 | + if (it == sessions_.end()) |
| 127 | + return nullptr; |
| 128 | + return it->second; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Get the number of active sessions. |
| 133 | + */ |
| 134 | + size_t session_count() const |
| 135 | + { |
| 136 | + std::lock_guard<std::mutex> lock(sessions_mutex_); |
| 137 | + return sessions_.size(); |
| 138 | + } |
| 139 | + |
| 140 | + private: |
| 141 | + void run_server(); |
| 142 | + std::string generate_session_id(); |
| 143 | + bool check_auth(const std::string& auth_header) const; |
| 144 | + |
| 145 | + McpHandler handler_; |
| 146 | + std::string host_; |
| 147 | + int port_; |
| 148 | + std::string mcp_path_; |
| 149 | + std::string auth_token_; // Optional Bearer token for authentication |
| 150 | + std::string cors_origin_; // Optional CORS origin (empty = no CORS) |
| 151 | + |
| 152 | + std::unique_ptr<httplib::Server> svr_; |
| 153 | + std::thread thread_; |
| 154 | + std::atomic<bool> running_{false}; |
| 155 | + |
| 156 | + // Security limits |
| 157 | + static constexpr size_t MAX_SESSIONS = 1000; |
| 158 | + |
| 159 | + // Active sessions mapped by session ID |
| 160 | + std::unordered_map<std::string, std::shared_ptr<ServerSession>> sessions_; |
| 161 | + mutable std::mutex sessions_mutex_; |
| 162 | +}; |
| 163 | + |
| 164 | +} // namespace fastmcpp::server |
0 commit comments