SimpleMcpServer

Struct SimpleMcpServer 

Source
pub struct SimpleMcpServer { /* private fields */ }

Implementations§

Source§

impl SimpleMcpServer

Source

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
Hide additional 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}
Source

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

Source§

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,

Source§

fn register_tool(&self, tool: Arc<dyn Tool>) -> Result<(), Error>

Source§

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§

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> 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, 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<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<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,