pub trait McpClient: Send + Sync {
// Required methods
fn connect<'life0, 'async_trait>(
&'life0 mut self,
config: McpServerConfig,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn disconnect<'life0, 'life1, 'async_trait>(
&'life0 mut self,
server_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn list_tools<'life0, 'life1, 'async_trait>(
&'life0 self,
server_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<Vec<McpToolInfo>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn call_tool<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
server_name: &'life1 str,
tool_name: &'life2 str,
arguments: Value,
) -> Pin<Box<dyn Future<Output = AgentResult<Value>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait;
fn server_info<'life0, 'life1, 'async_trait>(
&'life0 self,
server_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<McpServerInfo>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn connected_servers(&self) -> Vec<String>;
fn is_connected(&self, server_name: &str) -> bool;
}Expand description
MCP 客户端 Trait
定义与 MCP 服务器通信的接口。
具体实现在 mofa-foundation 层。
§示例
ⓘ
use mofa_kernel::agent::components::mcp::*;
let config = McpServerConfig::stdio(
"github",
"npx",
vec!["-y".into(), "@modelcontextprotocol/server-github".into()],
);
let mut client = MyMcpClient::new();
client.connect(config).await?;
let tools = client.list_tools("github").await?;
for tool in &tools {
println!("{}: {}", tool.name, tool.description);
}
let result = client.call_tool("github", "list_repos", json!({"owner": "mofa-org"})).await?;Required Methods§
Sourcefn connect<'life0, 'async_trait>(
&'life0 mut self,
config: McpServerConfig,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn connect<'life0, 'async_trait>(
&'life0 mut self,
config: McpServerConfig,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
连接到 MCP 服务器
使用给定配置建立与 MCP 服务器的连接。
连接建立后,可以通过 server_name 引用该服务器。
Sourcefn disconnect<'life0, 'life1, 'async_trait>(
&'life0 mut self,
server_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn disconnect<'life0, 'life1, 'async_trait>(
&'life0 mut self,
server_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
断开与 MCP 服务器的连接
Sourcefn list_tools<'life0, 'life1, 'async_trait>(
&'life0 self,
server_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<Vec<McpToolInfo>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn list_tools<'life0, 'life1, 'async_trait>(
&'life0 self,
server_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<Vec<McpToolInfo>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
列出 MCP 服务器提供的工具
Sourcefn call_tool<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
server_name: &'life1 str,
tool_name: &'life2 str,
arguments: Value,
) -> Pin<Box<dyn Future<Output = AgentResult<Value>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn call_tool<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
server_name: &'life1 str,
tool_name: &'life2 str,
arguments: Value,
) -> Pin<Box<dyn Future<Output = AgentResult<Value>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
调用 MCP 服务器上的工具
Sourcefn server_info<'life0, 'life1, 'async_trait>(
&'life0 self,
server_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<McpServerInfo>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn server_info<'life0, 'life1, 'async_trait>(
&'life0 self,
server_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<McpServerInfo>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
获取 MCP 服务器信息
Sourcefn connected_servers(&self) -> Vec<String>
fn connected_servers(&self) -> Vec<String>
获取所有已连接的服务器名称
Sourcefn is_connected(&self, server_name: &str) -> bool
fn is_connected(&self, server_name: &str) -> bool
检查服务器是否已连接