unity-mcp/UnityMcpBridge/Editor/Tools/CommandRegistry.cs

51 lines
2.0 KiB
C#
Raw Normal View History

2025-03-31 03:58:01 +08:00
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using MCPForUnity.Editor.Tools.MenuItems;
Standardize how we define MCP tools (#292) * refactor: migrate command routing to use CommandRegistry lookup instead of switch statement * style: improve code formatting and indentation consistency * refactor: clean up imports and type hints across tool modules * Revert "feat: Implement Asset Store Compliance for Unity MCP Bridge" This reverts commit 2fca7fc3dab4136f0e9b64cff49b62113c828cef. * Revert "feat(asset-store): implement post-installation prompt system for Asset Store compliance" This reverts commit ab25a71bc5bf008e395679dd621f18e505fc3022. * chore: upgrade mcp[cli] dependency from 1.4.1 to 1.15.0 * style: fix formatting and whitespace in Python server files * Remove description, probably a Python versionn change * feat: add type hints and parameter descriptions to Unity MCP tools * docs: improve shader management tool parameter descriptions and types * refactor: add type annotations and improve documentation for script management tools * refactor: improve type annotations and documentation in manage_scene tool * refactor: add type annotations and improve parameter descriptions across MCP tools * feat: add explicit name parameters to all MCP tool decorators * refactor: remove unused Unity connection instance in manage_asset_tools * chore: update type hints in manage_editor function parameters for better clarity * feat: make name and path parameters optional for scene management operations * refactor: remove unused get_unity_connection import from manage_asset.py * chore: rename Operation parameter annotation to Operations for consistency * feat: add logging to MCP clients for tool actions across MCP server components * chore: add FastMCP type hint to register_all_tools parameter * style: reformat docstring in apply_text_edits tool to use multiline string syntax * refactor: update type hints from Dict/List/Tuple/Optional to modern Python syntax * refactor: clean up imports and add type annotations to script editing tools * refactor: update type hints to use | None syntax for optional parameters * Minor fixes * docs: improve tool descriptions with clearer action explanations * refactor: remove legacy update action migration code from manage_script.py * style: replace em dashes with regular hyphens in tool descriptions [skip ci] * refactor: convert manage_script_capabilities docstring to multiline format [skip ci]
2025-09-28 01:53:10 +08:00
using MCPForUnity.Editor.Tools.Prefabs;
2025-03-31 03:58:01 +08:00
Rename namespace and public facing plugin output from "Unity MCP" to "MCP for Unity" (#225) * refactor: rename namespace from UnityMcpBridge to MCPForUnity across all files See thread in #6, we can't use Unity MCP because it violates their trademark. That name makes us look affiliated. We can use MCP for Unity * Change package display name, menu item and menu titles These are front facing so has to change for Unity asset store review * Misc name changes in logs and comments for better consistency * chore: update editor window title from 'MCP Editor' to 'MCP for Unity' * refactor: update branding from UNITY-MCP to MCP-FOR-UNITY across all log messages and warnings * chore: rename Unity MCP to MCP For Unity across all files and bump version to 2.1.2 * docs: update restore script title to clarify Unity MCP naming * Fix usage instructions * chore: update log messages to use MCP For Unity branding instead of UnityMCP * Add a README inside plugin, required for distributing via the asset store * docs: update Unity port description and fix typo in troubleshooting section * Address Rabbit feedback * Update Editor prefs to use new name Prevents overlap with other Unity MCPs, happy to revert if it's too much * refactor: rename server logger and identifier from unity-mcp-server to mcp-for-unity-server * Standardize casing of renamed project to "MCP for Unity", as it is on the asset store * Remove unused folder * refactor: rename Unity MCP to MCP for Unity across codebase * Update dangling references * docs: update product name from UnityMCP to MCP for Unity in README * Update log and comments for new name
2025-08-21 03:59:49 +08:00
namespace MCPForUnity.Editor.Tools
2025-03-31 03:58:01 +08:00
{
/// <summary>
/// Registry for all MCP command handlers (Refactored Version)
/// </summary>
public static class CommandRegistry
{
// Maps command names (matching those called from Python via ctx.bridge.unity_editor.HandlerName)
// to the corresponding static HandleCommand method in the appropriate tool class.
private static readonly Dictionary<string, Func<JObject, object>> _handlers = new()
{
{ "manage_script", ManageScript.HandleCommand },
{ "manage_scene", ManageScene.HandleCommand },
{ "manage_editor", ManageEditor.HandleCommand },
{ "manage_gameobject", ManageGameObject.HandleCommand },
{ "manage_asset", ManageAsset.HandleCommand },
{ "read_console", ReadConsole.HandleCommand },
{ "manage_menu_item", ManageMenuItem.HandleCommand },
{ "manage_shader", ManageShader.HandleCommand},
Standardize how we define MCP tools (#292) * refactor: migrate command routing to use CommandRegistry lookup instead of switch statement * style: improve code formatting and indentation consistency * refactor: clean up imports and type hints across tool modules * Revert "feat: Implement Asset Store Compliance for Unity MCP Bridge" This reverts commit 2fca7fc3dab4136f0e9b64cff49b62113c828cef. * Revert "feat(asset-store): implement post-installation prompt system for Asset Store compliance" This reverts commit ab25a71bc5bf008e395679dd621f18e505fc3022. * chore: upgrade mcp[cli] dependency from 1.4.1 to 1.15.0 * style: fix formatting and whitespace in Python server files * Remove description, probably a Python versionn change * feat: add type hints and parameter descriptions to Unity MCP tools * docs: improve shader management tool parameter descriptions and types * refactor: add type annotations and improve documentation for script management tools * refactor: improve type annotations and documentation in manage_scene tool * refactor: add type annotations and improve parameter descriptions across MCP tools * feat: add explicit name parameters to all MCP tool decorators * refactor: remove unused Unity connection instance in manage_asset_tools * chore: update type hints in manage_editor function parameters for better clarity * feat: make name and path parameters optional for scene management operations * refactor: remove unused get_unity_connection import from manage_asset.py * chore: rename Operation parameter annotation to Operations for consistency * feat: add logging to MCP clients for tool actions across MCP server components * chore: add FastMCP type hint to register_all_tools parameter * style: reformat docstring in apply_text_edits tool to use multiline string syntax * refactor: update type hints from Dict/List/Tuple/Optional to modern Python syntax * refactor: clean up imports and add type annotations to script editing tools * refactor: update type hints to use | None syntax for optional parameters * Minor fixes * docs: improve tool descriptions with clearer action explanations * refactor: remove legacy update action migration code from manage_script.py * style: replace em dashes with regular hyphens in tool descriptions [skip ci] * refactor: convert manage_script_capabilities docstring to multiline format [skip ci]
2025-09-28 01:53:10 +08:00
{ "manage_prefabs", ManagePrefabs.HandleCommand},
2025-03-31 03:58:01 +08:00
};
/// <summary>
/// Gets a command handler by name.
/// </summary>
/// <param name="commandName">Name of the command handler (e.g., "HandleManageAsset").</param>
/// <returns>The command handler function if found, null otherwise.</returns>
public static Func<JObject, object> GetHandler(string commandName)
{
if (!_handlers.TryGetValue(commandName, out var handler))
{
throw new InvalidOperationException(
$"Unknown or unsupported command type: {commandName}");
2025-03-31 03:58:01 +08:00
}
return handler;
}
public static void Add(string commandName, Func<JObject, object> handler)
{
_handlers.Add(commandName, handler);
2025-03-31 03:58:01 +08:00
}
}
}