/* ┌──────────────────────────────────────────────────────────────────┐ │ Author: Ivan Murzak (https://github.com/IvanMurzak) │ │ Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) │ │ Copyright (c) 2025 Ivan Murzak │ │ Licensed under the Apache License, Version 2.0. │ │ See the LICENSE file in the project root for more information. │ └──────────────────────────────────────────────────────────────────┘ */ #nullable enable using System; using System.ComponentModel; using System.Linq; using com.IvanMurzak.McpPlugin; using com.IvanMurzak.ReflectorNet; using com.IvanMurzak.ReflectorNet.Model; using com.IvanMurzak.ReflectorNet.Utils; namespace com.IvanMurzak.Unity.MCP.Editor.API { public partial class Tool_Reflection { public const string ReflectionMethodFindToolId = "reflection-method-find"; [AiTool ( ReflectionMethodFindToolId, Title = "Method C# / Find", ReadOnlyHint = true, IdempotentHint = true, Enabled = false )] [AiSkillDescription("Find C# methods across every loaded assembly by name / type / parameters — " + "including private methods. Returns serialized `MethodData` entries usable as schemas for '" + ReflectionMethodCallToolId + "'.")] [AiSkillBody("Find method in the project using C# Reflection. " + "It looks for all assemblies in the project and finds method by its name, class name and parameters. " + "Even private methods are available. " + "Use '" + ReflectionMethodCallToolId + "' to call the method after finding it.\n\n" + "## Match levels (apply to `typeName`, `MethodName`, `Parameters`)\n\n" + "- `typeNameMatchLevel` / `methodNameMatchLevel` (default 1 — contains ignoring case): " + "`0` ignore filter, `1` contains-ic, `2` contains-cs, `3` starts-with-ic, `4` starts-with-cs, " + "`5` equals-ic, `6` equals-cs.\n" + "- `parametersMatchLevel` (default 0 — ignore filter): `0` ignore, `1` count matches, `2` equals.\n\n" + "## Output\n\n" + "On match, returns a `[Success] Found N method(s):` line followed by a JSON dump of every method's " + "`MethodData`. Pass any single entry to '" + ReflectionMethodCallToolId + "' as its `filter`.")] [Description("Find method in the project using C# Reflection. " + "It looks for all assemblies in the project and finds method by its name, class name and parameters. " + "Even private methods are available. " + "Use '" + ReflectionMethodCallToolId + "' to call the method after finding it.")] public string MethodFind ( MethodRef filter, [Description("Set to true if 'Namespace' is known and full namespace name is specified in the 'filter.Namespace' property. Otherwise, set to false.")] bool knownNamespace = false, [Description("Minimal match level for 'typeName'. " + "0 - ignore 'filter.typeName', " + "1 - contains ignoring case (default value), " + "2 - contains case sensitive, " + "3 - starts with ignoring case, " + "4 - starts with case sensitive, " + "5 - equals ignoring case, " + "6 - equals case sensitive.")] int typeNameMatchLevel = 1, [Description("Minimal match level for 'MethodName'. " + "0 - ignore 'filter.MethodName', " + "1 - contains ignoring case (default value), " + "2 - contains case sensitive, " + "3 - starts with ignoring case, " + "4 - starts with case sensitive, " + "5 - equals ignoring case, " + "6 - equals case sensitive.")] int methodNameMatchLevel = 1, [Description("Minimal match level for 'Parameters'. " + "0 - ignore 'filter.Parameters' (default value), " + "1 - parameters count is the same, " + "2 - equals.")] int parametersMatchLevel = 0 ) { return MainThread.Instance.Run(() => { var methodEnumerable = FindMethods( filter: filter, knownNamespace: knownNamespace, typeNameMatchLevel: typeNameMatchLevel, methodNameMatchLevel: methodNameMatchLevel, parametersMatchLevel: parametersMatchLevel); var methods = methodEnumerable.ToList(); if (methods.Count == 0) return $"[Success] Method not found. With request:\n{filter}"; var reflector = UnityMcpPluginEditor.Instance.Reflector ?? throw new Exception("Reflector is not available."); var methodRefs = methods .Select(method => new MethodData(reflector, method, justRef: false)) .ToList(); return $@"[Success] Found {methods.Count} method(s): ```json {methodRefs.ToJson(reflector)} ```"; }); } } }