MCP工具网关
一个具有x402支付集成的去中心化、代理原生工具市场。
状态: MVP开发\ 作者 @开爪鼠李
______________________________________________________________________
概述
MCP工具网关使AI代理能够以编程方式发现、验证、支付和执行工具。基于类似于x402(HTTP 402 Payment Required)的原则,它将默认支付模型扩展到MCP(模型上下文协议)生态系统。
核心论文: 在代理经济中,工具就是基础设施。基础设施应该自动货币化、无需信任和机器可读。
______________________________________________________________________
特性
核心
- x402支付引擎 -HTTP 402支付要求模式
- 工具注册表 -注册、发现和管理MCP工具
- MCP协议适配器 -JSON-RPC 2.0兼容
- 托管经理 -通过争议解决确保资金持有
安全性(v1.0增强版)
- DID身份验证 -去中心化身份
- 可验证的凭据 -信任验证
- 欺诈检测 -基于机器学习的异常检测
- 加密实用程序 -ZK证明、签名、Merkle树
执行
- 多级沙盒 -WASM、VM、Docker、气隙
- 认证系统 -加密执行证明
- 工具组成 -链式多工具
______________________________________________________________________
快速开始
安装
# Clone repository
cd mcp-tool-gateway
# Install dependencies
pip install -e .
# Or install from source
pip install poetry
poetry install运行服务器
# Development
python -m mcp_gateway.main
# Or with uvicorn
uvicorn mcp_gateway.main:app --reload --host 0.0.0.0 --port 8000运行测试
pytest tests/______________________________________________________________________
建筑
┌─────────────────────────────────────────────────────────────────┐
│ MCP Tool Gateway │
├─────────────────────────────────────────────────────────────────┤
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │
│ │ Tool │ │ Auth │ │ Payment │ │
│ │ Registry │ │ Layer │ │ Engine │ │
│ │ │ │ (DID/VC) │ │ (x402 + Escrow) │ │
│ └─────────────┘ └─────────────┘ └─────────────────────────┘ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │
│ │ Executor │ │ Discovery │ │ Security │ │
│ │ Pool │ │ Engine │ │ (Fraud + Crypto) │ │
│ │ (Sandbox) │ │ (Vector) │ │ │ │
│ └─────────────┘ └─────────────┘ └─────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘______________________________________________________________________
API终点
MCP协议
POST /v1/execute # MCP JSON-RPC endpoint工具管理
GET /api/v1/tools # List tools
POST /api/v1/tools # Register tool
GET /api/v1/tools/{id} # Get tool
DELETE /api/v1/tools/{id} # Delete tool
GET /api/v1/tools/search # Search tools
GET /api/v1/categories # Get categories工具执行
POST /api/v1/execute # Execute tool支付
POST /api/v1/payments/challenge # Create payment challenge
POST /api/v1/payments/verify # Verify payment
POST /api/v1/payments/authorize # Authorize payment
GET /api/v1/payments/spending/{agent_id} # Get spending托管
POST /api/v1/escrow # Create escrow
GET /api/v1/escrow/{id} # Get escrow
POST /api/v1/escrow/{id}/fund # Fund escrow
POST /api/v1/escrow/{id}/lock # Lock escrow
POST /api/v1/escrow/{id}/complete # Complete execution
POST /api/v1/escrow/{id}/dispute # Open dispute______________________________________________________________________
使用示例
注册工具
import httpx
# Create tool
response = httpx.post("http://localhost:8000/api/v1/tools", json={
"name": "web-search",
"description": "Search the web",
"provider_id": "my-agent",
"provider_address": "0x1234...",
"category": "search",
"pricing": {
"type": "per_call",
"price": "500000" # 0.0005 USDC
}
})
tool = response.json()使用支付执行工具
# Execute tool
response = httpx.post("http://localhost:8000/api/v1/execute", json={
"tool_name": "web-search",
"arguments": {"query": "latest AI news"},
"auto_pay": True,
"max_price": "1000000"
})
if response.status_code == 402:
# Payment required
challenge = response.json()["error"]["data"]
print(f"Pay {challenge['amount']} USDC to {challenge['recipient']}")
else:
result = response.json()
print(result["result"])MCP JSON-RPC
# MCP request
request = {
"jsonrpc": "2.0",
"id": 1,
"method": "tools/execute",
"params": {
"tool": "web-search",
"arguments": {"query": "hello"}
}
}
response = httpx.post("http://localhost:8000/v1/execute", json=request)______________________________________________________________________
配置
创建 .env 文件:
# App
DEBUG=true
HOST=0.0.0.0
PORT=8000
# Network
NETWORK=base-sepolia
RPC_URL=https://sepolia.base.org
# Payment
PAYMENT_TOKEN=USDC
PAYMENT_TOKEN_ADDRESS=0x036CbD53842c5426634e7929541eC2318f3dCF7e
# Security
HIGH_VALUE_THRESHOLD=100.0
MAX_DAILY_SPENT=1000.0
# Escrow
ESCROW_TIMEOUT=300
DISPUTE_WINDOW=86400______________________________________________________________________
项目结构
mcp-tool-gateway/
├── src/mcp_gateway/
│ ├── __init__.py
│ ├── main.py # FastAPI app
│ ├── config.py # Configuration
│ ├── core/
│ │ ├── payment.py # x402 Payment Engine
│ │ ├── escrow.py # Escrow Manager
│ │ ├── registry.py # Tool Registry
│ │ └── mcp.py # MCP Protocol
│ ├── security/
│ │ ├── auth.py # DID/VC Auth
│ │ ├── fraud.py # Fraud Detection
│ │ └── crypto.py # Cryptography
│ ├── execution/
│ │ ├── sandbox.py # Sandboxing
│ │ └── executor.py # Tool Executor
│ └── discovery/
│ └── search.py # Vector Search
├── tests/
├── examples/
└── contracts/ # Smart contracts (future)______________________________________________________________________
路线图
v0.1(当前)-MVP
- \[x\] 核心支付引擎
- \[x\] 基本工具注册表
- \[x\] 简单的MCP适配器
- \[x\] 内存托管
- \[x\] API基础
v0.2-安全
- \[\]DID/VC身份验证
- \[\]欺诈检测集成
- \[\]速率限制
v0.3-高级执行
- \[\]沙盒集成
- \[\]认证系统
- \[\]工具组成
v1.0-生产
- \[\]链上注册表
- \[\]真实支付集成
- \[\]分布式执行
______________________________________________________________________
参考文献
______________________________________________________________________
许可证
麻省理工学院
