This document describes the optimizations made to the HTTP protocol implementation in SupaMCP and how to test these optimizations.
Based on the requirements in the mcp_http.md document, we have made the following optimizations to the HTTP protocol implementation:
-
Increased SSE Client and Event Limits
- Increased MAX_SSE_CLIENTS from 10000 to 50000
- Increased MAX_SSE_STORED_EVENTS from 1000 to 5000
- Implemented a more efficient circular buffer for storing SSE events
-
Added CORS Support
- Added complete CORS header support
- Implemented OPTIONS request handling (preflight requests)
- Added configurable CORS settings
-
Added Tool Discovery API
- Implemented the
/toolsendpoint that returns information about available tools
- Implemented the
-
Improved HTTP Functionality
- Added better error handling
- Improved HTTP header handling
We use a circular buffer to store SSE events, which is more efficient than the previous implementation:
- No longer need to move all events when the buffer is full
- Can add and retrieve events more quickly
- Better memory utilization
We have added complete CORS support, including:
- Access-Control-Allow-Origin
- Access-Control-Allow-Methods
- Access-Control-Allow-Headers
- Access-Control-Max-Age
- Access-Control-Allow-Credentials
These headers can be configured through the mcp_http_config_t structure.
The new /tools endpoint returns information about available tools on the server, including:
- Tool name
- Tool description
- Parameter information (name, type, description, whether required)
We have created a Python test script to test various aspects of the HTTP protocol implementation:
- Tool calls
- SSE events
- CORS support
- Tool discovery API
The test script is located at python/tests/test_http_protocol.py and can be run in the following ways:
# Automatically start mcp_server and run tests
python python/tests/test_http_protocol.py
# Specify server host and port
python python/tests/test_http_protocol.py --host 127.0.0.1 --port 8280
# Specify mcp_server executable path
python python/tests/test_http_protocol.py --server-path /path/to/mcp_server
# Don't start the server (assuming the server is already running)
python python/tests/test_http_protocol.py --no-serverThe test script tests the following:
-
Root Endpoint Test
- Checks if the server responds normally
-
Echo Tool Test
- Tests basic tool call functionality
-
Reverse Tool Test
- Tests more complex tool call functionality
-
CORS Support Test
- Tests preflight requests and CORS headers
-
SSE Event Test
- Tests SSE connections and event reception
The HTTP protocol implementation can be configured through the mcp_http_config_t structure:
typedef struct mcp_http_config {
const char* host; // Host to bind to (e.g., "0.0.0.0" for all interfaces)
uint16_t port; // Port to listen on
bool use_ssl; // Whether to use HTTPS
const char* cert_path; // SSL certificate file path (for HTTPS)
const char* key_path; // SSL private key file path (for HTTPS)
const char* doc_root; // Document root for static file serving (optional)
uint32_t timeout_ms; // Connection timeout in milliseconds (0 to disable)
// CORS settings
bool enable_cors; // Whether to enable CORS
const char* cors_allow_origin; // Allowed origins (e.g., "*" for all)
const char* cors_allow_methods; // Allowed methods (e.g., "GET, POST, OPTIONS")
const char* cors_allow_headers; // Allowed headers
int cors_max_age; // Maximum cache time for preflight requests (seconds)
} mcp_http_config_t;Here is an example of starting an MCP server with the HTTP transport layer:
// Create HTTP configuration
mcp_http_config_t http_config = {
.host = "0.0.0.0",
.port = 8280,
.use_ssl = false,
.cert_path = NULL,
.key_path = NULL,
.doc_root = NULL,
.timeout_ms = 0,
.enable_cors = true,
.cors_allow_origin = "*",
.cors_allow_methods = "GET, POST, OPTIONS",
.cors_allow_headers = "Content-Type, Authorization",
.cors_max_age = 86400
};
// Create HTTP transport layer
mcp_transport_t* transport = mcp_transport_http_create(&http_config);
// Start the server
mcp_server_start(server, transport);With these optimizations, the HTTP protocol implementation in SupaMCP is now more robust, efficient, and supports more features. These improvements allow SupaMCP to better integrate with web-based clients and provide a better user experience.