安全MCP集群

一个加固的容器化环境,用于AI代理通过模型上下文协议(MCP)与本地系统工具交互,提供TLS加密和基于令牌的身份验证。

  • by kummahiih
  • Python
  • 本地部署
  • 3 星标
  • 更新于
在 GitHub 查看 kummahiih/secure-mcp

使用说明

🛡️ 安全MCP集群

一个强化的容器化环境,供AI代理通过模型上下文协议(MCP)与本地系统工具交互。此设置利用sidecar架构在所有内部服务中强制执行TLS加密和基于令牌的身份验证。

🏗️ 系统架构

服务角色

  • 凯迪拉克Sidecar:网关。处理SSL终止(TLS 1.3),并为内部网络提供安全入口点。
  • LangChain服务器:编排者。运行LangGraph/Agent逻辑,并在LLM和本地工具之间进行协调。
  • LiteLLM代理:API网关。为LLM提供者(Ollama、OpenAI等)提供统一的接口,同时管理出口凭据。
  • MCP服务器:执行层。使用的安全Go服务 os.OpenRoot 为以下对象提供受限的文件系统访问 /workspace 音量。

🔌 网络拓扑

集群使用两个不同的Docker网络实施“气隙”式隔离:

🛰️ 服务库存

服务 映像 网络 端口(暴露) 描述
caddy-sidecar caddy:2-alpine ext_net, int_net 8443:8443 SSL终止和外部入口
proxy litellm:main-latest ext_net, int_net 无 Gemini/OpenAI的安全网关
langchain-server Dockerfile.langchain int_net 无 逻辑引擎(代理)
mcp-server Dockerfile.mcp int_net 无 工具提供程序(工作区访问)

内部沟通路径

  1. 用户请求: Host -> https://localhost:8443 -> Caddy
  2. 逻辑处理: Caddy -> https://langchain-server:8000
  3. 工具执行: LangChain -> https://mcp-server:8443/read
  4. 推断: LangChain -> https://proxy:4000/v1/chat/completions

🔒 安全护栏

1.统一信任链

所有服务都装载共享 ./certs 音量。通过设置 SSL_CERT_FILE 环境变量,每个容器(Python、Go和Caddy)都信任内部根CA,允许无缝的内部HTTPS,而无需 InsecureRequestWarning.

2.文件系统监狱

MCP服务器实现了新的 os.OpenRoot 能力。这创造了一个合乎逻辑的“监狱” /workspace即使提示代理执行目录遍历攻击(例如。, ../../etc/passwd),Go运行时将在系统级别阻止请求。

3.双层身份验证

  • 入口认证:由Caddy/FastAPI通过以下方式管理 LANGCHAIN_API_TOKEN.
  • 服务认证:LangChain服务器使用专用的 MCP_API_TOKEN.

4.到处使用https


📂 项目文件

该项目采用模块化微服务架构,将边缘路由、语言模型代理和文件系统工具分离到不同的容器化域中。

.
├──cluster/
|   ├── agent/                  # Python LangChain integration and agent logic
│   | ├── langchain_test.py     # Unit tests for the agent and tools
│   | └── server.py             # FastAPI server exposing the agent endpoints
|   ├── caddy/                  # Edge router and reverse proxy
|   │   ├── Caddyfile           # TLS and reverse proxy configuration
|   │   └── caddy_test.sh       # Validation script for Caddy configuration
|   ├── fileserver/             # Golang MCP (Model Context Protocol) file server
|   │   ├── go.mod              # Go module dependencies
|   │   ├── main.go             # Core MCP server logic and tool handlers
|   │   └── mcp_test.go         # Unit tests for the Go MCP handlers
|   ├── proxy/                  # Local proxy wrappers and routing
|   │   ├── proxy_config.yaml   # Proxy configuration rules
|   │   └── proxy_wrapper.py    # Python wrapper for proxy execution
|   ├── docker-compose.yml      # Orchestrates the Caddy, Agent, and MCP containers
|   ├── Dockerfile.langchain    # Container build steps for the Python agent
|   ├── Dockerfile.mcp          # Container build steps for the Go fileserver
|   ├── Dockerfile.caddy        # Container build steps for the sidecar Caddy
|   ├── Dockerfile.proxy        # Container build steps for the LiteLLM Proxy
|   └── start-cluster.sh        # starts the cluster (used by run.sh)
├── init_build.sh           # Initial environment setup and build script
├── query.sh                # CLI utility for sending test queries to the agent
├── run.sh                  # Operational script (generates certs, manages lifecycle)
├── test.sh                 # Master test suite (runs unit and integration tests)
└── README.md               # Project intruduction

🛠️ 作战命令

初始化并启动

这 run.sh 脚本自动生成证书、令牌轮换和容器编排:

./run.sh
./query.sh local "Please read info.txt from my workspace."
./query.sh remote "Please read info.txt from my workspace."

🛡️ 安全与质量审计

该项目实施了一种多层安全方法,以确保依赖关系和基础设施的安全。我们使用行业标准的开源工具来扫描漏洞和配置错误。

🔍 安全工具集

工具 重点领域 目的
pip审计 Python库 扫描 agent/requirements.txt 对于已知的CVE。
高斯检验 Go模块 分析Go代码以查找可访问的漏洞。
hadolint Dockerfiles Lints Dockerfile.* 安全最佳实践。
三叉戟 基础设施 扫描 docker-compose.yml 以及泄漏图像。

🚀 运行完整测试套件

要运行完整套件(审核Python、Go和Docker配置),请执行:

./test.sh