-
-
Notifications
You must be signed in to change notification settings - Fork 292
Custom Tools Development
Ivan Murzak edited this page Dec 29, 2025
·
4 revisions
One of the most powerful features of Unity-MCP is the ability to extend it with your own tools. You can expose your own game logic, utility functions, or editor scripts to the AI.
To create a tool, simply add the [McpPluginTool] attribute to any C# method in your project. It works with static methods or instance methods (if the class is registered, but static is easiest for utilities).
using com.IvanMurzak.McpPlugin.Common; // Attributes namespace
using UnityEngine;
using System.ComponentModel; // For Description attribute
// 1. Mark the class
[McpPluginToolType]
public class MyGameTools
{
// 2. Mark the method
[McpPluginTool("spawn-enemy", Title = "Spawn Enemy")]
[Description("Spawns an enemy of a specific type at a position.")]
public static string SpawnEnemy(
[Description("The type of enemy (e.g., 'Orc', 'Goblin')")] string type,
[Description("X,Y,Z position")] Vector3 position
)
{
// 3. Run on Main Thread if touching Unity API
return MainThread.Instance.Run(() =>
{
var prefab = Resources.Load<GameObject>($"Enemies/{type}");
if (!prefab) return $"Error: Enemy type '{type}' not found.";
var instance = Object.Instantiate(prefab, position, Quaternion.identity);
return $"Success: Spawned {instance.name} at {position}";
});
}
}-
Discovery: When the Unity Plugin starts (or recompiles), it scans for
[McpPluginToolType]classes. - Registration: Valid methods are registered as MCP Tools.
-
Usage: The AI Client will instantly see
spawn-enemyin its tool list. - Execution: When the AI uses the tool, your method is called.
The method should return a string (or Task<string>). The returned string is the "output" the AI reads.
- Return "Success: ..." or JSON data for success.
- Return "Error: ..." if something went wrong.
Supported parameter types:
-
int,float,bool,string -
Vector2,Vector3,Quaternion Color- Enum types
Use [Description] on parameters to help the AI understand what to pass.