🚀 高级MCP实验室:远程HTTP和安全根
______________________________________________________________________
📌 项目概述
该项目展示了 先进的、生产就绪的实施 模型上下文协议(MCP) --从基本STDIO升级 完全远程 可流式HTTP传输.
它实现了 文件系统根安全边界, 服务器启动LLM采样,以及 OpenAI GPT-4o-mini集成 --企业代理AI部署中使用的真实世界模式。
域名: Agent AI——先进的MCP架构\ 语言: Python 3.11\ 服务器: HTTP打开 http://127.0.0.1:8000/mcp\ 工作区: 严格沙盒化 workspace/ 目录
______________________________________________________________________
🏗️ 系统架构
┌──────────────────────────────────────────────────┐
│ AI Host App │
│ OpenAI GPT-4o-mini │
│ Natural Language → OpenAI Function Calling │
│ → MCP Tool Calls → Results → User │
└─────────────────────┬────────────────────────────┘
│
┌─────────────────────▼────────────────────────────┐
│ Base HTTP Client (MCPHTTPClient) │
│ streamablehttp_client → /mcp endpoint │
│ list_tools / call_tool / list_resources │
│ list_prompts / get_prompt / read_resource │
└─────────────────────┬────────────────────────────┘
│ Streamable HTTP Port 8000
┌─────────────────────▼────────────────────────────┐
│ FastMCP HTTP Server (127.0.0.1:8000) │
│ │
│ 🔧 Tools 📦 Resources │
│ ├── read_file file://workspace/{file} │
│ ├── write_file │
│ ├── list_files 💬 Prompts │
│ └── analyze_code ├── review_code │
│ (Sampling) └── analyze_security │
│ │
│ 🛡️ Roots: workspace/ only — is_within_roots() │
└──────────────────────────────────────────────────┘______________________________________________________________________
📂 项目结构
advanced-mcp-lab/
│
├── mcp_http_server.py # FastMCP HTTP server — tools, resources, prompts
├── mcp_http_client_base.py # Base client — Streamable HTTP transport layer
├── mcp_http_client_app.py # Gradio GUI client — manual tool/resource testing
├── mcp_http_host_app.py # OpenAI GPT-4o-mini AI host application
│
└── workspace/ # Secure sandbox — only accessible directory
├── test.txt # Sample test file
└── README.md # Workspace description______________________________________________________________________
🛠️ 技术栈
| 组件 | 技术 | 版本 |
|---|---|---|
| MCP框架 | FastMCP | 2.12.5 |
| MCP协议 | 模型上下文协议 | 1.16.0 |
| 语言 | Python | 3.11+ |
| HTTP传输 | 流式HTTP_client | httpx 0.28.1 |
| UI框架 | Gradio | 5.49.1 |
| LLM集成 | OpenAI GPT-4o-mini | OpenAI 1.26.1 |
| 服务器终结点 | http://127.0.0.1:8000/mcp | 可流式传输HTTP |
______________________________________________________________________
🔧 MCP服务器——工具、资源和提示
工具(共4个)
| 工具 | 描述 | 根检查 |
|---|---|---|
read_file | 从工作区读取文件 | ✅ 是的 |
write_file | 将内容写入工作区文件 | ✅ 是的 |
list_files | 列出工作区中的文件/目录 | ✅ 是的 |
analyze_code | 触发服务器启动的采样 | ✅ 是的 |
资源
| URI模式 | 描述 |
|---|---|
file://workspace/{filename} | 将任何工作区文件作为资源读取 |
提示
| 提示 | 目的 |
|---|---|
review_code | 为文件生成代码审查提示 |
analyze_security | 为文件生成安全分析提示 |
______________________________________________________________________
🛡️ 根安全实施
所有工具操作均已根据 workspace/ 目录:
def is_within_roots(path: Path) -> bool:
try:
path.resolve().relative_to(BASE_DIR.resolve())
return True
except ValueError:
return False这会阻止什么:
- 路径遍历攻击(
../../etc/passwd) - 访问工作区外的系统文件
- 未经授权读取/写入主机文件系统
______________________________________________________________________
🤖 服务器启动采样
这 analyze_code 该工具演示 采样模式:
Tool Call: analyze_code(code, focus)
│
▼
Server builds sampling/createMessage request
│
▼
Request sent back to client:
{
"method": "sampling/createMessage",
"params": {
"messages": [{"role": "user", "content": "Analyze this code..."}],
"maxTokens": 500
}
}
│
▼
Client shows approval → User approves
│
▼
Client calls LLM → Returns analysis to server主要优势: LLM API密钥永远不会离开客户端! ✅
______________________________________________________________________
🚀 如何跑步
步骤1——安装依赖项:
pip install mcp==1.16.0 fastmcp==2.12.5 httpx==0.28.1 gradio==5.49.1 openai==1.26.1步骤2-设置OpenAI API密钥:
export OPENAI_API_KEY="your-api-key-here"步骤3——启动MCP HTTP服务器(终端1):
python mcp_http_server.py
# Output: Starting HTTP MCP Server on http://127.0.0.1:8000
# Output: Workspace roots: ./workspace步骤4——启动GUI客户端(终端2):
python mcp_http_client_app.py步骤5——启动AI主机应用程序(终端3):
python mcp_http_host_app.py______________________________________________________________________
🌐 HTTP与STDIO传输
| 功能 | STDIO(基本MCP) | HTTP(此项目) |
|---|---|---|
| 连接 | 仅限本地进程 | 远程/云就绪 |
| 客户端 | 单客户端 | 多并发 |
| 服务器生命周期 | 绑定到客户端 | 独立进程 |
| 部署 | 同一台机器 | 任何服务器/云 |
| 端点 | stdin/stdout | http://host:port/mcp |
______________________________________________________________________
🎓 技能展示
- 具有HTTP传输的高级模型上下文协议(MCP)
- 可流式HTTP客户端-服务器通信
- 文件系统根安全边界实现
- 服务器启动的LLM采样模式
- OpenAI GPT-4o-mini函数调用集成
- MCP工具、资源和提示实施
- 合成工具——将MCP资源包装为OpenAI功能
- 用于AI应用程序开发的Gradio UI
- 基础/派生类架构(MCPHTTPClient→ 主机/GUI应用程序)
- 异步Python——异步、异步堆栈、httpx
- 生产就绪的代理人工智能系统设计
______________________________________________________________________
🔗 相关项目
| 项目 | 交通 | 焦点 |
|---|---|---|
| 具有权限的MCP安全 | STDIO | 权限管理与激励 |
| 高级MCP实验室 ← 您在这里 | HTTP | 根安全和采样 |
______________________________________________________________________
📜 认证
| 认证 | 发卡机构 | 平台 |
|---|---|---|
| IBM数据科学专业证书 | IBM | Coursera |
| IBM生成人工智能专业证书 | IBM | Coursera |
| IBM RAG和代理人工智能专业证书 | IBM | Coursera |
______________________________________________________________________
🤝 与我联系
  ](https://github.com/Leelaissakattaota)
