mcpcrs

Crates.iomcpcrs
lib.rsmcpcrs
version0.1.0
created_at2025-10-14 14:48:16.357026+00
updated_at2025-10-14 14:48:16.357026+00
descriptionMCP client library in pure rust.
homepage
repository
max_upload_size
id1882428
size91,067
Florian Loers (floers)

documentation

README

mcpcrs

MCP Client for Rust

Pure rust MCP client library.

Currently only ollama is supported but the abstractions for more llms exist.

Example

You can run this example via cargo run --example ollama_stdout.


// create a server connection (e.g. to a server running via `npx @modelcontextprotocol/server-everything streamableHttp`)
let s1 = ServerConnection::connect("http://localhost:3001/mcp", &info, &caps)
        .await
        .map_err(std::io::Error::other)?;

// put all servers into a server set
let servers = ServerSet::init(vec![s1])
    .await
    .map_err(std::io::Error::other)?;

// create a llm client instance
let ollama = Ollama::new(
    "http://localhost:11434/api/chat",
    "llama3.1:8b".into(),
    "You are a helpful agent.\nUse the available tools when necessary to help the user.\n"
        .into(),
)
.unwrap();

// create the mcp client
let mut client = Client::new(Box::new(ollama), servers);

// get a channel to send messages to end recv events from:
let (s, mut r) = client.channel().unwrap();

// print all events to stdout
tokio::task::spawn(async move {
    loop {
        if let Some(e) = r.recv().await {
            match e {
                client::Event::ToolCall { name, content: _ } => {
                    println!(" - call tool {name}");
                }
                client::Event::ToolCallDone { name, content } => {
                    println!(" - tool {name} done: {}", content.len());
                }
                client::Event::MessageChunk { content } => {
                    print!("{content}");
                    std::io::stdout().flush().unwrap();
                }
                client::Event::MessageDone { content: _ } => {
                    println!("\n");
                    sw.send(()).await.unwrap();
                }
            }
        } else {
            log::error!("receiver stopped.");
            break;
        }
    }
});

// send messages via stdin
tokio::task::spawn(async move {
    loop {
        let line = tokio::task::spawn_blocking(|| {
            let mut s = String::new();
            println!("Enter you prompt and press return:");
            std::io::stdin().read_line(&mut s).unwrap();
            s
        })
        .await
        .unwrap();

        s.send(vec![Message::user(line)]).await.unwrap();

        println!("wait...");

        working.recv().await.unwrap();
    }
});

// run the client
client.run().await.unwrap();
Commit count: 0

cargo fmt