fixed file path issues

main
Justin Barnett 2025-03-18 13:24:17 -04:00
parent 8b67718734
commit 14c566603c
5 changed files with 50 additions and 23 deletions

View File

@ -280,7 +280,7 @@ public class MCPEditorWindow : EditorWindow
Directory.CreateDirectory(Path.GetDirectoryName(configPath)); Directory.CreateDirectory(Path.GetDirectoryName(configPath));
// Get the absolute path to the Python directory // Get the absolute path to the Python directory
string pythonDir = Path.GetFullPath(Path.Combine(Application.dataPath, "MCPServer", "Python")); string pythonDir = Path.GetFullPath(Path.Combine(Application.dataPath, "unity-mcp", "Python"));
UnityEngine.Debug.Log($"Python directory path: {pythonDir}"); UnityEngine.Debug.Log($"Python directory path: {pythonDir}");
// Create configuration object // Create configuration object

8
Python/__pycache__.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 253b8f93cd23400478080cab9d619729
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -14,19 +14,23 @@ logging.basicConfig(
) )
logger = logging.getLogger("UnityMCPServer") logger = logging.getLogger("UnityMCPServer")
# Global connection state
_unity_connection: UnityConnection = None
@asynccontextmanager @asynccontextmanager
async def server_lifespan(server: FastMCP) -> AsyncIterator[Dict[str, Any]]: async def server_lifespan(server: FastMCP) -> AsyncIterator[Dict[str, Any]]:
"""Handle server startup and shutdown.""" """Handle server startup and shutdown."""
global _unity_connection
logger.info("UnityMCP server starting up") logger.info("UnityMCP server starting up")
try: try:
unity = get_unity_connection() _unity_connection = get_unity_connection()
logger.info("Connected to Unity on startup") logger.info("Connected to Unity on startup")
except Exception as e: except Exception as e:
logger.warning(f"Could not connect to Unity on startup: {str(e)}") logger.warning(f"Could not connect to Unity on startup: {str(e)}")
_unity_connection = None
try: try:
yield {} yield {}
finally: finally:
global _unity_connection
if _unity_connection: if _unity_connection:
_unity_connection.disconnect() _unity_connection.disconnect()
_unity_connection = None _unity_connection = None

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9230796797a49a54297a8fa444a1f5bb
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,7 +2,7 @@
A Unity package that enables seamless communication between Unity and Large Language Models (LLMs) like Claude Desktop via the **Model Context Protocol (MCP)**. This server acts as a bridge, allowing Unity to send commands to and receive responses from MCP-compliant tools, empowering developers to automate workflows, manipulate assets, and control the Unity Editor programmatically. A Unity package that enables seamless communication between Unity and Large Language Models (LLMs) like Claude Desktop via the **Model Context Protocol (MCP)**. This server acts as a bridge, allowing Unity to send commands to and receive responses from MCP-compliant tools, empowering developers to automate workflows, manipulate assets, and control the Unity Editor programmatically.
Welcome to the initial release of this open-source project! Whether you're looking to integrate LLMs into your Unity workflow or contribute to an exciting new tool, were thrilled to have you here. Welcome to the initial release of this open-source project! Whether you're looking to integrate LLMs into your Unity workflow or contribute to an exciting new tool, we're thrilled to have you here.
## Overview ## Overview
@ -24,11 +24,12 @@ Getting started is simple! Follow these steps to add the Unity MCP Server to you
1. **Download the Package** 1. **Download the Package**
Add via the Unity package manager using this link Add via the Unity package manager using this link
```text
https://github.com/justinpbarnett/unity-mcp.git
```
2. **Add to Unity** ```text
https://github.com/justinpbarnett/unity-mcp.git
```
2. **Add to Unity**
- Open Unity and navigate to `Window > Package Manager`. - Open Unity and navigate to `Window > Package Manager`.
- Click the `+` button and select `Add package from disk...`. - Click the `+` button and select `Add package from disk...`.
- Locate the downloaded package and select the `package.json` file. - Locate the downloaded package and select the `package.json` file.
@ -37,14 +38,15 @@ Getting started is simple! Follow these steps to add the Unity MCP Server to you
1. **Prerequisites** 1. **Prerequisites**
Ensure you have: Ensure you have:
- **Python** (version 3.7 or higher) installed. Download it from [python.org](https://www.python.org/downloads/). - **Python** (version 3.7 or higher) installed. Download it from [python.org](https://www.python.org/downloads/).
- **`uv`** installed for managing Python dependencies. Install it via: - **`uv`** installed for managing Python dependencies. Install it via:
```bash ```bash
pip install uv pip install uv
``` ```
2. **Set Up the Python Server** 2. **Set Up the Python Server**
- Navigate to the `Python` directory within the package (e.g., `Assets/MCPServer/Python`). - Navigate to the `Python` directory within the package (e.g., `Assets/unity-mcp/Python`).
- Create a virtual environment and install dependencies: - Create a virtual environment and install dependencies:
```bash ```bash
uv venv uv venv
@ -58,11 +60,12 @@ To connect the MCP Server to tools like Claude Desktop or Cursor:
1. **Open the Unity MCP Window** 1. **Open the Unity MCP Window**
In Unity, go to `Window > Unity MCP` to open the editor window. In Unity, go to `Window > Unity MCP` to open the editor window.
2. **Configure Your Tools** 2. **Configure Your Tools**
- In the Unity MCP window, youll see buttons to configure **Claude Desktop** or **Cursor**.
- In the Unity MCP window, you'll see buttons to configure **Claude Desktop** or **Cursor**.
- Click the appropriate button and follow the on-screen instructions to set up the integration. - Click the appropriate button and follow the on-screen instructions to set up the integration.
3. **Verify Server Status** 3. **Verify Server Status**
- Check the server status in the Unity MCP window. It will display: - Check the server status in the Unity MCP window. It will display:
- **Unity Bridge**: Should show "Running" when active. - **Unity Bridge**: Should show "Running" when active.
- **Python Server**: Should show "Connected" (green) when successfully linked. - **Python Server**: Should show "Connected" (green) when successfully linked.
@ -78,10 +81,10 @@ Once configured, you can use the MCP Server to interact with LLMs directly from
create_primitive(primitive_type="Cube", position=[0, 0, 0]) create_primitive(primitive_type="Cube", position=[0, 0, 0])
``` ```
### Changing a Materials Color ### Changing a Material's Color
```python ```python
# Set a materials color to red (RGBA: 1, 0, 0, 1) # Set a material's color to red (RGBA: 1, 0, 0, 1)
set_material_color(material_name="MyMaterial", color=[1, 0, 0, 1]) set_material_color(material_name="MyMaterial", color=[1, 0, 0, 1])
``` ```
@ -98,21 +101,23 @@ Explore more commands in the [HOW_TO_ADD_A_TOOL.md](HOW_TO_ADD_A_TOOL.md) file f
## Contributing ## Contributing
Wed love your help to make the Unity MCP Server even better! Heres how to contribute: We'd love your help to make the Unity MCP Server even better! Here's how to contribute:
1. **Fork the Repository** 1. **Fork the Repository**
Fork [github.com/justinpbarnett/unity-mcp](https://github.com/justinpbarnett/unity-mcp) to your GitHub account. Fork [github.com/justinpbarnett/unity-mcp](https://github.com/justinpbarnett/unity-mcp) to your GitHub account.
2. **Create a Branch** 2. **Create a Branch**
```bash ```bash
git checkout -b feature/your-feature-name git checkout -b feature/your-feature-name
``` ```
3. **Make Changes** 3. **Make Changes**
Implement your feature or fix, following the projects coding standards (see [HOW_TO_ADD_A_TOOL.md](HOW_TO_ADD_A_TOOL.md) for guidance). Implement your feature or fix, following the project's coding standards (see [HOW_TO_ADD_A_TOOL.md](HOW_TO_ADD_A_TOOL.md) for guidance).
4. **Commit and Push** 4. **Commit and Push**
Use clear, descriptive commit messages: Use clear, descriptive commit messages:
```bash ```bash
git commit -m "Add feature: your feature description" git commit -m "Add feature: your feature description"
git push origin feature/your-feature-name git push origin feature/your-feature-name
@ -126,6 +131,7 @@ For more details, check out [CONTRIBUTING.md](CONTRIBUTING.md) (to be created).
## License ## License
This project is licensed under the **MIT License**. Feel free to use, modify, and distribute it as you see fit. See the full license [here](https://github.com/justinpbarnett/unity-mcp/blob/master/LICENSE). This project is licensed under the **MIT License**. Feel free to use, modify, and distribute it as you see fit. See the full license [here](https://github.com/justinpbarnett/unity-mcp/blob/master/LICENSE).
## Troubleshooting ## Troubleshooting
Encountering issues? Here are some common fixes: Encountering issues? Here are some common fixes:
@ -133,13 +139,14 @@ Encountering issues? Here are some common fixes:
- **Unity Bridge Not Running** - **Unity Bridge Not Running**
Ensure the Unity Editor is open and the MCP window is active. Restart Unity if needed. Ensure the Unity Editor is open and the MCP window is active. Restart Unity if needed.
- **Python Server Not Connected** - **Python Server Not Connected**
- Verify the Python server is running (`python server.py` in the `Python` directory). - Verify the Python server is running (`python server.py` in the `Python` directory).
- Check `config.json` (in `Assets/MCPServer`) for correct port settings (default: `unity_port: 6400`, `mcp_port: 6500`). - Check `config.json` (in `Assets/unity-mcp`) for correct port settings (default: `unity_port: 6400`, `mcp_port: 6500`).
- Ensure `uv` and dependencies are installed correctly. - Ensure `uv` and dependencies are installed correctly.
- **Configuration Issues with Claude Desktop or Cursor** - **Configuration Issues with Claude Desktop or Cursor**
Confirm the paths and settings in the configuration dialog match your tools installation. Confirm the paths and settings in the configuration dialog match your tool's installation.
For additional help, check the [issue tracker](https://github.com/justinpbarnett/unity-mcp/issues) or file a new issue. For additional help, check the [issue tracker](https://github.com/justinpbarnett/unity-mcp/issues) or file a new issue.
@ -148,11 +155,11 @@ For additional help, check the [issue tracker](https://github.com/justinpbarnett
Have questions or want to chat about the project? Reach out! Have questions or want to chat about the project? Reach out!
- **X**: [@justinpbarnett](https://x.com/justinpbarnett) - **X**: [@justinpbarnett](https://x.com/justinpbarnett)
- **GitHub**: [justinpbarnett](https://github.com/justinpbarnett) - **GitHub**: [justinpbarnett](https://github.com/justinpbarnett)
- **Discord**: Join our community (link coming soon!). - **Discord**: Join our community (link coming soon!).
## Acknowledgments ## Acknowledgments
A huge thanks to everyone whos supported this projects initial release. Special shoutout to Unity Technologies for inspiring tools that push creative boundaries, and to the open-source community for making projects like this possible. A huge thanks to everyone who's supported this project's initial release. Special shoutout to Unity Technologies for inspiring tools that push creative boundaries, and to the open-source community for making projects like this possible.
Happy coding, and enjoy integrating LLMs with Unity! Happy coding, and enjoy integrating LLMs with Unity!