Struct McpRunner

Source
pub struct McpRunner { /* private fields */ }
Expand description

Configure and run MCP servers

This struct is the main entry point for managing MCP server lifecycles and obtaining clients to interact with them. All public methods are instrumented with tracing spans.

Implementations§

Source§

impl McpRunner

Source

pub fn from_config_file(path: impl AsRef<Path>) -> Result<Self>

Create a new MCP runner from a configuration file path

This method is instrumented with tracing.

Source

pub fn from_config_str(config: &str) -> Result<Self>

Create a new MCP runner from a configuration string

This method is instrumented with tracing.

Source

pub fn new(config: Config) -> Self

Create a new MCP runner from a configuration

This method is instrumented with tracing.

Source

pub async fn start_server(&mut self, name: &str) -> Result<ServerId>

Start a specific MCP server

This method is instrumented with tracing.

Source

pub async fn start_all_servers(&mut self) -> Result<Vec<ServerId>>

Start all configured servers

This method is instrumented with tracing.

Source

pub async fn start_all_with_proxy(&mut self) -> (Result<Vec<ServerId>>, bool)

Start all configured servers and the SSE proxy if configured

This is a convenience method that starts all configured MCP servers and then starts the SSE proxy if it’s configured. This ensures that all servers are available before the proxy begins accepting connections.

§Returns

A tuple containing:

  • A Result<Vec<ServerId>> with server IDs for all started servers, or an error
  • A bool indicating whether the SSE proxy was started
§Examples
use mcp_runner::McpRunner;

#[tokio::main]
async fn main() -> mcp_runner::Result<()> {
    // Create runner from config
    let mut runner = McpRunner::from_config_file("config.json")?;

    // Start all servers and proxy if configured
    let (server_ids, proxy_started) = runner.start_all_with_proxy().await;
     
    // Check if servers started successfully
    let server_ids = server_ids?;
    println!("Started {} servers", server_ids.len());
     
    if proxy_started {
        println!("SSE proxy started successfully");
    }
     
    Ok(())
}

This method is instrumented with tracing.

Source

pub async fn stop_server(&mut self, id: ServerId) -> Result<()>

Stop a running server

This method is instrumented with tracing.

Source

pub async fn stop_all_servers(&mut self) -> Result<()>

Stop all running servers and the SSE proxy if it’s running

This method stops all running servers and the SSE proxy (if running). It collects all errors but only returns the first one encountered.

§Returns

A Result<()> indicating success or the first error encountered.

§Examples
use mcp_runner::McpRunner;

#[tokio::main]
async fn main() -> mcp_runner::Result<()> {
    let mut runner = McpRunner::from_config_file("config.json")?;
    runner.start_all_with_proxy().await;
     
    // Later, stop everything
    runner.stop_all_servers().await?;
    println!("All servers and proxy stopped");
     
    Ok(())
}

This method is instrumented with tracing.

Source

pub fn server_status(&self, id: ServerId) -> Result<ServerStatus>

Get server status

This method is instrumented with tracing.

Source

pub fn get_server_id(&self, name: &str) -> Result<ServerId>

Get server ID by name

This method is instrumented with tracing.

Source

pub fn get_client(&mut self, id: ServerId) -> Result<McpClient>

Get a client for a server

This method is instrumented with tracing.

If the client already exists in cache, a ClientAlreadyCached error is returned. In this case, you can retrieve the cached client using specialized methods like get_server_tools that handle the cache internally.

Source

pub async fn start_sse_proxy(&mut self) -> Result<()>

Start the SSE proxy server if enabled in configuration

This method is instrumented with tracing.

Source

pub fn is_sse_proxy_configured(&self) -> bool

Check if the SSE proxy is enabled in configuration

This method is instrumented with tracing.

Source

pub fn get_sse_proxy_config(&self) -> Result<&SSEProxyConfig>

Get the SSE proxy configuration if it exists

Retrieves a reference to the SSE proxy configuration from the runner’s config. This is useful for accessing proxy settings like address and port.

§Returns

A Result containing a reference to the SSEProxyConfig if configured, or an Error::Other if no SSE proxy is configured.

§Examples
use mcp_runner::McpRunner;

#[tokio::main]
async fn main() -> mcp_runner::Result<()> {
    let runner = McpRunner::from_config_file("config.json")?;
     
    if runner.is_sse_proxy_configured() {
        let proxy_config = runner.get_sse_proxy_config()?;
        println!("SSE proxy will listen on {}:{}", proxy_config.address, proxy_config.port);
    }
     
    Ok(())
}

This method is instrumented with tracing.

Source

pub fn get_sse_proxy_handle(&self) -> Result<&SSEProxyHandle>

Get the running SSE proxy handle if it exists

This method provides access to the running SSE proxy handle, which can be used to communicate with the proxy or control it. Note that this will only return a value if the proxy was previously started with start_sse_proxy() or start_all_with_proxy().

§Returns

A Result containing a reference to the running SSEProxyHandle instance, or an Error::Other if no SSE proxy is running.

§Examples
use mcp_runner::McpRunner;

#[tokio::main]
async fn main() -> mcp_runner::Result<()> {
    let mut runner = McpRunner::from_config_file("config.json")?;
     
    // Start servers and proxy
    let (server_ids, proxy_started) = runner.start_all_with_proxy().await;
    let _server_ids = server_ids?;
     
    if proxy_started {
        // Access the running proxy handle
        let proxy_handle = runner.get_sse_proxy_handle()?;
        let config = proxy_handle.config();
        println!("SSE proxy is running on {}:{}", config.address, config.port);
    }
     
    Ok(())
}

This method is instrumented with tracing.

Source

pub fn get_all_server_statuses(&self) -> HashMap<String, ServerStatus>

Get status for all running servers

This method returns a HashMap of server names to their current status. This is a convenience method that can be called at any time to check on all servers.

§Returns

A HashMap<String, ServerStatus> containing the status of all currently running servers.

§Examples
use mcp_runner::McpRunner;

#[tokio::main]
async fn main() -> mcp_runner::Result<()> {
    let mut runner = McpRunner::from_config_file("config.json")?;
    runner.start_all_servers().await?;
     
    // Check status of all servers
    let statuses = runner.get_all_server_statuses();
    for (name, status) in statuses {
        println!("Server '{}' status: {:?}", name, status);
    }
     
    Ok(())
}

This method is instrumented with tracing.

Source

pub async fn get_server_tools(&mut self, name: &str) -> Result<Vec<Tool>>

Get a list of available tools for a specific server

This is a convenience method that creates a temporary client to query tools from a server. Unlike get_client().list_tools(), this method handles all the client creation and cleanup internally.

§Returns

A Result containing a vector of tools provided by the specified server.

§Examples
use mcp_runner::McpRunner;

#[tokio::main]
async fn main() -> mcp_runner::Result<()> {
    let mut runner = McpRunner::from_config_file("config.json")?;
    runner.start_server("fetch").await?;
     
    // Get tools for a specific server
    let tools = runner.get_server_tools("fetch").await?;
    for tool in tools {
        println!("Tool: {} - {}", tool.name, tool.description);
    }
     
    Ok(())
}

This method is instrumented with tracing.

Source

pub async fn get_all_server_tools( &mut self, ) -> HashMap<String, Result<Vec<Tool>>>

Get tools for all running servers

This method returns a HashMap of server names to their available tools. This is a convenience method that can be called at any time to check tools for all running servers.

§Returns

A HashMap<String, Result<Vec<Tool>>> containing the tools of all currently running servers. The Result indicates whether listing tools was successful for each server.

§Examples
use mcp_runner::McpRunner;

#[tokio::main]
async fn main() -> mcp_runner::Result<()> {
    let mut runner = McpRunner::from_config_file("config.json")?;
    runner.start_all_servers().await?;
     
    // Get tools for all servers
    let all_tools = runner.get_all_server_tools().await;
    for (server_name, tools_result) in all_tools {
        match tools_result {
            Ok(tools) => {
                println!("Server '{}' tools:", server_name);
                for tool in tools {
                    println!(" - {}: {}", tool.name, tool.description);
                }
            },
            Err(e) => println!("Failed to get tools for '{}': {}", server_name, e),
        }
    }
     
    Ok(())
}

This method is instrumented with tracing.

Trait Implementations§

Source§

impl Clone for McpRunner

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T