MCP服务器框架

kuri是一个专注于开发者体验和清晰性的框架,用于构建Model Context Protocol (MCP)服务器,使LLM能够执行预定义的功能。

  • by itsaphel
  • Rust
  • 本地部署
  • 13 星标
  • 更新于
在 GitHub 查看 itsaphel/kuri

使用说明

kuri (板栗or 栗子)

kuri 是一个需要构建的框架 模型上下文协议 (MCP)服务器,专注于开发人员人体工程学和清晰度。

MCP允许LLM执行预定义的功能(称为“工具”),这允许它获取数据并产生副作用(即:与外界交互)。LLM只需要提供函数的输入参数,然后执行函数并将响应返回给模型。这些工具由“MCP服务器”提供,可以在本地运行,单个服务器可以提供多个工具。

设计理念

Rust是一种编写可靠MCP服务器的优秀语言,具有强大的类型系统和正确性保证。 kuri 旨在使在Rust中进行MCP服务器编程变得非常愉快,以方便使用Rust构建MCP服务器。我们的设计目标是:

  • 符合人体工程学的开发人员体验: MCP服务器编程应该感觉像正常的Rust编程。工具和提示只是普通的异步Rust函数。
  • 尽量减少宏的使用 (#[tool], #[prompt]):仅用于附加工具和参数描述,不用于复杂的代码生成。
  • 最低样板: 专注于应用程序逻辑,而不是串行化或MCP协议路由。
  • 利用 tower 中间件、服务和实用程序的生态系统。免费获取超时、跟踪、紧急处理等功能,并重用来自的组件 axum, tonic 和 hyper.

以上是我们与其他MCP服务器机箱的区别。我们专注于做一件事,而且做得很好。而且没有神奇的复杂宏,你的应用程序代码仍然不言自明 kuri的内部结构清晰易读。 kuri 也建立在 tower,允许您重用丰富的中间件和层生态系统。

示例

use kuri::{MCPServiceBuilder, ServiceExt, ToolError, prompt, serve, tool, transport::StdioTransport};
use schemars::JsonSchema;
use serde::Deserialize;

#[derive(Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum Operation {
    Add,
    Subtract,
    Multiply,
    Divide,
}

// A pure function that takes three inputs and returns an integer. Descriptions
// for the tool and its parameters help the model decide which tool to use, and
// correctly supply the tool's parameters.
#[tool(
    description = "Perform basic arithmetic operations",
    params(
        x = "First number in the calculation",
        y = "Second number in the calculation",
        operation = "The operation to perform (add, subtract, multiply, divide)"
    )
)]
async fn calculator(x: i32, y: i32, operation: Operation) -> Result<i32, ToolError> {
    match operation {
        Operation::Add => Ok(x + y),
        Operation::Subtract => Ok(x - y),
        Operation::Multiply => Ok(x * y),
        Operation::Divide => {
            if y == 0 {
                Err(ToolError::ExecutionError("Division by zero".to_string()))
            } else {
                Ok(x / y)
            }
        }
    }
}

// Creates a prompt template for text summarisation. The application provides
// the text to summarise, and an optional format parameter (denoted using Rust's
// `Option` type). kuri tells the model that `format` may be omitted.
#[prompt(
    description = "Generates a prompt for summarising text",
    params(
        text = "The text to summarise",
        format = "Optional format for the summary (eg: 'bullet points' or 'Shakespeare')"
    )
)]
async fn summarise_text(text: String, format: Option<String>) -> String {
    let format_instruction = match format {
        Some(f) => format!(" in the format of {}", f),
        None => String::new(),
    };

    format!(
        "Please summarize the following text{}:\n\n{}",
        format_instruction, text
    )
}

#[tokio::main]
async fn main() -> Result<(), TransportError> {
    // Create the MCP service with the server's name
    let service = MCPServiceBuilder::new("kuri's test server".to_string())
        // Register the tool and prompt
        .with_tool(Calculator)
        .with_prompt(SummariseText)
        .build();

    // Serve over the stdio transport
    serve(service.into_request_service(), StdioTransport::new()).await
}

更多内容 示例

要开始,请添加 kuri 以及一些必要的依赖关系 Cargo.toml:

[dependencies]
kuri = "0.1"
async-trait = "0.1"
schemars = "0.8"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1", features = ["full"] }

MCP规范支持

  • [x] 核心生命周期:连接初始化、能力协商和会话控制
  • [x] 工具:功能完整,带有测试
  • [x] 提示:基本完成测试
  • []资源
  • 运输
  • 额外(可选)功能

我们目前的优先事项是增加HTTP传输支持,稳定API,并确保对核心规范的全面支持。

贡献

这个项目的目标是为构建MCP服务器构建一个令人愉快、符合人体工程学、符合习惯的Rust库。它还处于早期阶段,因此结构仍有可能发生变化。如果你喜欢Rust、MCP、构建板条箱或网络协议中的任何一种(也许你喜欢 原黑客!),我们很想有你!参与回购,或 通过电子邮件联系 如果你想聊天!

如果你在项目中使用过这个框架:谢谢你尝试!我很想听听你的经历!