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
impl McpRunner
Sourcepub fn from_config_file(path: impl AsRef<Path>) -> Result<Self>
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
.
Sourcepub fn from_config_str(config: &str) -> Result<Self>
pub fn from_config_str(config: &str) -> Result<Self>
Create a new MCP runner from a configuration string
This method is instrumented with tracing
.
Sourcepub fn new(config: Config) -> Self
pub fn new(config: Config) -> Self
Create a new MCP runner from a configuration
This method is instrumented with tracing
.
Sourcepub async fn start_server(&mut self, name: &str) -> Result<ServerId>
pub async fn start_server(&mut self, name: &str) -> Result<ServerId>
Start a specific MCP server
This method is instrumented with tracing
.
Sourcepub async fn start_all_servers(&mut self) -> Result<Vec<ServerId>>
pub async fn start_all_servers(&mut self) -> Result<Vec<ServerId>>
Start all configured servers
This method is instrumented with tracing
.
Sourcepub async fn start_all_with_proxy(&mut self) -> (Result<Vec<ServerId>>, bool)
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
.
Sourcepub async fn stop_server(&mut self, id: ServerId) -> Result<()>
pub async fn stop_server(&mut self, id: ServerId) -> Result<()>
Stop a running server
This method is instrumented with tracing
.
Sourcepub async fn stop_all_servers(&mut self) -> Result<()>
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
.
Sourcepub fn server_status(&self, id: ServerId) -> Result<ServerStatus>
pub fn server_status(&self, id: ServerId) -> Result<ServerStatus>
Get server status
This method is instrumented with tracing
.
Sourcepub fn get_server_id(&self, name: &str) -> Result<ServerId>
pub fn get_server_id(&self, name: &str) -> Result<ServerId>
Get server ID by name
This method is instrumented with tracing
.
Sourcepub fn get_client(&mut self, id: ServerId) -> Result<McpClient>
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.
Sourcepub async fn start_sse_proxy(&mut self) -> Result<()>
pub async fn start_sse_proxy(&mut self) -> Result<()>
Start the SSE proxy server if enabled in configuration
This method is instrumented with tracing
.
Sourcepub fn is_sse_proxy_configured(&self) -> bool
pub fn is_sse_proxy_configured(&self) -> bool
Check if the SSE proxy is enabled in configuration
This method is instrumented with tracing
.
Sourcepub fn get_sse_proxy_config(&self) -> Result<&SSEProxyConfig>
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
.
Sourcepub fn get_sse_proxy_handle(&self) -> Result<&SSEProxyHandle>
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
.
Sourcepub fn get_all_server_statuses(&self) -> HashMap<String, ServerStatus>
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
.
Sourcepub async fn get_server_tools(&mut self, name: &str) -> Result<Vec<Tool>>
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
.
Sourcepub async fn get_all_server_tools(
&mut self,
) -> HashMap<String, Result<Vec<Tool>>>
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
.