Commune

Commune

site icon
2025.04.10 3
RustAI代理网络资源管理开发效率
Commune是一个Rust库,旨在支持可发现的AI代理网络的开发。它作为[mcp-sdk-rs](https://github.com/jgmartin/mcp-sdk-rs)的包装器,提供了增强的功能,用于在[Model Context Protocol (MCP)](https://spec.modelcontextprotocol.io/specification/2024-11-05/)网络中进行对等发现和资源利用。
View on GitHub

Overview

基本能力

产品定位

Commune是一个用于构建和管理AI代理网络的Rust库,专注于对等发现和资源利用。

核心功能

  • 发现和维护对等MCP服务器列表
  • 利用对等服务器上的可用工具、提示和资源
  • 支持WebSocket通信(带和不带TLS加密)
  • 为AWS、OpenAI等推理API提供类型转换,简化其使用

适用场景

  • 构建分布式AI代理网络
  • 管理和利用多个MCP服务器的资源
  • 简化不同推理API的集成

工具列表

  • PeerBuilder: 用于创建和配置对等MCP服务器
  • ClientBuilder: 用于创建Commune客户端,管理多个对等服务器
  • all_tools(): 获取所有对等服务器的工具
  • all_resources(): 获取所有对等服务器的资源
  • all_prompts(): 获取所有对等服务器的提示
  • subscribe(): 订阅资源更新

常见问题解答

  • 类型转换支持哪些API?
  • 目前支持AWS Bedrock,OpenAI支持即将推出。

使用教程

使用依赖

确保已安装Rust和Cargo。

安装教程

Cargo.toml中添加以下依赖:

[dependencies]
commune = { package = "mcp-commune", version = "0.1.2" }

调试方式

使用以下代码示例进行基本调试:

use commune::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let peer1 = PeerBuilder::new()
        .with_name("everything".to_string())
        .with_url("ws://localhost:8780".to_string())
        .with_description("various example resources".to_string())
        .build()
        .await?;
    let peer2 = PeerBuilder::new()
        .with_name("memory".to_string())
        .with_url("ws://localhost:8781".to_string())
        .with_description("memory based on a knowledge graph".to_string())
        .build()
        .await?;

    let commune_client = ClientBuilder::new()
        .with_peers(vec![peer1.clone(), peer2])
        .build()
        .await?;

    let peer_tools = commune_client.all_tools().await?;
    log::info!("found {} tools", peer_tools.len());

    let peer_resources = commune_client.all_resources().await?;
    log::info!("found {} resources!", peer_resources.len());

    let peer_prompts = commune_client.all_prompts().await?;
    log::info!("found {} prompts!", peer_prompts.len());

    peer1.subscribe("test://static/resource/2").await?;

    Ok(())
}

许可证

该项目遵循 MIT 开源许可条款。