使用说明
MCP网关MIA-意图感知安全层
用于MCP(模型上下文协议)交互的意图感知安全层。
而不是问 *“此客户端可以访问此工具吗?”*MIA问: “此交易是否符合客户端和服务器之间约定的意图?”
🚀 快速开始
# Clone and setup
git clone <repository-url>
cd mcpgw
# One-command setup (installs Ollama, pulls models, sets up environment)
./setup.sh
# Start all services
./start.sh
# Run complete demonstration
./demo.sh
🎯 MIA是什么?
共同意向协议(MIA) 是一种新的人工智能工具安全方法,它用对客户端意图和服务器功能的动态语义理解取代了静态权限系统。
范式转变
传统安全:
- 静态权限列表
- “用户X可以访问工具Y吗?”
- 二元允许/拒绝决策
- 不理解目的
MIA安保:
- 动态意图分析
- “这笔交易是否符合约定的目的?”
- 对交互的语义理解
- 上下文感知安全决策
🏗️ MIA如何运作
三阶段流程
第一阶段:声明
- 客户 宣布他们的意图: “我需要天气数据来制定旅行计划”
- 服务器 注册功能: “我提供公共天气数据,没有跟踪”
第二阶段:谈判
- LLM分析 意图与能力的语义相容性
- 创建具有约束力的合同 有商定的条款和限制
- 建立信任 通过相互理解
第3阶段:验证
- 每笔交易 根据商定的意图进行验证
- 实时监控 意图漂移和违规
- 双向保护 适用于客户端和服务器
示例流程
# Client declares intent
client_intent = {
"purpose": "I need weather data for travel planning",
"constraints": ["no_personal_data", "read_only", "public_data_only"],
"data_requirements": ["current_weather", "forecasts"]
}
# Server registers capability
server_capability = {
"provides": ["weather_data", "forecasts"],
"boundaries": ["no_pii", "public_data_only"],
"supported_operations": ["read"]
}
# LLM analyzes compatibility
compatibility = await analyzer.analyze_intent_compatibility(
client_intent, server_capability
)
# Contract created if compatible
if compatibility.status == "compatible":
contract = IntentContract(
agreed_purpose=client_intent.purpose,
constraints=client_intent.constraints + compatibility.suggested_constraints
)
🛡️ 双向保护
MIA保护 双方 在AI工具交互中:
服务器保护
- 根据声明的意图验证客户端请求
- 防止范围蔓延和恶意使用
- 检测随时间推移的意图漂移
客户端保护(增强功能)
- 根据客户端约束验证服务器响应
- 防止数据泄露和侵犯隐私
- 阻止跟踪尝试和意外数据
保护示例
场景1:违反意图(服务器保护)
Client Intent: "Weather data for travel planning"
Malicious Request: get_personal_location_history()
MIA Action: ❌ BLOCKED - Violates travel planning intent
场景2:侵犯隐私(客户保护)
Client Constraint: "no_location_tracking"
Server Response: { weather: "sunny", user_location: "37.7749,-122.4194" }
MIA Action: ❌ BLOCKED - Violates no tracking constraint
🧠 LLM动力分析
MIA使用复杂的LLM分析进行语义理解:
意图兼容性分析
system_prompt = """You are an AI security analyst specializing in Mutual Intent Agreement (MIA) systems. Your role is to perform deep semantic analysis of client intentions and server capabilities to determine compatibility for secure AI tool interactions.
CORE PRINCIPLES:
1. Semantic Understanding: Analyze the MEANING and PURPOSE behind requests
2. Intent Alignment: Evaluate if client's declared purpose aligns with server capabilities
3. Risk Assessment: Identify potential security, privacy, and misuse risks
4. Constraint Synthesis: Recommend specific constraints to ensure safe interaction"""
实时交易验证
- 对每个请求进行意图对齐分析
- 上下文感知安全决策
- 所有决策的详细推理
- 信心评分和风险评估
📊 建筑
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ MIA Gateway │ │ Ollama Client │ │ Weather Server │
│ │ │ │ │ │
│ ┌─────────────┐ │ │ ┌─────────────┐ │ │ ┌─────────────┐ │
│ │IntentBroker │ │◄──►│ │LLM Analysis │ │ │ │ MCP Tools │ │
│ └─────────────┘ │ │ └─────────────┘ │ │ └─────────────┘ │
│ ┌─────────────┐ │ │ │ │ │
│ │ Contracts │ │ │ gpt-oss20b-128k │ │ FastAPI Server │
│ └─────────────┘ │ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
└────────────────────────┼────────────────────────┘
│
┌─────────────────┐
│ Travel Client │
│ │
│ Python Client │
│ Demonstrates │
│ Complete MIA │
└─────────────────┘
核心组件
- IntentBroker:协调三阶段MIA流程
- IntentAnalyzer:使用本地Ollama进行LLM驱动的语义分析
- 意向合同:具有生命周期管理约束力的协议
- MIA网关:带有意图验证中间件的FastAPI应用程序
🔧 API终点
意图管理
POST /intent/declare-客户声明意向POST /capability/register-服务器注册功能POST /intent/negotiate-谈判意向合同GET /intent/contracts-列出有效合同GET /intent/capabilities-列出服务器功能
交易处理
POST /mcp/request-意图验证的MCP代理GET /intent/violations-查看合同违规行为GET /status-系统状态和监控
API使用示例
# Declare client intent
curl -X POST http://localhost:8000/intent/declare \
-H "Content-Type: application/json" \
-d '{
"purpose": "I need weather data for travel planning",
"data_requirements": ["current_weather", "forecasts"],
"constraints": ["no_personal_data", "read_only"]
}'
# Negotiate contract
curl -X POST http://localhost:8000/intent/negotiate \
-H "Content-Type: application/json" \
-d '{
"client_intent_id": "intent-123",
"server_capability_id": "weather-server-456"
}'
# Make validated request
curl -X POST http://localhost:8000/mcp/request \
-H "Content-Type: application/json" \
-d '{
"contract_id": "contract-789",
"server_name": "weather-server",
"request": {
"method": "tools/call",
"params": {
"name": "get_current_weather",
"arguments": {"location": "Paris"}
}
}
}'
🐳 地方发展
先决条件
- Docker和Docker Compose
- Python 3.9+
- macOS(用于Ollama集成)
设置
# Complete setup (one command)
./setup.sh
# Manual setup
brew install ollama
ollama pull gpt-oss20b-128k
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
开发命令
# Start all services
./start.sh
# Stop all services
./stop.sh
# Clean restart
./stop.sh --clean && ./start.sh
# View logs
docker compose -f docker/docker-compose.yml logs -f
# Run demo
./demo.sh
服务URL
- MIA网关: http://localhost:8000
- API文档: http://localhost:8000/docs
- 网关健康: http://localhost:8000/health
- API Ollama: http://localhost:11434
📋 演示场景
附带的演示展示:
1.MIA基本流程
- 意向声明和能力登记
- LLM驱动的兼容性分析
- 合同谈判和激活
2.经过意图验证的请求
- 天气要求与旅行计划意图相一致
- 根据合同条款进行实时验证
3.违规检测
- 试图在声明意图之外访问数据
- 具有详细推理的自动阻塞
4.Python客户端集成
- 使用MIA完成申请
- 多目的地旅行计划
- 会话管理和错误处理
5.双向保护
- 服务器响应验证
- 隐私违规检测
- 保护客户端免受恶意服务器的攻击
🔍 输出示例
🔐 Demo 1: Basic Mutual Intent Agreement Flow
==============================================
[SUCCESS] Intent declared successfully!
[SUCCESS] Contract negotiated successfully!
[DEMO] Contract ID: b9fc1ff8-be7d-47fd-a6cc-585e698d8abf
🌤️ Demo 2: Intent-Validated MCP Requests
==========================================
[SUCCESS] Weather request validated and processed!
[SUCCESS] Forecast request validated and processed!
🚨 Demo 3: Intent Violation Detection
=====================================
[SUCCESS] Intent violation correctly detected and blocked!
[DEMO] Validation Result: invalid
[DEMO] Reasons: ['The request calls `get_personal_location_history`, which retrieves personal location data.', 'This violates the constraint `no_personal_data_storage`...']
🎯 关键利益
对于开发者
- 语义安全:理解意图,而不仅仅是权限
- 动态策略:安全适应申报目的
- 透明决策:解释每一个安全决策
- 双向信任:保护客户端和服务器
对于组织
- 降低风险:意图感知安全可防止误用
- 审计跟踪:所有互动和决策的完整记录
- 合规:内置隐私和数据保护
- 可扩展性:没有要维护的静态权限列表
对于AI系统
- 情境感知:基于目的的安全决策
- 意图漂移检测:自动检测行为变化
- 自适应保护:安全性随着使用模式而发展
- 相互信任:双方通过经过核实的意图受到保护
🚀 AI安全的下一步
MIA代表了我们对人工智能工具安全性的看法的转变:
- 从权限到意图:理解目的,而不仅仅是访问
- 从静态到动态:适应环境的安全性
- 从单边到互惠:保护各方
- 从二进制到语义:加深对互动的理解
📚 技术细节
数据模型
- 客户意向声明:客户声明的目的和限制
- 服务器能力声明:服务器的功能和边界
- 意向合同:与生命周期管理具有约束力的协议
- IntentTransactionValidation:实时验证结果
LLM集成
- 当地Ollama:无云依赖,完全隐私
- 128K上下文窗口:处理复杂的意图分析
- 精致的提示:深入的语义理解
- 信心评分:对决策的量化信任
监控和分析
- 实时度量:交易成功率、违规行为
- 意图漂移分析:检测不断变化的行为模式
- 合同生命周期:创建、验证、过期跟踪
- 审计日志:所有安全决策的完整记录
🤝 贡献
该项目展示了意图感知人工智能工具治理的首次实际实施。欢迎捐款:
- 其他意图模式和模板
- 增强的LLM分析能力
- 新的MCP服务器集成
- 安全策略改进
📄 许可证
MIT许可证-有关详细信息,请参阅许可证文件。
MIA代表了人工智能工具安全的下一步——意图对齐取代了权限管理,创建了真正智能和自适应的安全系统。
🚀 准备好见证意图感知安全的实际应用了吗?跑 ./demo.sh 看看它是如何工作的!
