Skip to content

Custom Tools Development

Ivan Murzak edited this page Nov 13, 2026 · 7 revisions

Custom Tools Development

Any C# method in your project can become an MCP tool with two attributes. The plugin scans loaded assemblies after each compilation and registers what it finds; the agent sees the new tool without any extra setup.

Tools

#nullable enable
using System;
using System.ComponentModel;
using com.IvanMurzak.McpPlugin;
using com.IvanMurzak.ReflectorNet.Utils;
using UnityEditor;
using UnityEngine;

[AiToolType]
public partial class Tool_Sample
{
    [AiTool("sample-rename", Title = "Sample / Rename")]
    [Description("Renames a GameObject in the open scene.")]
    public string Rename
    (
        [Description("Current name of the GameObject.")]
        string name,
        [Description("New name to assign.")]
        string newName
    )
    {
        return MainThread.Instance.Run(() =>
        {
            var go = GameObject.Find(name)
                ?? throw new ArgumentException($"GameObject '{name}' not found.", nameof(name));
            go.name = newName;
            EditorUtility.SetDirty(go);
            return $"Renamed to '{newName}'.";
        });
    }
}

Rules that keep tools reliable:

  1. Main thread. Unity APIs work only on the main thread: wrap them in MainThread.Instance.Run(() => …) or await MainThread.Instance.RunAsync(…). Work that does not touch Unity can stay on the calling thread.
  2. Describe everything. [Description] on the method and every parameter is what the agent reads.
  3. Optional inputs are nullable parameters with defaults: string? filter = null, int maxResults = 10.
  4. Errors are exceptions with a clear message; the agent receives the message.
  5. IDs are kebab-case with a prefix, e.g. myteam-level-generate.
  6. Return a string, void, a serializable object, or a Task<T> for async work. Unity objects can be returned as references such as GameObjectRef (namespace AIGD).

Useful [AiTool] properties: Title, ReadOnlyHint, DestructiveHint, IdempotentHint, OpenWorldHint, Visible (set false to install the tool hidden), Target.

Editor, runtime or both

[AiToolType(Target = ToolTarget.Runtime)] (from com.IvanMurzak.McpPlugin.Common) registers the tools only in the built game, and the server routes their calls to the running game even while the Editor is connected. Editor-only tools go in an Editor assembly (or under #if UNITY_EDITOR). See the runtime API.

Prompts

A prompt is a reusable instruction the user can pick in their agent.

using System.ComponentModel;
using com.IvanMurzak.McpPlugin;
using com.IvanMurzak.McpPlugin.Common.Model;

[AiPromptType]
public static class Prompt_Team
{
    [AiPrompt(Name = "add-event-system", Role = Role.User)]
    [Description("Add a UnityEvent-based event system.")]
    public static string AddEventSystem()
        => "Create an event system with UnityEvents for decoupled communication.";
}

Resources

A resource is read-only data addressed by a URI template.

[AiResourceType]
public class Resource_Levels
{
    [AiResource(
        Route = "level://{name}",
        Name = "Level description",
        MimeType = "application/json",
        ListResources = nameof(ListLevels),
        Description = "Data of one level by name.")]
    public ResponseResourceContent[] Get(string uri, string name) { /* … */ }

    public ResponseListResource[] ListLevels() { /* … */ }
}

ListResources names the method that enumerates the available URIs. Types come from com.IvanMurzak.McpPlugin.Common.Model.

Check it

After Unity recompiles, the tool is in unity-tool-list, in the AI Game Dev window, and in:

unity-mcp-cli tools sample-rename
unity-mcp-cli run-tool sample-rename ./MyGame --input '{"name":"Cube","newName":"Crate"}'

More detail: API Reference. The plugin's own tools under Editor/Scripts/API/Tool are good examples.

Clone this wiki locally