MCPF代理到代理注册表(A2A)
 ](https://nodejs.org/)    
AI代理的委托控制 -注册表,用于管理哪些代理可以将任务委托给哪些其他代理,并具有策略执行和审计日志记录功能。
🌟 什么是A2A注册?
代理到代理(A2A)注册表管理AI代理之间的委托关系:
Agent A (fraud-detector)
↓ wants to delegate to
Agent B (risk-analyzer)
↓ check registry
A2A Policy: ALLOWED (with constraints)
↓ enforce policy
Delegation proceeds with audit log基于谷歌的A2A协议和MCPF规范
特性
- 🔐 授权政策 -定义谁可以委派给谁
- 📋 策略注册表 -持久存储委托规则
- ✅ 实时授权 -检查委派权限
- 🔍 审计日志 -完整的代表团历史
- ⚡ 政策限制 -时间限制、范围限制、条件
- 🔄 撤销 -即时策略撤销
- 🗄️ PostgreSQL后端 -生产就绪数据库
- 🚀 REST API -简单的HTTP/JSON接口
🚀 快速开始
使用Docker(推荐)
# Clone repository
git clone https://github.com/MCPTrustFramework/MCPF-a2a-registry.git
cd MCPF-a2a-registry
# Start service
docker-compose up -d
# Verify running
curl http://localhost:4003/health
# {"status":"ok"}
# Check delegation policy
curl 'http://localhost:4003/a2a/check?from=did:web:agent1.example&to=did:web:agent2.example'手动安装
# Install dependencies
cd src
npm install
# Set up database
createdb mcpf_a2a
psql mcpf_a2a < db.sql
# Configure
cp .env.example .env
# Edit .env
# Run
npm start📖 api参考
核心终点
检查委派权限
GET /a2a/check?from={fromDID}&to={toDID}&action={action}例子:
curl 'http://localhost:4003/a2a/check?from=did:web:fraud-detector.bank.example&to=did:web:risk-analyzer.bank.example&action=analyze'答复:
{
"allowed": true,
"policy": {
"id": "pol_123",
"fromAgent": "did:web:fraud-detector.bank.example",
"toAgent": "did:web:risk-analyzer.bank.example",
"allowedActions": ["analyze", "query"],
"constraints": {
"maxDuration": 3600,
"scope": ["transaction-data"],
"requiresApproval": false
},
"status": "active",
"issuedBy": "did:web:bank.example",
"validFrom": "2025-01-01T00:00:00Z",
"validUntil": "2026-01-01T00:00:00Z"
}
}列出所有政策
GET /a2a/policies?page=1&limit=50答复:
{
"page": 1,
"limit": 50,
"total": 15,
"items": [
{
"id": "pol_123",
"fromAgent": "did:web:fraud-detector.bank.example",
"toAgent": "did:web:risk-analyzer.bank.example",
"allowedActions": ["analyze", "query"],
"constraints": {...},
"status": "active",
"createdAt": "2025-01-01T00:00:00Z"
}
]
}获取代理策略
GET /a2a/policies/from/:did
GET /a2a/policies/to/:did例子:
# Get all policies where agent can delegate FROM
curl http://localhost:4003/a2a/policies/from/did:web:fraud-detector.bank.example
# Get all policies where others can delegate TO this agent
curl http://localhost:4003/a2a/policies/to/did:web:risk-analyzer.bank.example注册委派策略
POST /a2a/policies
Content-Type: application/json
{
"fromAgent": "did:web:fraud-detector.bank.example",
"toAgent": "did:web:risk-analyzer.bank.example",
"allowedActions": ["analyze", "query", "report"],
"constraints": {
"maxDuration": 3600,
"scope": ["transaction-data"],
"requiresApproval": false,
"maxConcurrent": 5
},
"issuedBy": "did:web:bank.example",
"validFrom": "2025-01-01T00:00:00Z",
"validUntil": "2026-01-01T00:00:00Z"
}撤销政策
POST /a2a/revoke
Content-Type: application/json
{
"policyId": "pol_123",
"reason": "Agent credentials compromised"
}审核日志
GET /a2a/audit?from={fromDID}&to={toDID}&action={action}&startDate={date}&endDate={date}例子:
curl 'http://localhost:4003/a2a/audit?from=did:web:fraud-detector.bank.example&startDate=2025-01-01'答复:
{
"entries": [
{
"id": "audit_456",
"timestamp": "2025-01-15T10:30:00Z",
"fromAgent": "did:web:fraud-detector.bank.example",
"toAgent": "did:web:risk-analyzer.bank.example",
"action": "analyze",
"result": "allowed",
"policyId": "pol_123",
"metadata": {
"requestId": "req_789",
"duration": 245
}
}
]
}🏗️ 建筑
┌─────────────────────────────────────┐
│ HTTP API (Express.js) │
│ /a2a/check, /a2a/policies │
└──────────────┬──────────────────────┘
│
┌──────────────┴──────────────────────┐
│ A2A Authorization Engine │
│ • Policy matching │
│ • Constraint validation │
│ • Audit logging │
└──────────────┬──────────────────────┘
│
┌──────────────┴──────────────────────┐
│ PostgreSQL Database │
│ • a2a_policies table │
│ • a2a_audit_log table │
│ • Indexes on DIDs, actions │
└─────────────────────────────────────┘📊 数据库模式
a2a_policy表
CREATE TABLE a2a_policies (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
from_agent TEXT NOT NULL, -- Delegating agent DID
to_agent TEXT NOT NULL, -- Receiving agent DID
allowed_actions JSONB NOT NULL, -- Array of allowed actions
constraints JSONB NOT NULL DEFAULT '{}', -- Policy constraints
status TEXT NOT NULL DEFAULT 'active', -- active|revoked
issued_by TEXT NOT NULL, -- Policy issuer DID
valid_from TIMESTAMPTZ NOT NULL,
valid_until TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
revoked_at TIMESTAMPTZ,
revocation_reason TEXT,
UNIQUE(from_agent, to_agent)
);a2a_audit_log表
CREATE TABLE a2a_audit_log (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
timestamp TIMESTAMPTZ NOT NULL DEFAULT now(),
from_agent TEXT NOT NULL,
to_agent TEXT NOT NULL,
action TEXT NOT NULL,
result TEXT NOT NULL, -- allowed|denied
policy_id UUID,
metadata JSONB NOT NULL DEFAULT '{}',
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);索引
CREATE INDEX idx_a2a_policies_from ON a2a_policies (from_agent);
CREATE INDEX idx_a2a_policies_to ON a2a_policies (to_agent);
CREATE INDEX idx_a2a_policies_status ON a2a_policies (status);
CREATE INDEX idx_a2a_audit_timestamp ON a2a_audit_log (timestamp DESC);
CREATE INDEX idx_a2a_audit_from ON a2a_audit_log (from_agent);
CREATE INDEX idx_a2a_audit_to ON a2a_audit_log (to_agent);🔐 政策限制
政策支持各种约束:
{
"constraints": {
"maxDuration": 3600, // Max delegation duration (seconds)
"scope": ["data-type-1"], // Data/resource scope
"requiresApproval": false, // Human approval required
"maxConcurrent": 5, // Max concurrent delegations
"allowedDays": ["Mon","Tue"], // Day restrictions
"allowedHours": [9, 17], // Hour restrictions (9 AM - 5 PM)
"ipWhitelist": ["192.168.1.0/24"],// IP restrictions
"conditions": { // Custom conditions
"minimumRiskScore": 0.7,
"requiresEncryption": true
}
}
}🐳 Docker部署
docker-compose.yml
version: '3.8'
services:
a2a-registry:
build: .
ports:
- "4003:4003"
environment:
- DATABASE_URL=postgresql://postgres:password@db:5432/mcpf_a2a
- PORT=4003
depends_on:
- db
restart: unless-stopped
db:
image: postgres:16-alpine
environment:
- POSTGRES_DB=mcpf_a2a
- POSTGRES_PASSWORD=password
volumes:
- postgres_data:/var/lib/postgresql/data
restart: unless-stopped
volumes:
postgres_data:📝 例子
示例1:银行欺诈检测
# Register policy: fraud-detector can delegate to risk-analyzer
curl -X POST http://localhost:4003/a2a/policies \
-H 'Content-Type: application/json' \
-d '{
"fromAgent": "did:web:fraud-detector.bank.example",
"toAgent": "did:web:risk-analyzer.bank.example",
"allowedActions": ["analyze", "query", "report"],
"constraints": {
"maxDuration": 3600,
"scope": ["transaction-data"],
"requiresApproval": false,
"maxConcurrent": 5
},
"issuedBy": "did:web:bank.example",
"validFrom": "2025-01-01T00:00:00Z",
"validUntil": "2026-01-01T00:00:00Z"
}'
# Check if delegation is allowed
curl 'http://localhost:4003/a2a/check?from=did:web:fraud-detector.bank.example&to=did:web:risk-analyzer.bank.example&action=analyze'示例2:医疗诊断链
# Register policy: primary-diagnostics can delegate to specialist-ai
curl -X POST http://localhost:4003/a2a/policies \
-H 'Content-Type: application/json' \
-d '{
"fromAgent": "did:web:primary-diagnostics.hospital.example",
"toAgent": "did:web:radiology-specialist.hospital.example",
"allowedActions": ["analyze-xray", "analyze-ct", "generate-report"],
"constraints": {
"maxDuration": 1800,
"scope": ["radiology-images"],
"requiresApproval": true,
"allowedDays": ["Mon","Tue","Wed","Thu","Fri"]
},
"issuedBy": "did:web:hospital.example",
"validFrom": "2025-01-01T00:00:00Z",
"validUntil": "2025-12-31T23:59:59Z"
}'示例3:客户服务升级
# Register policy: chatbot can escalate to supervisor-ai
curl -X POST http://localhost:4003/a2a/policies \
-H 'Content-Type: application/json' \
-d '{
"fromAgent": "did:web:chatbot-l1.company.example",
"toAgent": "did:web:supervisor-ai.company.example",
"allowedActions": ["escalate", "review", "approve"],
"constraints": {
"maxDuration": 600,
"scope": ["customer-support"],
"requiresApproval": false,
"conditions": {
"minimumSeverity": "medium",
"customerTier": ["premium", "enterprise"]
}
},
"issuedBy": "did:web:company.example",
"validFrom": "2025-01-01T00:00:00Z"
}'🧪 测试
# Run tests
npm test
# With coverage
npm run test:coverage
# Integration tests
npm run test:integration📈 演出
标准硬件(4 CPU,8GB RAM)上的基准测试:
| 操作 | 性能 | 注意事项 |
|---|---|---|
| 检查委托 | ~8ms | 索引查询 |
| 列出策略 | ~15ms | 分页 |
| 注册策略 | ~20ms | 插入+验证 |
| 审核日志条目 | ~5ms | 异步写入 |
| 吞吐量 | ~3000请求/秒 | 检查操作 |
🔗 与MCPF集成
有了MCPF,vc
import { VCVerifier } from 'mcpf-did-vc';
import { A2ARegistry } from 'mcpf-a2a-registry';
const a2a = new A2ARegistry('http://localhost:4003');
const verifier = new VCVerifier();
// Before delegation, verify both agents
const fromAgentValid = await verifier.verifyAgent(fromDID);
const toAgentValid = await verifier.verifyAgent(toDID);
if (fromAgentValid && toAgentValid) {
// Check delegation permission
const result = await a2a.check(fromDID, toDID, 'analyze');
if (result.allowed) {
// Proceed with delegation
await delegateTask(fromDID, toDID, taskData);
}
}使用MCPF ans
import { ANSClient } from 'mcpf-ans';
import { A2ARegistry } from 'mcpf-a2a-registry';
// Resolve agent names
const ans = new ANSClient('https://ans.example.com');
const fromAgent = await ans.resolve('fraud-detector.risk.bank.example.agent');
const toAgent = await ans.resolve('risk-analyzer.analytics.bank.example.agent');
// Check delegation
const a2a = new A2ARegistry('http://localhost:4003');
const result = await a2a.check(fromAgent.card.did, toAgent.card.did, 'analyze');使用谷歌A2A协议
MCPF A2A注册表实现了谷歌的A2A协议概念:
// Google A2A style delegation
{
"agent": "did:web:fraud-detector.bank.example",
"delegateTo": "did:web:risk-analyzer.bank.example",
"task": {
"type": "analyze",
"data": {...}
},
"authorization": {
"registry": "https://a2a-registry.example.com",
"policyId": "pol_123"
}
}🤝 贡献
看 贡献.md 作为指导方针。
📝 许可证
MIT许可证-请参阅 许可证
📞 联系
- 网站: https://mcpf.dev
- github: https://github.com/MCPTrustFramework/MCPF-a2a-registry
- 问题: https://github.com/MCPTrustFramework/MCPF-a2a-registry/issues
- 讨论: https://github.com/MCPTrustFramework/MCPF-a2a-registry/discussions
🙏 致谢
基于:
- 谷歌A2A协议 -代理对代理委托概念
- MCPF规范 -信任框架集成
🔗 相关项目
______________________________________________________________________
版本: 1.0.0-alpha\ 最后更新时间: 2025年12月31日\ 状态: 生产准备就绪
