Skip to content

Commit 403713d

Browse files
committed
refactor: reorganize menu item functionality into separate execute and get commands
An MCP resource for retrieval, and a simple command to execute. Because it's a resource, it's easier for the user to see what's in the menu items
1 parent 012ea6b commit 403713d

21 files changed

+151
-300
lines changed

MCPForUnity/Editor/MCPForUnityBridge.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using MCPForUnity.Editor.Helpers;
1515
using MCPForUnity.Editor.Models;
1616
using MCPForUnity.Editor.Tools;
17-
using MCPForUnity.Editor.Tools.MenuItems;
1817
using MCPForUnity.Editor.Tools.Prefabs;
1918

2019
namespace MCPForUnity.Editor

MCPForUnity/Editor/Tools/MenuItems.meta renamed to MCPForUnity/Editor/Resources/MenuItems.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using MCPForUnity.Editor.Helpers;
5+
using Newtonsoft.Json.Linq;
6+
using UnityEditor;
7+
8+
namespace MCPForUnity.Editor.Resources.MenuItems
9+
{
10+
/// <summary>
11+
/// Provides a simple read-only resource that returns Unity menu items.
12+
/// </summary>
13+
[McpForUnityResource("get_menu_items")]
14+
public static class GetMenuItems
15+
{
16+
private static List<string> _cached;
17+
18+
[InitializeOnLoadMethod]
19+
private static void BuildCache() => Refresh();
20+
21+
public static object HandleCommand(JObject @params)
22+
{
23+
bool forceRefresh = @params?["refresh"]?.ToObject<bool>() ?? false;
24+
string search = @params?["search"]?.ToString();
25+
26+
var items = GetMenuItemsInternal(forceRefresh);
27+
28+
if (!string.IsNullOrEmpty(search))
29+
{
30+
items = items
31+
.Where(item => item.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0)
32+
.ToList();
33+
}
34+
35+
string message = $"Retrieved {items.Count} menu items";
36+
return Response.Success(message, items);
37+
}
38+
39+
internal static List<string> GetMenuItemsInternal(bool forceRefresh)
40+
{
41+
if (forceRefresh || _cached == null)
42+
{
43+
Refresh();
44+
}
45+
46+
return (_cached ?? new List<string>()).ToList();
47+
}
48+
49+
private static void Refresh()
50+
{
51+
try
52+
{
53+
var methods = TypeCache.GetMethodsWithAttribute<MenuItem>();
54+
_cached = methods
55+
.SelectMany(m => m
56+
.GetCustomAttributes(typeof(MenuItem), false)
57+
.OfType<MenuItem>()
58+
.Select(attr => attr.menuItem))
59+
.Where(s => !string.IsNullOrEmpty(s))
60+
.Distinct(StringComparer.Ordinal)
61+
.OrderBy(s => s, StringComparer.Ordinal)
62+
.ToList();
63+
}
64+
catch (Exception ex)
65+
{
66+
McpLog.Error($"[GetMenuItems] Failed to scan menu items: {ex}");
67+
_cached ??= new List<string>();
68+
}
69+
}
70+
}
71+
}
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MCPForUnity/Editor/Tools/MenuItems/MenuItemExecutor.cs renamed to MCPForUnity/Editor/Tools/ExecuteMenuItem.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
using System;
22
using System.Collections.Generic;
3+
using MCPForUnity.Editor.Helpers;
34
using Newtonsoft.Json.Linq;
45
using UnityEditor;
5-
using MCPForUnity.Editor.Helpers;
66

7-
namespace MCPForUnity.Editor.Tools.MenuItems
7+
namespace MCPForUnity.Editor.Tools
88
{
9-
/// <summary>
10-
/// Executes Unity Editor menu items by path with safety checks.
11-
/// </summary>
12-
public static class MenuItemExecutor
9+
[McpForUnityTool("execute_menu_item")]
10+
public static class ExecuteMenuItem
1311
{
1412
// Basic blacklist to prevent execution of disruptive menu items.
1513
private static readonly HashSet<string> _menuPathBlacklist = new HashSet<string>(
@@ -19,10 +17,11 @@ public static class MenuItemExecutor
1917
};
2018

2119
/// <summary>
22-
/// Execute a specific menu item. Expects 'menu_path' or 'menuPath' in params.
20+
/// Routes actions: execute, list, exists, refresh
2321
/// </summary>
24-
public static object Execute(JObject @params)
22+
public static object HandleCommand(JObject @params)
2523
{
24+
McpLog.Info("[ExecuteMenuItem] Handling menu item command");
2625
string menuPath = @params["menu_path"]?.ToString() ?? @params["menuPath"]?.ToString();
2726
if (string.IsNullOrWhiteSpace(menuPath))
2827
{

MCPForUnity/Editor/Tools/MenuItems/MenuItemExecutor.cs.meta renamed to MCPForUnity/Editor/Tools/ExecuteMenuItem.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MCPForUnity/Editor/Tools/MenuItems/ManageMenuItem.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

MCPForUnity/Editor/Tools/MenuItems/ManageMenuItem.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

MCPForUnity/Editor/Tools/MenuItems/MenuItemsReader.cs

Lines changed: 0 additions & 95 deletions
This file was deleted.

MCPForUnity/Editor/Tools/MenuItems/MenuItemsReader.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)