使用说明
铁砧
从任何API锻造安全的MCP服务器。
人工智能代理正在从聊天转向行动——预订航班、管理基础设施、处理付款。但是将LLM连接到活动API是危险的。如今,大多数MCP工具都是愚蠢的管道,在没有护栏的情况下暴露每个端点。把你的Stripe密钥和错误的提示交给代理人,它会很高兴的 DELETE /customers.
Anvil解决了这个问题。向它提供任何API规范-OpenAPI、Swagger、Postman,甚至原始文档页面-它会生成一个完整的MCP服务器 自动安全分类, 破坏性动作防护装置,以及 人在环政策。输出可与Claude、Cursor、Gemini或任何兼容MCP的客户端开箱即用。
快速开始
# 1. Clone and install
git clone https://github.com/elliotnou/MCPshield.git && cd MCPshield
pip install -r requirements.txt
# 2. Configure
cp .env.example .env
# Add your Anthropic and Gemini keys (see .env.example)
# 3. Forge a server from any API spec
python -m mcp_adapter generate \
--spec examples/petstore.yaml \
-o ./output/petstore-mcp
# 4. Run it
cd output/petstore-mcp
pip install -r requirements.txt
cp .env.example .env # add your upstream API key
python server.py # MCP server on http://127.0.0.1:8000/mcp
运作原理
API Spec (any format) Production MCP Server
│ ▲
▼ │
┌───────────┐ ┌────────┐ ┌─────────┐ ┌──────────┐
│ INGEST │──▸│ MINE │──▸│ SAFETY │──▸│ FORGE │
│ │ │ │ │ │ │ │
│ Parse any │ │ Group │ │ Classify│ │ Claude │
│ spec fmt │ │ into │ │ R/W/D │ │ writes │
│ │ │ tools │ │ + badge │ │ server │
└───────────┘ └────────┘ └─────────┘ └──────────┘
摄取 --解析OpenAPI 3.x、Swagger 2.x、Postman v2.1和原始HTML文档(通过Gemini)。解决 $ref 链,自动发现Swagger UI端点,并处理混乱的现实世界规范。
我的 --按标签和路径前缀将原始端点分组到逻辑工具中。 GET /users + POST /users + GET /users/{id} 成为 list_users, create_user, get_user。当启发式触发时,阅读量大的组会合并到一个搜索工具中。
安全 --将每个工具分类为 READ (安全), WRITE (修改数据),或 DESTRUCTIVE (删除数据)。徽章如 [⚠️ DESTRUCTIVE] 被注入到工具描述中,以便AI代理知道暂停和确认。敏感参数名称(密码、令牌、SSN)会自动编辑。
锻造 --Claude Haiku在一次LLM过程中编写了一个完整的、可运行的异步Python服务器 ast.parse() 如果发现语法错误,则自动修复。回归到基于模板的生成 --no-llm.
部署 (可选)--使用推送到GitHub --deploy。web仪表板还支持使用您自己的GitHub PAT进行一键部署。
命令行界面
# Generate from a local file
python -m mcp_adapter generate --spec path/to/openapi.yaml -o ./output/my-api
# Generate from a URL
python -m mcp_adapter generate --url https://petstore.swagger.io/v2/swagger.json -o ./output/petstore
# Block all destructive tools
python -m mcp_adapter generate --spec api.yaml -o ./output/safe-api --block-destructive
# Only include specific tools
python -m mcp_adapter generate --spec api.yaml -o ./output/scoped --allowlist get_users,create_user
# Template-based generation (no LLM key needed)
python -m mcp_adapter generate --spec api.yaml -o ./output/my-api --no-llm
# Preview tools without generating (dry run)
python -m mcp_adapter inspect --spec api.yaml
python -m mcp_adapter inspect --spec api.yaml --json-output
# Auto-deploy to GitHub
python -m mcp_adapter generate --spec api.yaml -o ./output --deploy --github-org myorg
旗帜
| 标志 | 描述 |
|---|---|
--spec PATH |
本地规范文件(YAML/JSON) |
--url URL |
远程规范URL |
-o, --output DIR |
输出目录(必填) |
--name NAME |
覆盖服务器名称 |
--block-destructive |
删除所有破坏性(删除)工具 |
--max-tools N |
限制工具数量(0=无限制) |
--allowlist A,B,C |
仅包括这些工具 |
--denylist X,Y,Z |
排除这些工具 |
--no-llm |
基于模板的代码生成(不需要API密钥) |
--deploy |
生成后推送到GitHub |
--github-org ORG |
GitHub组织部署 |
-v, --verbose |
调试日志记录 |
Web仪表板
Anvil附带了一个完整的web UI,可以引导您完成7步流程:
摄取→ 发现→ 模式→ 政策→ 生成→ Test → 部署
# Terminal 1 — API backend
uvicorn api_server:app --port 8080 --reload
# Terminal 2 — Next.js frontend
cd frontend && npm install && npm run dev
# Open http://localhost:3000
创建 frontend/.env.local:
NEXT_PUBLIC_API_URL=http://localhost:8080
仪表板支持所有摄入模式(OpenAPI上传、Swagger URL、SDK仓库、原始文档粘贴)、Discover步骤中的交互式工具选择、每个工具的策略配置,以及使用您自己的PAT进行一键式GitHub部署。
生成的输出
每次运行都会生成一个自包含的目录:
output/my-api-mcp/
├── server.py # Async MCP server with @tool decorators
├── test_server.py # Contract tests for every tool
├── main.py # Deployment entry point
├── requirements.txt # httpx, dedalus-mcp
├── pyproject.toml # Package metadata
└── .env.example # Upstream API credentials template
生成工具示例:
@tool(description="Delete a customer [⚠️ DESTRUCTIVE — may permanently delete data]")
async def delete_customer(customer_id: str) -> str:
"""Permanently remove a customer record."""
return await _request("DELETE", f"/customers/{customer_id}")
这 [DESTRUCTIVE] badge告诉、Cursor和其他MCP客户端在执行之前提示用户进行确认。
环境变量
| 变量 | 必需 | 目的 |
|---|---|---|
ANTHROPIC_API_KEY |
是 | Claude Haiku——代码生成与代理推理 |
GEMINI_API_KEY |
是 | Google Gemini——解析非标准文档和SDK源代码 |
GitHub部署使用在部署时输入的个人访问令牌(不存储在 .env).
看 .env.example 有关设置说明。
项目布局
├── mcp_adapter/ # Core forge pipeline
│ ├── cli.py # Click CLI (generate, inspect)
│ ├── ingest.py # Spec parser (OpenAPI / Swagger / Postman)
│ ├── swagger_ingest.py # Prance-based parser + Gemini fallback
│ ├── sdk_ingest.py # GitHub repo / SDK introspection via Gemini
│ ├── mine.py # Endpoint → tool grouping
│ ├── discover.py # Gemini + rule-based tool classification
│ ├── safety.py # READ / WRITE / DESTRUCTIVE engine
│ ├── reasoning.py # Optional AI-enhanced descriptions
│ ├── agentic_codegen.py # Claude-powered code generation
│ ├── codegen.py # Template-based code generation (no LLM)
│ ├── deploy.py # GitHub repo creation + push
│ ├── models.py # Pydantic domain models
│ └── logger.py # Coloured, stage-aware logging
├── api_server.py # FastAPI backend for the web dashboard
├── frontend/ # Next.js 16 + React 19 + Tailwind CSS
├── examples/ # Sample specs (petstore.yaml)
├── test_application/ # Math API for integration testing
├── test_all.py # Integration test suite
├── requirements.txt
├── Dockerfile
└── .env.example
码头工人
docker build -t anvil .
docker run -p 8080:8080 --env-file .env anvil
技术栈
- 克劳德·海库 (Anthropic)——LLM代码生成
- 谷歌双子座2.5 Flash --文档解析、SDK自检、工具分类
- Python 3.11+ --FastAPI、Click、Pydantic、httpx
- Next.js 16 +React 19+顺风CSS v4--网页控制面板
许可证
麻省理工学院
