统一api mcp
](https://pypi.org/project/unity-api-mcp/) ](https://pypi.org/project/unity-api-mcp/)  ](https://github.com/Codeturion/unity-api-mcp) ](https://github.com/Codeturion/unity-api-mcp)  
MCP服务器,为AI代理提供准确的Unity API文档。防止产生幻觉的签名、错误的名称空间和不推荐使用API。
支持 Unity 2022 LTS, 2023,以及 团结6 每个版本都有单独的数据库。适用于Claude Code、Cursor、Windsurf或任何兼容MCP的AI工具。无需安装Unity。
快速开始
添加到MCP配置(.mcp.json, mcp.json,或工具的MCP设置),设置 UNITY_VERSION 要匹配您的项目:
{
"mcpServers": {
"unity-api": {
"command": "uvx",
"args": ["unity-api-mcp"],
"env": {
"UNITY_VERSION": "2022"
}
}
}
}有效值: "2022", "2023",或 "6".
首次运行时,服务器会将正确的数据库(约18-24 MB)下载到 ~/.unity-api-mcp/.
运作原理
- 版本检测。 服务器计算出要服务哪个Unity版本:
| 优先级 | 来源 | 示例 |
|---|---|---|
| 1 | UNITY_VERSION 有人是。 "2022", "6",或 "6000.3.8f1" | |
| 2 | UNITY_PROJECT_PATH | 阅读 ProjectSettings/ProjectVersion.txt,地图 2022.3.62f1 到 "2022" |
| 3 | 默认值 | "6" |
- 数据库下载。 如果该版本的数据库未在本地缓存,则从GitHub下载(一次)。
- 发球。 所有工具调用都查询特定版本的SQLite数据库。每个查询在\
using UnityEngine.SceneManagement;|
| get_class_reference |全类参考卡|“InputAction”->所有方法/字段/属性| | get_deprecation_warnings |检查API是否已过时|“WWW”->改用UnityWebRequest|
覆盖
所有UnityEngine和UnityEditor模块,以及从C#源解析的包:输入系统、可寻址文件、uGUI、TextMeshPro、AI导航和Netcode。
| 版本 | 记录 | 弃用 | 模块 | 大小 |
|---|---|---|---|---|
| Unity 2022 LTS | 32000 | 442 | 86个XML+包 | 18 MB |
| Unity 2023 | 31387 | 436 | 92 XML | 18 MB |
| Unity 6 | 42223 | 516 | 139个XML+包 | 24 MB |
是否 不 涵盖第三方资产(DOTween、VContainer、Newtonsoft.Json)。对于这些,请依赖项目来源。
基准测试
在10步研究工作流程中,MCP使用 代币数量减少4倍 比熟练的代理人和 减少11倍 比一个天真的代理人:
这种差距存在于每种问题类型中。MCP在简单的查找和复杂的多部分研究中获胜:
即使在MCP结果被目标文件读取跟踪的现实混合工作流程中,它仍然使用 代币减少54% 与没有MCP的熟练代理人相比:
“没有MCP”的估计假设是完整的文件读取。具有良好工具的熟练代理可能会使用比所示更少的令牌。MCP保证每次通话中都有一个正确、结构化的答案。
按问题细分
Accuracy
| 测试 | 结果 |
|---|---|
| 搜索前1个相关性(12个常见查询) | 100% |
| 命名空间解析(6个关键类) | 100% |
| 关键类覆盖率(17个常见Unity类) | 94%(16/17) |
排名使用BM25和经过调整的列权重(成员名10x,类名5x)以及核心命名空间增强来确保 Object.Instantiate 排名高于利基API,如 InstantiationParameters.Instantiate.
CLAUDE.md片段
将此添加到您的项目 CLAUDE.md (或等效的说明文件)。 这一步很重要。 没有它,人工智能有工具,但不知道什么时候可以使用它们。
## Unity API Lookup (unity-api MCP)
Use the `unity-api` MCP tools to verify Unity API usage instead of guessing. **Do not hallucinate signatures.**
| When | Tool | Example |
|------|------|---------|
| Unsure about a method's parameters or return type | `get_method_signature` | `get_method_signature("UnityEngine.Tilemaps.Tilemap.SetTile")` |
| Need the `using` directive for a type | `get_namespace` | `get_namespace("SceneManager")` |
| Want to see all members on a class | `get_class_reference` | `get_class_reference("InputAction")` |
| Searching for an API by keyword | `search_unity_api` | `search_unity_api("async load scene")` |
| Checking if an API is deprecated | `get_deprecation_warnings` | `get_deprecation_warnings("FindObjectOfType")` |
**Rules:**
- Before writing a Unity API call you haven't used in this conversation, verify the signature with `get_method_signature`
- Before adding a `using` directive, verify with `get_namespace` if unsure
- Covers: all UnityEngine/UnityEditor modules, Input System, Addressables
- Does NOT cover: DOTween, VContainer, Newtonsoft.Json (third-party)安装详细信息
Auto-detect version from project path
而不是设置 UNITY_VERSION,您可以指向您的Unity项目。服务器读取 ProjectSettings/ProjectVersion.txt 自动:
{
"mcpServers": {
"unity-api": {
"command": "uvx",
"args": ["unity-api-mcp"],
"env": {
"UNITY_PROJECT_PATH": "/path/to/your/unity-project"
}
}
}
}Alternative installation methods
使用pip安装:
pip install unity-api-mcp{
"mcpServers": {
"unity-api": {
"command": "unity-api-mcp",
"args": [],
"env": {
"UNITY_VERSION": "2022"
}
}
}
}Environment variables
| 变量 | 目的 | 示例 |
|---|---|---|
UNITY_VERSION | Unity版本服务 | 2022, 2023, 6,或 6000.3.8f1 |
UNITY_PROJECT_PATH | 从项目中自动检测版本 | F:/Unity Projects/my-project |
UNITY_INSTALL_PATH | 覆盖Unity安装路径(用于 ingest 仅) | D:/Unity/6000.3.8f1 |
Building databases locally
如果你想从自己的Unity安装中构建数据库,而不是下载:
# Install with ingest dependencies
pip install unity-api-mcp[ingest]
# Build for a specific version
python -m unity_api_mcp.ingest --unity-version 6 --unity-install "D:/Unity/6000.3.8f1" --project "F:/Unity Projects/MyProject"
python -m unity_api_mcp.ingest --unity-version 2022 --unity-install "D:/Unity/2022.3.62f1"
python -m unity_api_mcp.ingest --unity-version 2023 --unity-install "D:/Unity/2023.1.22f1"数据库被写入 ~/.unity-api-mcp/unity_docs_{version}.db 默认情况下。
AI-Assisted Setup
如果AI代理正在为您设置此项:
添加unity-api-mcp使用以下命令访问我的MCP配置uvx随着UNITY_VERSION设置为与我的项目匹配,从README中附加CLAUDE.md代码段,并用验证get_namespace("SceneManager").
Project structure
unity-api-mcp/
├── src/unity_api_mcp/
│ ├── server.py # MCP server (5 tools)
│ ├── db.py # SQLite + FTS5 database layer
│ ├── version.py # Version detection + DB download
│ ├── xml_parser.py # Parse Unity XML IntelliSense files
│ ├── cs_doc_parser.py # Parse C# doc comments from package source
│ ├── unity_paths.py # Locate Unity install + package dirs
│ └── ingest.py # CLI ingestion pipeline
└── pyproject.toml数据库存储在 ~/.unity-api-mcp/ (首次运行时下载)。
故障排除
| 问题 | 修复 |
|---|---|
| “无法下载Unity X数据库” | 检查互联网连接。或者在本地构建: python -m unity_api_mcp.ingest --unity-version 2022 |
| 正在提供错误的API版本 | 设置 UNITY_VERSION 明确地。检查标准错误: unity-api-mcp: serving Unity API docs |
| 服务器无法启动 | 检查 python --version (需要3.10+)。检查路径: which unity-api-mcp 或 where unity-api-mcp |
| 第三方软件包未返回任何结果 | DOTween、VContainer、Newtonsoft。Jsons没有索引(第三方,不是Unity包) |
______________________________________________________________________
另见
虚幻api mcp --虚幻引擎(C++)也有同样的概念。涵盖UE 5.5、5.6和5.7。
联系
需要为您的引擎或框架定制MCP服务器吗?我构建了MCP工具,可以减少代币浪费,防止人工智能辅助游戏开发的幻觉。如果你想为你的团队的堆栈提供类似的东西,请联系我们。
fuatcankoseoglu@gmail.com
许可证
可以出于任何个人或非商业目的自由使用、分叉、修改和共享。 商业用途需要许可。
