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.
- 💾 Project-Local Disk Graph Cache: Caches parsed project graphs to
.unity_context_slicer/graph_cache.jsonfor near-instant (< 15ms) startup across new chats, automatically invalidating when project files change. - 🤖 Automated Agent Rule Provisioning: Automatically generates and maintains
.cursorrules,.windsurfrules,.clinerules, and.gemini/rules.mdin target Unity projects to ensure AI agents proactively use slicer tools in every chat session. - ⚡ 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 project files to keep the graph up to date. - 🔌 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.
- rules.py — Automated rule file generator (.cursorrules, .windsurfrules, .clinerules, .gemini/rules.md).
- 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.
Metadata
Release files for unity-context-slicer 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| unity_context_slicer-0.2.0.tar.gz | 34.4 kB | With provenance | Details for unity_context_slicer-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| unity_context_slicer-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for unity_context_slicer-0.2.0-py3-none-any.whl |
Total release size: 71.4 kB
Release files / unity_context_slicer-0.2.0.tar.gz
| Download URL | unity_context_slicer-0.2.0.tar.gz |
|---|---|
| Size | 34.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c926550bac05c0fabf63da030c4859d23f026199eaf4200e2d15e1bf90806926
|
|
BLAKE2b-256 checksum How to use checksums |
13bcbb2fa3ec683e728b92f38196da0a7a9cda43c2b4157d20f0ff48531057af
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 23, 2026.
Transparency logRelease files / unity_context_slicer-0.2.0-py3-none-any.whl
| Download URL | unity_context_slicer-0.2.0-py3-none-any.whl |
|---|---|
| Size | 37.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
17074d2a59dc43b514fe95547363a8e879a5095bbb36de94250a59ea36c54800
|
|
BLAKE2b-256 checksum How to use checksums |
8b7bb5743e05a8b9e14c42e99f05715137336c4d89c3d10b064c3f37f4f144cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 23, 2026.
Transparency logSecurity for unity-context-slicer 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for unity_context_slicer-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 23, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.