MCP Runner
A Rust library for running and interacting with Model Context Protocol (MCP) servers locally.
Overview
MCP Runner provides a complete solution for managing Model Context Protocol servers in Rust applications. It enables:
- Starting and managing MCP server processes
- Configuring multiple servers through a unified interface
- Communicating with MCP servers using JSON-RPC
- Listing and calling tools exposed by MCP servers
- Accessing resources provided by MCP servers
- Proxying Server-Sent Events (SSE) to enable clients to connect to MCP servers
Installation
Add this to your Cargo.toml
:
[]
= "0.3.1"
Quick Start
Here's a simple example of using MCP Runner to start a server and call a tool:
use ;
use ;
use json;
async
Observability
This library uses the tracing
crate for logging and diagnostics. To enable logging, ensure you have a tracing_subscriber
configured in your application and set the RUST_LOG
environment variable. For example:
# Show info level logs for all crates
RUST_LOG=info
# Show trace level logs specifically for mcp_runner
RUST_LOG=mcp_runner=trace
Configuration
MCP Runner uses JSON configuration to define MCP servers and optional SSE proxy settings.
You can load configurations in three different ways:
1. Load from a file
use McpRunner;
let runner = from_config_file?;
2. Load from a JSON string
use McpRunner;
let config_json = r#"{
"mcpServers": {
"fetch": {
"command": "uvx",
"args": ["mcp-server-fetch"]
}
}
}"#;
let runner = from_config_str?;
3. Create programmatically
use ;
use HashMap;
let mut servers = new;
let server_config = ServerConfig ;
servers.insert;
let config = Config ;
// Initialize the runner
let runner = new;
Error Handling
MCP Runner uses a custom error type that covers:
- Configuration errors
- Server lifecycle errors
- Communication errors
- Serialization errors
match result
Core Components
McpRunner
The main entry point for managing MCP servers:
let mut runner = from_config_file?;
let server_ids = runner.start_all_servers.await?;
McpClient
For interacting with MCP servers:
let client = runner.get_client?;
client.initialize.await?;
// Call tools
let result = client.call_tool.await?;
SSE Proxy
The SSE (Server-Sent Events) proxy allows clients to connect to MCP servers through HTTP and receive real-time updates using the Server-Sent Events protocol. Built on Actix Web, it provides a unified JSON-RPC over HTTP interface with high performance, reliability, and maintainability.
Features
- Unified JSON-RPC API: Single endpoint for all MCP server interactions via JSON-RPC
- Authentication: Optional Bearer token authentication for secure access
- Server Access Control: Restrict which servers can be accessed through the proxy
- Event Streaming: Real-time updates from MCP servers to clients via SSE
- Cross-Origin Support: Built-in CORS support for web browser clients
- JSON-RPC Compatibility: Full support for JSON-RPC 2.0 messages in both directions
- Efficient Event Broadcasting: Uses Tokio broadcast channels for efficient event distribution
Starting the Proxy
You can start the SSE proxy automatically when starting your servers:
// Start all servers and the proxy if configured
let = runner.start_all_with_proxy.await;
let server_ids = server_ids?;
if proxy_started
Or manually start it after configuring your servers:
if runner.is_sse_proxy_configured
Proxy Configuration
Configure the SSE proxy in your configuration file:
Proxy API Endpoints
The SSE proxy exposes the following HTTP endpoints:
Endpoint | Method | Description |
---|---|---|
/sse |
GET | SSE event stream endpoint for receiving real-time updates (sends endpoint and message events) |
/sse/messages |
POST | JSON-RPC endpoint for sending requests to MCP servers (supports initialize, tools/list, tools/call, ping) |
Examples
Check the examples/
directory for more usage examples:
-
simple_client.rs
: Basic usage of the client API# Run with info level logging RUST_LOG=info
-
sse_proxy.rs
: Example of using the SSE proxy to expose MCP servers to web clients# Run with info level logging RUST_LOG=info
This example uses the config in
examples/sse_config.json
to start servers and an SSE proxy, allowing web clients to connect and interact with MCP servers through HTTP and SSE.JavaScript client example:
// Connect to the event stream const eventSource = ; // First you'll receive the endpoint information eventSource.; // Then you'll receive JSON-RPC responses eventSource.; // Make a tool call
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the terms in the LICENSE file.