pub struct SimpleMcpServer { /* private fields */ }Implementations§
Source§impl SimpleMcpServer
impl SimpleMcpServer
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
examples/mcp_ping_example.rs (line 10)
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7 println!("开始 MCP Ping 示例");
8
9 // 创建 MCP 服务器
10 let server = SimpleMcpServer::new();
11
12 // 启动服务器
13 let server_address = "127.0.0.1:6000";
14 server.start(server_address).await?;
15
16 // 等待服务器启动
17 tokio::time::sleep(Duration::from_millis(100)).await;
18
19 // 创建 MCP 客户端
20 let mut client = SimpleMcpClient::new(format!("http://{}", server_address));
21
22 // 连接到服务器 - 修复URL格式
23 client.connect(&format!("http://{}", server_address)).await?;
24 client.set_server_connected(true);
25
26 // 执行多次 ping 测试
27 for i in 1..=3 {
28 let start_time = std::time::Instant::now();
29
30 match client.ping().await {
31 Ok(_) => {
32 let elapsed = start_time.elapsed();
33 println!("Ping {} 成功,响应时间: {:?}", i, elapsed);
34 }
35 Err(e) => {
36 println!("Ping {} 失败: {}", i, e);
37 }
38 }
39
40 // 等待 1 秒再进行下一次 ping
41 tokio::time::sleep(Duration::from_secs(1)).await;
42 }
43
44 // 停止服务器
45 server.stop().await?;
46
47 println!("MCP Ping 示例完成");
48
49 Ok(())
50}More examples
examples/mcp_server_complete_example.rs (line 195)
191async fn main() -> Result<(), Box<dyn std::error::Error>> {
192 println!("=== Rust Agent Complete MCP Server Example ===");
193
194 // 创建MCP服务器实例
195 let server = rust_agent::SimpleMcpServer::new().with_address("127.0.0.1:6000".to_string());
196
197 // 创建实际的工具实现
198 let weather_tool = WeatherTool::new();
199 let calculator_tool = CalculatorTool::new();
200
201 // 注册工具
202 server.register_tool(Arc::new(weather_tool))?;
203 server.register_tool(Arc::new(calculator_tool))?;
204
205 // 启动服务器
206 server.start("127.0.0.1:6000").await?;
207
208 println!("MCP服务器已启动,地址: 127.0.0.1:6000");
209 println!("MCP Server端工具:");
210 println!(" 1. get_weather: Get the weather information for a specified city. For example: 'What's the weather like in Beijing?'");
211 println!(" 2. simple_calculate: Perform simple mathematical calculations. Input should be a mathematical expression with numbers and operators (+, -, *, /). For example: '15.5 + 24.3'");
212 println!("服务器正在运行中,按 Ctrl+C 停止服务器");
213
214 // 保持服务器持续运行
215 // tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
216 // 这里我们使用 tokio::signal 来等待 Ctrl+C 信号
217 #[cfg(unix)]
218 let terminate = async {
219 tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
220 .unwrap()
221 .recv()
222 .await;
223 };
224
225 #[cfg(not(unix))]
226 let terminate = std::future::pending::<()>();
227
228 tokio::select! {
229 _ = tokio::signal::ctrl_c() => {
230 println!("收到 Ctrl+C 信号,正在停止服务器...");
231 },
232 _ = terminate => {
233 println!("收到终止信号,正在停止服务器...");
234 },
235 }
236
237 // 停止服务器
238 // server.stop().await?;
239 println!("MCP服务器已停止");
240
241 Ok(())
242}Sourcepub fn with_address(self, address: String) -> Self
pub fn with_address(self, address: String) -> Self
Examples found in repository?
examples/mcp_server_complete_example.rs (line 195)
191async fn main() -> Result<(), Box<dyn std::error::Error>> {
192 println!("=== Rust Agent Complete MCP Server Example ===");
193
194 // 创建MCP服务器实例
195 let server = rust_agent::SimpleMcpServer::new().with_address("127.0.0.1:6000".to_string());
196
197 // 创建实际的工具实现
198 let weather_tool = WeatherTool::new();
199 let calculator_tool = CalculatorTool::new();
200
201 // 注册工具
202 server.register_tool(Arc::new(weather_tool))?;
203 server.register_tool(Arc::new(calculator_tool))?;
204
205 // 启动服务器
206 server.start("127.0.0.1:6000").await?;
207
208 println!("MCP服务器已启动,地址: 127.0.0.1:6000");
209 println!("MCP Server端工具:");
210 println!(" 1. get_weather: Get the weather information for a specified city. For example: 'What's the weather like in Beijing?'");
211 println!(" 2. simple_calculate: Perform simple mathematical calculations. Input should be a mathematical expression with numbers and operators (+, -, *, /). For example: '15.5 + 24.3'");
212 println!("服务器正在运行中,按 Ctrl+C 停止服务器");
213
214 // 保持服务器持续运行
215 // tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
216 // 这里我们使用 tokio::signal 来等待 Ctrl+C 信号
217 #[cfg(unix)]
218 let terminate = async {
219 tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
220 .unwrap()
221 .recv()
222 .await;
223 };
224
225 #[cfg(not(unix))]
226 let terminate = std::future::pending::<()>();
227
228 tokio::select! {
229 _ = tokio::signal::ctrl_c() => {
230 println!("收到 Ctrl+C 信号,正在停止服务器...");
231 },
232 _ = terminate => {
233 println!("收到终止信号,正在停止服务器...");
234 },
235 }
236
237 // 停止服务器
238 // server.stop().await?;
239 println!("MCP服务器已停止");
240
241 Ok(())
242}Trait Implementations§
Source§impl McpServer for SimpleMcpServer
impl McpServer for SimpleMcpServer
fn start<'life0, 'life1, 'async_trait>(
&'life0 self,
address: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn register_tool(&self, tool: Arc<dyn Tool>) -> Result<(), Error>
fn stop<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Auto Trait Implementations§
impl Freeze for SimpleMcpServer
impl RefUnwindSafe for SimpleMcpServer
impl Send for SimpleMcpServer
impl Sync for SimpleMcpServer
impl Unpin for SimpleMcpServer
impl UnsafeUnpin for SimpleMcpServer
impl UnwindSafe for SimpleMcpServer
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