#artificial-intelligence #ai-agents #mcp #claude #mcp-client

kodegen_server_http

KODEGEN.ᴀɪ: Database query and schema exploration MCP tools for AI agents

14 releases

Uses new Rust 2024

0.10.11 Jan 2, 2026
0.10.10 Dec 26, 2025
0.8.0 Dec 6, 2025
0.5.0 Nov 29, 2025

#2202 in Development tools

Download history 9/week @ 2025-10-28 189/week @ 2025-11-04 82/week @ 2025-11-11 64/week @ 2025-11-18 36/week @ 2025-11-25 77/week @ 2025-12-02 67/week @ 2025-12-09 12/week @ 2025-12-16 11/week @ 2025-12-23 8/week @ 2025-12-30 12/week @ 2026-01-06 25/week @ 2026-01-13 45/week @ 2026-01-20 34/week @ 2026-01-27

116 downloads per month
Used in 21 crates (17 directly)

Apache-2.0 OR MIT

4MB
2K SLoC

Kodegen AI Banner

kodegen-server-http

License

HTTP/HTTPS server infrastructure for building MCP (Model Context Protocol) tools servers.

Overview

kodegen-server-http is a Rust library that provides the foundation for creating category-specific MCP servers that expose tools and prompts via HTTP. It handles all the boilerplate including:

  • HTTP/HTTPS server setup with optional TLS
  • MCP protocol implementation via rmcp Streamable HTTP transport
  • Graceful shutdown coordination
  • Configuration management
  • Usage tracking
  • Tool and prompt routing
  • Manager lifecycle coordination

This library is designed to be used by category servers (filesystem, browser, database, etc.) - it is not a standalone application.

Features

  • 🚀 Easy Integration - Single run_http_server() call handles all setup
  • 🔒 TLS Support - Optional HTTPS with certificate-based encryption
  • 🎯 Graceful Shutdown - Coordinated shutdown of HTTP server and managed resources
  • 📊 Built-in Tracking - Automatic usage tracking and tool history
  • 🔄 Stateful Sessions - Support for stateful HTTP sessions with SSE keep-alive
  • 🌐 CORS Enabled - Permissive CORS for cross-origin requests

Quick Start

Create a category server in main.rs:

use kodegen_server_http::{run_http_server, RouterSet, Managers, register_tool};
use rmcp::handler::server::router::{prompt::PromptRouter, tool::ToolRouter};
use anyhow::Result;

#[tokio::main]
async fn main() -> Result<()> {
    run_http_server("my-category", |config, tracker| {
        let mut tool_router = ToolRouter::new();
        let mut prompt_router = PromptRouter::new();
        let mut managers = Managers::new();

        // Register your tools
        (tool_router, prompt_router) = register_tool(
            tool_router,
            prompt_router,
            MyTool::new(config.clone()),
        );

        // Register managers that need shutdown
        // let my_manager = Arc::new(MyManager::new());
        // managers.register(my_manager.clone());

        Ok(RouterSet::new(tool_router, prompt_router, managers))
    }).await
}

CLI Usage

# Basic HTTP server
cargo run -- --http 127.0.0.1:8080

# HTTPS with TLS
cargo run -- --http 127.0.0.1:8443 \
  --tls-cert /path/to/cert.pem \
  --tls-key /path/to/key.pem

# Custom shutdown timeout
cargo run -- --http 127.0.0.1:8080 --shutdown-timeout-secs 60

CLI Options

Option Required Description Default
--http <ADDRESS> Yes HTTP server bind address (e.g., 127.0.0.1:8080) -
--tls-cert <PATH> No Path to TLS certificate file (enables HTTPS) -
--tls-key <PATH> No Path to TLS private key file -
--shutdown-timeout-secs <SECONDS> No Graceful shutdown timeout 30

Note: Both --tls-cert and --tls-key must be provided together to enable HTTPS.

Architecture

Inversion of Control

The library uses an inversion of control pattern. You provide a registration callback to run_http_server(), and the library handles:

  1. Environment initialization (logging, rustls crypto provider)
  2. CLI argument parsing
  3. Configuration and usage tracker setup
  4. Calling your registration callback to build routers
  5. HTTP/HTTPS server creation and startup
  6. Signal handling (SIGINT, SIGTERM, SIGHUP)
  7. Graceful shutdown coordination

Components

RouterSet<S>

Container holding your tool router, prompt router, and managers.

pub struct RouterSet<S> {
    pub tool_router: ToolRouter<S>,
    pub prompt_router: PromptRouter<S>,
    pub managers: Managers,
}

HttpServer

The MCP server implementation that serves tools via Streamable HTTP. Implements the rmcp::ServerHandler trait.

Managers

Container for components requiring graceful shutdown (browsers, tunnels, background tasks, etc.).

let my_manager = Arc::new(MyManager::new());
managers.register(my_manager.clone());

Your manager must implement the ShutdownHook trait:

impl ShutdownHook for MyManager {
    fn shutdown(&self) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>> {
        Box::pin(async move {
            // Cleanup logic here
            Ok(())
        })
    }
}

Tool Registration

Two helper functions for registering tools:

// Takes ownership, wraps in Arc
let (tool_router, prompt_router) = register_tool(
    tool_router,
    prompt_router,
    MyTool::new(config.clone()),
);

// For pre-Arc'd tools (when you need a reference)
let my_tool = Arc::new(MyTool::new(config.clone()));
let (tool_router, prompt_router) = register_tool_arc(
    tool_router,
    prompt_router,
    my_tool.clone(),
);

Graceful Shutdown

The library implements a coordinated graceful shutdown:

  1. Signal Received - SIGINT/SIGTERM/SIGHUP (Unix) or Ctrl+C (Windows)
  2. Shutdown Initiated - CancellationToken triggers
  3. Parallel Shutdown:
    • HTTP server begins graceful shutdown (20s timeout)
    • Manager shutdown starts after 2s delay (allows in-flight requests to complete)
  4. Completion - Waits up to --shutdown-timeout-secs for all to complete

Development

# Build
cargo build

# Run tests
cargo test

# Run Clippy
cargo clippy

# Format code
cargo fmt

# Build release
cargo build --release

Requirements

  • Rust: Nightly toolchain (see rust-toolchain.toml)
  • Components: rustfmt, clippy
  • Targets: x86_64-apple-darwin, wasm32-unknown-unknown

License

Dual-licensed under Apache 2.0 or MIT terms. See LICENSE.md for details.

Author

KODEGEN.ᴀɪ

Dependencies

~47–86MB
~1M SLoC