模型控制协议服务器

一个基于Rust实现的模型控制协议(MCP)服务器,用于通过标准化协议实现客户端与AI模型之间的通信,适用于AI代理系统、LLM工具和自定义JSON-RPC 2.0服务层开发。

  • by marekkucak
  • Rust
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 marekkucak/anet-mcp-server

使用说明

Anet MCP服务器

Rust实现 模型控制协议(MCP) 服务器,通过标准化协议实现客户端和AI模型之间的通信。

该项目提供了一个可扩展的异步框架,用于使用 锈, 东京,以及 NATS。它是为开发商建筑而设计的 AI代理系统, 基于LLM的工具,或自定义 JSON-RPC 2.0 服务层。该架构支持实时消息传递,非常适合 微服务, AI编排,以及 基于工具的模型交互.


特性

  • ✅ JSON-RPC 2.0兼容API
  • 🔄 用于消息传递的NATS传输层
  • 🛠️ 可扩展工具系统
  • 🧠 支持提示和资源
  • ⚡ 与Tokio的异步请求处理

需求

  • 锈 1.70+
  • NATS 本地运行或可通过网络访问的服务器

安装

将以下内容添加到您的 Cargo.toml:

[dependencies]
anet_mcp_server = "0.1.0"

入门指南

运行示例服务器

存储库包括一个演示核心功能的基本示例服务器:

# Start a NATS server in another terminal or ensure one is already running
# Example:
nats-server

# Run the example server
cargo run --example basic_server

测试服务器

您可以使用附带的测试客户端测试服务器:

cargo run --example test_client

这将向服务器发送各种请求并打印响应。


用法

创建服务器

use anet_mcp_server::{
    ServerBuilder, ServerCapabilities, 
    transport::nats::NatsTransport,
};
use serde_json::json;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let transport = NatsTransport::new("nats://localhost:4222", "mcp.requests").await?;

    let server = ServerBuilder::new()
        .transport(transport)
        .name("my-mcp-server")
        .version("0.1.0")
        .capabilities(ServerCapabilities {
            tools: Some(json!({})),
            prompts: Some(json!({})),
            resources: Some(json!({})),
            notification_options: None,
            experimental_capabilities: None,
        })
        .build()?;

    server.run().await
}

实现自定义工具

use anet_mcp_server::{Content, Tool};
use async_trait::async_trait;
use serde_json::{json, Value};

struct MyTool;

#[async_trait]
impl Tool for MyTool {
    fn name(&self) -> String {
        "my_tool".to_string()
    }

    fn description(&self) -> String {
        "A custom tool".to_string()
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "input": { "type": "string" }
            }
        })
    }

    async fn call(&self, input: Option<Value>) -> anyhow::Result<Vec<Content>> {
        Ok(vec![Content::Text {
            text: "Tool response".to_string()
        }])
    }
}

api参考

服务器实现了以下功能 JSON-RPC 方法:

  • initialize –初始化连接并获取服务器信息
  • listTools –获取可用工具列表
  • callTool –调用带有参数的特定工具
  • listResources –获取可用资源列表
  • readResource –阅读特定资源
  • listPrompts –获取可用提示列表
  • getPrompt –获取带有参数的特定提示

建筑

服务器采用模块化设计:

  • 服务器 –核心服务器逻辑和请求处理
  • 运输 –消息传输层(目前为NATS)
  • 工具 –工具接口和实施
  • 类型 –通用数据结构

许可证

MIT许可证


Let me know if you want badges, contribution guidelines, or example JSON-RPC payloads added to the README as well.