一款快速且易于配置的 模型上下文协议 (MCP) 服务器和客户端SDK,适用于Rust。 通过简单的配置和符合人体工程学的API,它提供了您快速构建MCP客户端和服务器所需的一切,完全符合最新的MCP规范。
💡 注意:此项目目前处于预览阶段。可能会在没有事先通知的情况下引入破坏性更改。
[dependencies]
neva = { version = "0.2.2", features = ["client-full"] }
tokio = { version = "1", features = ["full"] }
use neva::prelude::*;
#[tokio::main]
async fn main() -> Result<(), Error> {
let mut client = Client::new()
.with_options(|opt| opt
.with_stdio("npx", ["-y", "@modelcontextprotocol/server-everything"])
.with_timeout(Duration::from_secs(5)));
client.connect().await?;
// 列出工具
let tools = client.list_tools(None).await?;
for tool in tools.tools {
println!("- {}", tool.name);
}
// 调用一个工具
let args = [
("message", "Hello MCP!")
];
let result = client.call_tool("echo", args).await?;
println!("{:?}", result.content);
client.disconnect().await
}
[dependencies]
neva = { version = "0.2.2", features = ["server-full"] }
tokio = { version = "1", features = ["full"] }
use neva::prelude::*;
#[tool(descr = "一个打招呼的工具")]
async fn hello(name: String) -> String {
format!("你好,{name}!")
}
#[resource(uri = "res://{name}", descr = "关于资源的一些细节")]
async fn get_res(name: String) -> ResourceContents {
ResourceContents::new(format!("res://{name}"))
.with_mime("plain/text")
.with_text(format!("关于资源的一些细节:{name}"))
}
#[prompt(descr = "分析代码以寻找潜在改进")]
async fn analyze_code(lang: String) -> PromptMessage {
PromptMessage::user()
.with(format!("语言:{lang}"))
}
#[tokio::main]
async fn main() {
App::new()
.with_options(|opt| opt
.with_stdio()
.with_name("示例MCP服务器")
.with_version("1.0.0"))
.run()
.await;
}