run

Function run 

Source
pub fn run<T>(builder: ServerBuilder) -> Result<(), String>
where T: ToolBox + TryFrom<CallToolRequestParams, Error = CallToolError> + Send + Sync + 'static,
Expand description

Runs an MCP server with automatically generated command-line interface.

This function creates a complete CLI application from a ServerBuilder configuration and tool collection. It handles argument parsing, generates help text with tool descriptions, and starts the server in either stdio or HTTP mode based on the provided arguments.

The function automatically parses command-line arguments from std::env::args_os() and configures the server accordingly. If parsing fails, it displays help and exits. Runtime errors are returned as formatted strings.

§Type Parameters

  • T

    A type implementing ToolBox that represents your collection of MCP tools. This is generated using the setup_tools! macro from mcp-utils.

§Server Behavior

  • When called without --host or --port the server starts in stdio mode
  • When called with --host and/or --port the server starts an HTTP server with Server-Sent Events

§Examples

use mcp_cli_builder::{run, ServerBuilder};
use mcp_utils::{tool_prelude::*, server_prelude::*};

setup_tools!(pub MyTools, [
    text(ExampleTool),
]);

fn main() -> Result<(), String> {
    let builder = ServerBuilder::new()
        .with_name(env!("CARGO_PKG_NAME")) // uses the name from Cargo.toml
        .with_version(env!("CARGO_PKG_VERSION")) // uses the version from Cargo.toml
        .with_title("My MCP Server")
        .with_instructions("Demonstrates MCP server functionality");

    run::<MyTools>(builder)
}