| Crates.io | anp-rs-sdk |
| lib.rs | anp-rs-sdk |
| version | 0.1.3 |
| created_at | 2025-09-11 15:55:48.505461+00 |
| updated_at | 2025-10-09 09:02:39.358138+00 |
| description | ANP Rust SDK - 智能体网络协议自动配置工具包,提供HTTP服务器自动配置、DID自动生成、智能体描述等功能 |
| homepage | https://alou.fun |
| repository | https://github.com/logos-42/anp-rust-sdk |
| max_upload_size | |
| id | 1834014 |
| size | 444,023 |
ANP Rust SDK 是智能体网络协议(Agent Network Protocol)的 Rust 实现,提供完整的自动配置工具包,包括 HTTP 服务器自动配置、DID 自动生成、智能体描述等功能。
did:wba 和 did:web 格式在你的 Cargo.toml 中添加依赖:
[dependencies]
anp-rs-sdk = "0.1.2"
use anp_rs_sdk::{ANPSDK, AutoConfigOptions};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let options = AutoConfigOptions {
auto_start: Some(true),
auto_port: Some(true),
..Default::default()
};
let mut sdk = ANPSDK::new(options);
let config = sdk.start().await?;
println!("HTTP服务器启动在: {}", config.endpoint);
// 你的应用逻辑...
sdk.stop().await?;
Ok(())
}
use anp_rs_sdk::did_auto_config::{DIDAutoConfig, DIDAutoConfigOptions};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let options = DIDAutoConfigOptions {
agent_name: Some("My ANP Agent".to_string()),
..Default::default()
};
let mut did_config = DIDAutoConfig::new(options);
let config = did_config.generate_did().await?;
println!("DID: {}", config.did);
println!("DID文档: {}", serde_json::to_string_pretty(&config.did_document)?);
Ok(())
}
use anp_rs_sdk::{ANPSDK, AutoConfigOptions};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let options = AutoConfigOptions {
auto_start: Some(true),
auto_did: Some(true),
agent_name: Some("My ANP Agent".to_string()),
..Default::default()
};
let mut sdk = ANPSDK::new(options);
let config = sdk.start().await?;
println!("🎉 ANP智能体启动成功!");
println!("- HTTP端点: {}", config.endpoint);
println!("- DID: {}", config.did);
println!("- DID文档: {}/.well-known/did.json", config.endpoint);
println!("- 智能体描述: {}/agents/auto-agent/ad.json", config.endpoint);
// 等待一段时间
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
sdk.stop().await?;
Ok(())
}
SDK 提供了多个示例来帮助你快速上手:
# 基础示例(包含 did:web 支持)
cargo run --example basic_agent_with_did_web
# 完整示例(包含 IPFS 注册)
cargo run --example complete_agent_with_ipfs
# IPFS 注册表演示
cargo run --example ipfs_registry_demo
# 传统示例
cargo run --example basic_http_config
cargo run --example basic_did_config
cargo run --example full_anp_agent
cargo run --example custom_config
let config = sdk.start().await?;
println!("DID (wba): {}", config.did);
println!("DID (web): {}", config.did_web.unwrap());
GET /.well-known/did.json - 返回真实的 DID 文档GET /agents/{id}/ad.json - 返回智能体描述文档POST /anp/api - ANP 协议通信端点let options = AutoConfigOptions {
auto_ipfs_register: Some(true),
ipfs_config: Some(IpfsRegistryConfig {
api_url: "http://127.0.0.1:5001".to_string(),
gateway_url: "https://ipfs.io".to_string(),
pin: true,
}),
..Default::default()
};
GET /.well-known/did.json 现在返回真实的 DID 文档GET /agents/{id}/ad.json 现在返回真实的智能体描述文档POST /anp/api 可以接收和处理 ANP 协议消息did:wba 和 did:web 格式KeyPairResult 现在包含 did_web 字段DIDConfig 和 AgentConfig 都包含 did_web 字段did:web 标识符示例:
let config = sdk.start().await?;
println!("DID (wba): {}", config.did);
// 输出: did:wba:127.0.0.1:3000:auto-agent
println!("DID (web): {}", config.did_web.unwrap());
// 输出: did:web:127.0.0.1%3A3000:auto-agent
ipfs_registry 模块,提供完整的 IPFS 注册表功能使用方式:
// 自动注册到 IPFS
let options = AutoConfigOptions {
auto_ipfs_register: Some(true),
ipfs_config: Some(IpfsRegistryConfig {
api_url: "http://127.0.0.1:5001".to_string(),
gateway_url: "https://ipfs.io".to_string(),
pin: true,
}),
..Default::default()
};
let config = sdk.start().await?;
if let Some(cid) = config.ipfs_cid {
println!("IPFS CID: {}", cid);
println!("访问: https://ipfs.io/ipfs/{}", cid);
}
手动使用:
use anp_rs_sdk::{IpfsRegistry, IpfsRegistryConfig, AgentRegistryEntry};
let registry = IpfsRegistry::new(IpfsRegistryConfig::default());
// 发布智能体
let cid = registry.publish_agent(entry).await?;
// 查询智能体
let agent = registry.query_agent(&cid).await?;
// 搜索智能体
let results = registry.search_agents(&index_cid, filter).await?;
basic_agent_with_did_web.rs
complete_agent_with_ipfs.rs
ipfs_registry_demo.rs
// IPFS 注册表配置
pub struct IpfsRegistryConfig {
pub api_url: String,
pub gateway_url: String,
pub pin: bool,
}
// 智能体注册信息
pub struct AgentRegistryEntry {
pub did: String,
pub did_web: Option<String>,
pub name: String,
pub endpoint: String,
pub did_document_url: String,
pub ad_url: String,
pub capabilities: Vec<String>,
pub interfaces: Vec<String>,
pub registered_at: String,
pub updated_at: String,
}
// IPFS 注册表
pub struct IpfsRegistry { /* ... */ }
// 搜索过滤器
pub struct AgentSearchFilter {
pub did: Option<String>,
pub capabilities: Option<Vec<String>>,
pub interfaces: Option<Vec<String>>,
}
// AutoConfigOptions 新增字段
pub struct AutoConfigOptions {
// ... 原有字段 ...
pub auto_ipfs_register: Option<bool>, // 新增
pub ipfs_config: Option<IpfsRegistryConfig>, // 新增
}
// AgentConfig 新增字段
pub struct AgentConfig {
// ... 原有字段 ...
pub did_web: Option<String>, // 新增
pub ipfs_cid: Option<String>, // 新增
}
// KeyPairResult 新增字段
pub struct KeyPairResult {
// ... 原有字段 ...
pub did_web: Option<String>, // 新增
}
// DIDConfig 新增字段
pub struct DIDConfig {
// ... 原有字段 ...
pub did_web: Option<String>, // 新增
}
// HTTPAutoConfig
impl HTTPAutoConfig {
pub async fn set_did_document(&self, doc: Value);
pub async fn set_ad_document(&self, doc: Value);
}
// IpfsRegistry
impl IpfsRegistry {
pub fn new(config: IpfsRegistryConfig) -> Self;
pub async fn publish_agent(&self, entry: AgentRegistryEntry) -> Result<String>;
pub async fn query_agent(&self, cid: &str) -> Result<AgentRegistryEntry>;
pub async fn publish_registry_index(&self, entries: Vec<AgentRegistryEntry>) -> Result<String>;
pub async fn query_registry_index(&self, cid: &str) -> Result<RegistryIndex>;
pub async fn search_agents(&self, index_cid: &str, filter: AgentSearchFilter) -> Result<Vec<AgentRegistryEntry>>;
}
reqwest: 添加 multipart featuremultipart: 新增依赖 0.18IPFS 功能:
ipfs daemon)DID Web 格式:
did:web 格式中的端口号使用 %3A 编码did:web:example.com%3A3000:agent: 替换回端口号向后兼容:
Option<T> 类型cargo run --example basic_agent_with_did_web
# 1. 启动 IPFS 节点
ipfs daemon
# 2. 运行示例
cargo run --example complete_agent_with_ipfs
use anp_rs_sdk::{ANPSDK, AutoConfigOptions};
let options = AutoConfigOptions {
auto_ipfs_register: Some(false), // 暂不使用 IPFS
..Default::default()
};
let mut sdk = ANPSDK::new(options);
let config = sdk.start().await?;
// 访问 DID 文档
// GET http://127.0.0.1:{port}/.well-known/did.json
// 访问 AD 文档
// GET http://127.0.0.1:{port}/agents/auto-agent/ad.json
完整的 API 文档可以在 docs.rs 上找到。
我们欢迎社区贡献!请查看 CONTRIBUTING.md 了解如何参与开发。
本项目采用 MIT 许可证。详情请查看 LICENSE 文件。