Critical: the MCP SDK auto-enables DNS-rebinding protection when its host parameter is left at the 127.0.0.1 default, hard-coding a loopback-only Host allow-list. Every remote client (the only deployment this feature targets) was refused with HTTP 421 before auth or the tool ran. Now driven by a new MCP_ALLOWED_HOSTS setting, with '*' as an explicit, logged opt-out. Also bounds query/top_k to match AskRequest (top_k is amplified 4x downstream, so an unbounded value was a resource-exhaustion vector), decodes the Authorization header as latin-1 per the ASGI spec instead of raising a 500 on malformed bytes, and returns WWW-Authenticate on 401 per RFC 7235. Moves the psycopg2 import guard into backend/tests/conftest.py: duplicated across four test modules, it only worked because of alphabetical collection order, and any earlier-sorting package would have reintroduced a live connection attempt against the production database. Registers the mcp module in the authoritative backend architecture doc. 84 backend tests pass. Verified against a live server: allowed remote Host returns a valid initialize result, unknown Host returns 421, missing token returns 401 with WWW-Authenticate. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
AI+合规智能中枢后端
backend 是当前正式使用的 FastAPI 后端目录,入口为 app.main:app。
架构约束入口
- Backend authoritative architecture 文档:
docs/architecture/backend-project-architecture.md - Backend migration RFC:
docs/rfc/backend-api-parsing-embedding-migration-requirements.md - 后续 backend 新增功能和重构默认遵守:
api -> application -> domain ports -> infrastructure backend/app/services/*与backend/app/workflows/*为迁移期 legacy 目录,除迁移或兼容修复外,不应新增业务编排逻辑。
启动
pip install -r backend/requirements.txt
PYTHONPATH=backend uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
也可以直接使用根目录统一脚本:
./dev.sh start api --foreground
主要接口
GET /healthGET /POST /api/v1/documents/uploadGET /api/v1/documents/listGET /api/v1/documents/management-listGET /api/v1/documents/download/{doc_id}POST /api/v1/knowledge/searchPOST /api/v1/knowledge/retrievalPOST /api/v1/agent/askPOST /api/v1/agent/chatGET /api/v1/agent/chat/stream
目录说明
backend/
├── app/
│ ├── api/ # FastAPI 路由与 transport models
│ ├── application/ # 用例编排层
│ ├── domain/ # 核心业务模型与稳定端口
│ ├── infrastructure/ # 外部系统适配器
│ ├── shared/ # composition root 与横切支撑
│ ├── config/ # 配置与日志
│ ├── services/ # legacy façade / 兼容入口
│ ├── workflows/ # legacy workflow 入口
│ └── workers/ # 任务相关代码
├── .env.example
├── requirements.txt
└── main.py
说明
- 路由前缀保持为
/api/v1,以兼容当前前端。 - 当前主业务链路入口是
documents、knowledge、agent。 compliance.py当前仍被挂载,但尚未满足目标架构约束;在迁移前不应继续扩展业务编排。docs.py与rag.py为遗留/非主入口,不应继续扩展。
开发约束
- backend 开发前先阅读
docs/architecture/backend-project-architecture.md。 - 新增业务能力默认落在
application层,由api调用,不要直接写进 route。 - route 不应直接访问 MinIO、Milvus、Parser SDK、LLM SDK 或
ConversationStore。 backend/app/shared/bootstrap.py是当前 composition root;依赖装配优先收口到这里。