pub struct McpClient { /* private fields */ }
Expand description
MCP client that manages connections to multiple MCP servers
The main interface for interacting with Model Context Protocol servers. Handles connection lifecycle, tool discovery, and provides integration with tool calling systems.
§Features
- Multi-server Management: Connects to and manages multiple MCP servers simultaneously
- Automatic Tool Discovery: Discovers available tools from connected servers
- Tool Registration: Converts MCP tools to internal Tool format for seamless integration
- Connection Pooling: Maintains persistent connections for efficient tool execution
- Error Handling: Robust error handling with proper cleanup and reconnection logic
§Example
use mistralrs_mcp::{McpClient, McpClientConfig};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = McpClientConfig::default();
let mut client = McpClient::new(config);
// Initialize all configured server connections
client.initialize().await?;
// Get tool callbacks for model integration
let callbacks = client.get_tool_callbacks_with_tools();
Ok(())
}
Implementations§
Source§impl McpClient
impl McpClient
Sourcepub fn new(config: McpClientConfig) -> McpClient
pub fn new(config: McpClientConfig) -> McpClient
Create a new MCP client with the given configuration
Sourcepub async fn initialize(&mut self) -> Result<(), Error>
pub async fn initialize(&mut self) -> Result<(), Error>
Initialize connections to all configured servers
Sourcepub fn get_tool_callbacks(
&self,
) -> &HashMap<String, Arc<dyn Fn(&CalledFunction) -> Result<String, Error> + Send + Sync>>
pub fn get_tool_callbacks( &self, ) -> &HashMap<String, Arc<dyn Fn(&CalledFunction) -> Result<String, Error> + Send + Sync>>
Get tool callbacks that can be used with the existing tool calling system
Sourcepub fn get_tool_callbacks_with_tools(
&self,
) -> &HashMap<String, ToolCallbackWithTool>
pub fn get_tool_callbacks_with_tools( &self, ) -> &HashMap<String, ToolCallbackWithTool>
Get tool callbacks with their associated Tool definitions
Sourcepub fn get_tools(&self) -> &HashMap<String, McpToolInfo>
pub fn get_tools(&self) -> &HashMap<String, McpToolInfo>
Get discovered tools information
Auto Trait Implementations§
impl Freeze for McpClient
impl !RefUnwindSafe for McpClient
impl Send for McpClient
impl Sync for McpClient
impl Unpin for McpClient
impl !UnwindSafe for McpClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> Downcast for Twhere
T: AsAny + ?Sized,
impl<T> Downcast for Twhere
T: AsAny + ?Sized,
§fn downcast_ref<T>(&self) -> Option<&T>where
T: AsAny,
fn downcast_ref<T>(&self) -> Option<&T>where
T: AsAny,
Forward to the method defined on the type
Any
.§fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: AsAny,
fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: AsAny,
Forward to the method defined on the type
Any
.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more