[Add] Unity MCP

This commit is contained in:
2026-03-29 01:25:56 +07:00
parent cfe04c18a1
commit 1cff234df1
455 changed files with 50327 additions and 0 deletions
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MCPForUnity.Editor.Helpers;
using Newtonsoft.Json.Linq;
using UnityEditor;
namespace MCPForUnity.Editor.Resources.MenuItems
{
/// <summary>
/// Provides a simple read-only resource that returns Unity menu items.
/// </summary>
[McpForUnityResource("get_menu_items")]
public static class GetMenuItems
{
private static List<string> _cached;
[InitializeOnLoadMethod]
private static void BuildCache() => Refresh();
public static object HandleCommand(JObject @params)
{
bool forceRefresh = @params?["refresh"]?.ToObject<bool>() ?? false;
string search = @params?["search"]?.ToString();
var items = GetMenuItemsInternal(forceRefresh);
if (!string.IsNullOrEmpty(search))
{
items = items
.Where(item => item.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0)
.ToList();
}
string message = $"Retrieved {items.Count} menu items";
return new SuccessResponse(message, items);
}
internal static List<string> GetMenuItemsInternal(bool forceRefresh)
{
if (forceRefresh || _cached == null)
{
Refresh();
}
return (_cached ?? new List<string>()).ToList();
}
private static void Refresh()
{
try
{
var methods = TypeCache.GetMethodsWithAttribute<MenuItem>();
_cached = methods
.SelectMany(m => m
.GetCustomAttributes(typeof(MenuItem), false)
.OfType<MenuItem>()
.Select(attr => attr.menuItem))
.Where(s => !string.IsNullOrEmpty(s))
.Distinct(StringComparer.Ordinal)
.OrderBy(s => s, StringComparer.Ordinal)
.ToList();
}
catch (Exception ex)
{
McpLog.Error($"[GetMenuItems] Failed to scan menu items: {ex}");
_cached ??= new List<string>();
}
}
}
}
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 04eeea61eb5c24033a88013845d25f23
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 329908
packageName: MCP for Unity | AI Driven Development
packageVersion: 9.0.3
assetPath: Assets/MCPForUnity/Editor/Resources/MenuItems/GetMenuItems.cs
uploadId: 855486