Skip to content

Latest commit

 

History

History
155 lines (110 loc) · 4.8 KB

File metadata and controls

155 lines (110 loc) · 4.8 KB

SupaMCP HTTP Protocol Implementation Optimization

This document describes the optimizations made to the HTTP protocol implementation in SupaMCP and how to test these optimizations.

Optimization Content

Based on the requirements in the mcp_http.md document, we have made the following optimizations to the HTTP protocol implementation:

  1. 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
  2. Added CORS Support

    • Added complete CORS header support
    • Implemented OPTIONS request handling (preflight requests)
    • Added configurable CORS settings
  3. Added Tool Discovery API

    • Implemented the /tools endpoint that returns information about available tools
  4. Improved HTTP Functionality

    • Added better error handling
    • Improved HTTP header handling

Implementation Details

Circular Buffer Implementation

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

CORS Support

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.

Tool Discovery API

The new /tools endpoint returns information about available tools on the server, including:

  • Tool name
  • Tool description
  • Parameter information (name, type, description, whether required)

Testing

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

Running Tests

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-server

Test Content

The test script tests the following:

  1. Root Endpoint Test

    • Checks if the server responds normally
  2. Echo Tool Test

    • Tests basic tool call functionality
  3. Reverse Tool Test

    • Tests more complex tool call functionality
  4. CORS Support Test

    • Tests preflight requests and CORS headers
  5. SSE Event Test

    • Tests SSE connections and event reception

Configuration

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;

Usage Example

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);

Conclusion

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.