Graph-based context extraction engine & MCP server for Unity projects
Project description
Unity Context Slicer
Unity Context Slicer is a graph-based context extraction engine and Model Context Protocol (MCP) server designed for Unity projects. It parses C# scripts, Unity scenes (.unity), prefabs (.prefab), and .meta asset GUIDs into an in-memory knowledge graph. By slicing targeted $N$-hop neighborhoods around relevant components and compressing raw YAML/AST structures, it delivers 5–10× token reduction, producing compact context bundles optimized for local and cloud LLMs.
🔑 Key Features
- 🕸️ Graph-Based Project Indexing: Maps C# classes, methods, events, Unity GameObjects, MonoBehaviours, scenes, and prefabs into a unified directed graph.
- ⚡ 5–10× Context Compression: Converts verbose scene YAML and code structures into dense, high-information text representations suited for restricted LLM context windows (e.g., 7B local models).
- 🎯 Focused Slicing: Extracts $N$-hop relational neighborhoods (
unity_slice) or comprehensive class context cards (unity_class) including method callers, attached scene objects, and inheritance hierarchies. - 🔄 Auto-Reloading Resident Session: Monitors file modification times (
mtime) across.cs,.unity, and.prefabfiles to update the resident graph in milliseconds upon code changes. - 🔌 Built-in MCP Server: Exposes stdio-based MCP tools for direct integration with MCP clients such as Cursor, Windsurf, Claude Desktop, VS Code (Cline / Roo Code), and custom AI agents.
- 🆔 Unity GUID Resolution: Resolves
.metafile GUIDs to bridge C# MonoBehaviours with serialized scene/prefab component references.
🛠️ Architecture & Pipeline
graph TD
A["Unity Project Directory"] --> B["C# Roslyn Scanner / YAML Parser"]
A --> C["Meta GUID Resolver"]
B --> D["Loader & In-Memory Graph"]
C --> D
D --> E["Session Manager"]
E --> F["Graph Slicer"]
F --> G["Compressor & Task Bundler"]
G --> H["MCP Server & Prompt Bundles"]
Pipeline Steps
- Parsing & Resolution: C# AST analysis via Roslyn (ScannerCore.cs) combined with Python-native Unity scene/prefab parsing (unity_parser.py) and GUID resolution (meta_resolver.py).
- Graph Construction: Builds a unified node/edge model in ProjectGraph indexed for bidirectional lookup.
- Neighborhood Slicing: SubGraph algorithms isolate relevant subgraphs based on class, method, or GameObject seeds.
- Dense Compression: Renders subgraphs into task-ready prompt context via compressor.py.
🔌 MCP Tools Provided
| Tool Name | Description |
|---|---|
unity_search |
Search for project graph nodes by name substring and optional node type (class, method, scene, prefab, etc.). |
unity_class |
Retrieve structured class context (methods, callers, attached GameObjects/scenes, inheritance). |
unity_slice |
Extract an $N$-hop neighborhood graph surrounding a target seed node. |
unity_bundle |
Generate a full LLM coding prompt context bundle combining task requirements, graph context, and constraints. |
unity_stats |
View graph node and edge count statistics. |
unity_reload |
Force-reload the project graph from disk. |
🤖 Integration Guide for Popular Coding Agents
1. Cursor
Navigate to Cursor Settings $\rightarrow$ Features $\rightarrow$ MCP Servers and click + Add New MCP Server:
- Name:
unity-context-slicer - Type:
command - Command:
python -m unity_context_slicer.mcp_server --project-dir "${workspaceFolder}"
2. Claude Desktop
Add to your claude_desktop_config.json:
{
"mcpServers": {
"unity-context-slicer": {
"command": "python",
"args": [
"-m",
"unity_context_slicer.mcp_server",
"--project-dir",
"C:/Path/To/Your/UnityProject"
]
}
}
}
3. Windsurf (Codeium)
Add to ~/.codeium/windsurf/mcp_config.json:
{
"mcpServers": {
"unity-context-slicer": {
"command": "python",
"args": [
"-m",
"unity_context_slicer.mcp_server",
"--project-dir",
"${workspaceFolder}"
]
}
}
}
4. VS Code Extensions (Cline / Roo Code / Continue.dev)
Add to your extension's MCP server configuration JSON:
{
"mcpServers": {
"unity-context-slicer": {
"command": "python",
"args": [
"-m",
"unity_context_slicer.mcp_server",
"--project-dir",
"${workspaceFolder}"
]
}
}
}
💡 Recommended Agent Rules (.cursorrules / .windsurfrules)
To help AI coding agents use Unity Context Slicer effectively, add this instruction to your project's rule file:
When answering questions or writing C#/Unity code:
1. Use `unity_search` to discover relevant classes, methods, or scene GameObjects.
2. Use `unity_class` to inspect MonoBehaviour callers, attached scene objects, and class inheritance.
3. Use `unity_bundle` before initiating large refactors to receive a compressed graph context bundle.
📁 Repository Structure
- unity_context_slicer/
- mcp_server.py — FastMCP stdio server implementation and tool definitions.
- loader.py — Data primitives (GraphNode, GraphEdge) and graph indexing.
- slicer.py — Neighborhood traversal and class context extraction.
- compressor.py — Dense text formatting and LLM context reduction.
- session.py — Resident GraphSession manager with stale file detection.
- unity_parser.py — Scene and prefab document structure extractor.
- meta_resolver.py — Mapping between
.metaGUIDs and C# source files. - annotations.py — Annotation cache for node purpose metadata.
- task_log.py — Task history parser and context embedder.
- csharp_scanner/ — C# Roslyn scanner project (CSharpScanner.csproj).
🚀 Installation & Quick Start
Installation via Pip / Editable Install
pip install -e .
Running the MCP Server
unity-context-slicer --project-dir "path/to/UnityProject"
Testing with MCP Inspector
npx -y @modelcontextprotocol/inspector unity-context-slicer --project-dir "path/to/UnityProject"
📜 License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unity_context_slicer-0.1.0.tar.gz.
File metadata
- Download URL: unity_context_slicer-0.1.0.tar.gz
- Upload date:
- Size: 32.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa1be1db336725633e5b255b53c063cc1c281f3e6e97ab4ecbf94c8c335915a2
|
|
| MD5 |
582da2747723abf4580daadeb32f2d18
|
|
| BLAKE2b-256 |
af7026a2f4b8e1ff0b8351998c601819685e34d518ee935c4d913ac8c402f402
|
Provenance
The following attestation bundles were made for unity_context_slicer-0.1.0.tar.gz:
Publisher:
publish.yml on guharoyAntik/unity-context-slicer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
unity_context_slicer-0.1.0.tar.gz -
Subject digest:
fa1be1db336725633e5b255b53c063cc1c281f3e6e97ab4ecbf94c8c335915a2 - Sigstore transparency entry: 2212350221
- Sigstore integration time:
-
Permalink:
guharoyAntik/unity-context-slicer@36b11c832728158fb38ba7b68af8be401a628571 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/guharoyAntik
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@36b11c832728158fb38ba7b68af8be401a628571 -
Trigger Event:
release
-
Statement type:
File details
Details for the file unity_context_slicer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: unity_context_slicer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 34.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3736c48c3beeefd304cc0b7384f8763bc521d3b06e598da5429a4edfc3049a00
|
|
| MD5 |
2b05bcb1bc096e6128fcb4a15dc8856e
|
|
| BLAKE2b-256 |
86e0b96310de2e32ca4508c25ab6de5f7679081600a474fbf115d50ecce2fd35
|
Provenance
The following attestation bundles were made for unity_context_slicer-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on guharoyAntik/unity-context-slicer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
unity_context_slicer-0.1.0-py3-none-any.whl -
Subject digest:
3736c48c3beeefd304cc0b7384f8763bc521d3b06e598da5429a4edfc3049a00 - Sigstore transparency entry: 2212350248
- Sigstore integration time:
-
Permalink:
guharoyAntik/unity-context-slicer@36b11c832728158fb38ba7b68af8be401a628571 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/guharoyAntik
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@36b11c832728158fb38ba7b68af8be401a628571 -
Trigger Event:
release
-
Statement type: