Skip to main content
Glama

unity-api-mcp

PyPI 版本 PyPI 下载量 MCP 注册表 GitHub 星标 GitHub 最后提交 每周数据库构建 许可证: MIT Python 3.10+

MCP 服务器,为 AI 代理提供准确的 Unity API 文档。防止幻觉签名、错误命名空间和已弃用 API 的使用。

支持 Unity 6(每个次要流一个数据库)、Unity 2023 和 Unity 2022 LTS。适用于 Claude Code、Cursor、Windsurf 或任何兼容 MCP 的 AI 工具。无需安装 Unity。请参阅支持的版本。新的 Unity 版本每周自动检测并构建。

快速开始

添加到您的 MCP 配置(.mcp.json、mcp.json 或您工具的 MCP 设置),将 UNITY_VERSION 设置为与您的项目匹配:

{
  "mcpServers": {
    "unity-api": {
      "command": "uvx",
      "args": ["unity-api-mcp"],
      "env": {
        "UNITY_VERSION": "6000.3"
      }
    }
  }
}

有效值:Unity 6 流,如 "6000.3",或 "6"、"2023"、"2022"。

首次运行时,服务器会下载正确的数据库(约 20-30 MB)到 ~/.unity-api-mcp/。

Related MCP server: Funplay MCP for Unity

工作原理

  1. 版本检测。 服务器确定要提供哪个 Unity 版本:

优先级

来源

示例

1

UNITY_VERSION 环境变量

"2022"、"6"、"6000.3" 或 "6000.3.8f1"

2

UNITY_PROJECT_PATH

读取 ProjectSettings/ProjectVersion.txt,将 2022.3.62f1 映射到 "2022",将 6000.3.8f1 映射到 "6000.3"

3

默认值

"6"

  1. 数据库下载。 如果该版本的数据库未在本地缓存,则从 GitHub 下载。Unity 6 次要流(6000.0、6000.3、6000.5 等)拥有各自的按流数据库,当流数据库未发布时,回退到通用 6 数据库。缓存的数据库在启动时会与发布版本进行新鲜度检查,因此每周重建会自动更新现有安装。

  2. 提供服务。 所有工具调用都查询特定版本的 SQLite 数据库。每次查询返回时间 <15ms。

每个版本都有自己的数据库,包含该版本的正确签名、弃用警告和成员列表。

工具

工具

用途

示例

search_unity_api

通过关键字查找 API

"Tilemap SetTile"、"async load scene"

get_method_signature

精确签名,包含所有重载

UnityEngine.Physics.Raycast

get_namespace

解析 using 指令

"SceneManager" -> using UnityEngine.SceneManagement;

get_class_reference

完整的类参考卡片

"InputAction" -> 所有方法/字段/属性

get_deprecation_warnings

检查 API 是否已弃用

"WWW" -> 改用 UnityWebRequest

覆盖范围

所有 UnityEngine 和 UnityEditor 模块,以及从 C# 源代码解析的包:Input System、Addressables、uGUI(包括 Unity 6 上的 TextMeshPro)、AI Navigation 和 Netcode。每个 Unity 6 数据库约 42,500 条记录,每个约 500 条弃用警告。

完整版本列表:db-v1 发布页面。CI 在每次构建时重新生成该表。新的 Unity 补丁每周一自动检测并构建。

不涵盖第三方资源(DOTween、VContainer、Newtonsoft.Json)。对于这些,请依赖项目源代码。

基准测试

已测量,非承诺:3 个测试平台上的 25 个研究问题,由 3 个代理配置回答,每个答案根据事先在源代码中验证的真实情况进行评判。完整测试框架位于 docs/benchmark/ 中,可通过一条命令重新运行。

配置

正确

部分正确

错误

幻觉 API

MCP + 定向读取

24/25

1

0

0

熟练(Grep+读取)

20/25

3

2

1

朴素(完全读取)

19/25

3

3

1

一个重现的幻觉很有启发性。当被要求列出 SceneManager.LoadSceneAsync 的重载时,两个非 MCP 代理都发明了不存在的单参数 LoadSceneAsync(string) 和 LoadSceneAsync(int) 重载。针对它们编写的代码无法编译。MCP 代理返回了准确的四个真实重载。

为什么是正确性而不是 token 节省?代理工具现在在代码搜索方面表现良好。Claude Code 从一开始就提供了 Grep 工具,当前模型会先搜索然后读取狭窄的行范围,因此在我们所有的运行中,所有配置的原始 token 使用量相当。但精确的重载、命名空间和弃用信息根本不在您的项目文件中。没有 MCP 的代理只能从使用示例中推断它们,当推断错误时,您将付出构建失败的代价。

  • 3 个测试平台:一个真实的 Unity 6 游戏项目(11 个问题)、纯 Unity API 查找(8 个)以及 Unity Input System 包源代码(2,700 到 4,600 行文件,6 个问题)

  • 3 个配置,相同的模型和轮次限制:MCP 工具 + Grep/读取、仅 Grep/读取、仅读取

  • 真实情况通过在任何运行前阅读源代码进行验证;答案由单独的模型会话根据该真实情况进行评判;token 使用量取自 API 使用字段

  • 自行运行:python docs/benchmark/run.py --project <unity-project-path>(结果来自 2026 年 7 月;代理行为会变化,因此在引用前请重新运行)

CLAUDE.md 片段

将此添加到您项目的 CLAUDE.md(或等效的指令文件)中。这一步很重要。 没有它,AI 拥有工具但不知道何时使用它们。

## 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, uGUI/TextMeshPro, AI Navigation, Netcode
- Does NOT cover: DOTween, VContainer, Newtonsoft.Json (third-party)

设置详情

无需设置 UNITY_VERSION,您可以指向您的 Unity 项目。服务器会自动读取 ProjectSettings/ProjectVersion.txt:

{
  "mcpServers": {
    "unity-api": {
      "command": "uvx",
      "args": ["unity-api-mcp"],
      "env": {
        "UNITY_PROJECT_PATH": "/path/to/your/unity-project"
      }
    }
  }
}

使用 pip 安装:

pip install unity-api-mcp
{
  "mcpServers": {
    "unity-api": {
      "command": "unity-api-mcp",
      "args": [],
      "env": {
        "UNITY_VERSION": "6000.3"
      }
    }
  }
}

变量

用途

示例

UNITY_VERSION

要提供的 Unity 版本

6000.3、6000.3.8f1、6、2023、2022

UNITY_PROJECT_PATH

从项目自动检测版本

F:/Unity Projects/my-project

UNITY_INSTALL_PATH

覆盖 Unity 安装路径(仅用于 ingest)

D:/Unity/6000.3.8f1

如果您想从自己的 Unity 安装构建数据库而不是下载:

# Install with ingest dependencies
pip install unity-api-mcp[ingest]

# Windows
python -m unity_api_mcp.ingest --unity-version 6000.3 --unity-install "D:/Unity/6000.3.8f1" --project "F:/Unity Projects/MyProject"

# macOS
python -m unity_api_mcp.ingest --unity-version 6000.3 --unity-install "/Applications/Unity/Hub/Editor/6000.3.20f1" --project "/path/to/UnityProject"

# Legacy versions
python -m unity_api_mcp.ingest --unity-version 2022 --unity-install "D:/Unity/2022.3.62f1"

数据库默认写入 ~/.unity-api-mcp/unity_docs_{version}.db。

如果 AI 代理正在为您设置:

使用 uvx 将 unity-api-mcp 添加到我的 MCP 配置中,并将 UNITY_VERSION 设置为与我的项目匹配,从 README 追加 CLAUDE.md 片段,并使用 get_namespace("SceneManager") 进行验证。

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。检查 stderr:unity-api-mcp: serving Unity <version> API docs

服务器无法启动

检查 python --version(需要 3.10+)。检查路径:which unity-api-mcp 或 where unity-api-mcp

第三方包返回无结果

DOTween、VContainer、Newtonsoft.Json 未被索引(第三方,非 Unity 包)


另请参阅

unreal-api-mcp:相同概念,适用于 Unreal Engine (C++),每个 UE 版本都有每周自动构建的数据库。

联系方式

需要为您的引擎或框架定制 MCP 服务器?我构建 MCP 工具,减少 token 浪费并防止 AI 辅助游戏开发中的幻觉。如果您为团队的技术栈需要类似工具,请联系我。

fuatcankoseoglu@gmail.com

许可证

MIT

Related MCP Connectors

Related MCP Servers

  • F
    license
    Not graded
    quality
    D
    maintenance
    An intelligent MCP server that enables AI agents to crawl, index, and semantically search official framework documentation using local RAG. It prevents hallucinations by providing precise, up-to-date documentation excerpts directly into the AI's context window.
    1
    -
  • A
    license
    Not graded
    quality
    A
    maintenance
    This MCP server integrates AI assistants with Unity Editor, allowing them to create scenes, generate scripts, simulate input, and automate workflows using 91 built-in tools.
    255
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    An MCP server that provides tools to fetch live, version-accurate documentation, changelogs, examples, and method signatures for npm and PyPI packages, preventing AI coding agents from hallucinating stale APIs.
    11 npm
    ISC
  • A
    license
    Not graded
    quality
    D
    maintenance
    An MCP Server that provides Retrieval-Augmented Generation (RAG) capabilities to surface the latest Unity API documentation (currently v6.x).
    1
    MIT