返回市场
内瓦

内瓦

作者:RomanEmreis4 星标更新:2025-11-22

项目介绍

Neva

一款快速且易于配置的 模型上下文协议 (MCP) 服务器和客户端SDK,适用于Rust。 通过简单的配置和符合人体工程学的API,它提供了您快速构建MCP客户端和服务器所需的一切,完全符合最新的MCP规范。

最新版本 Rust编译器 MIT许可证 持续集成 发布

💡 注意:此项目目前处于预览阶段。可能会在没有事先通知的情况下引入破坏性更改。

教程 | API文档 | 示例

主要特性

  • 客户端与服务器SDK - 使用Rust的强大功能构建MCP客户端和服务器的一个库。
  • 性能 - 异步且由Tokio驱动。
  • 传输方式 - 标准输入输出用于本地集成,可流式HTTP用于远程双向通信。
  • 工具资源提示 - 完整支持定义和消费主要的MCP实体。
  • 认证与授权 - 承载令牌认证、基于角色的访问控制等,以满足高标准的安全需求。
  • 结构化数据 - 输出验证、嵌入资源和资源链接开箱即用。
  • 规范对齐 - 设计为跟踪最新的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
}

MCP服务器

依赖项

[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;
}