using System; using System.Linq; using System.Threading; using System.Reflection; using Newtonsoft.Json.Linq; using xAiModels.Attributes; using System.ComponentModel; using System.Collections.Generic; namespace xAiService.Services { public class XAiMcpToolScanner { private readonly Assembly[] assemblies; private readonly Lazy> blueprints; public XAiMcpToolScanner(IEnumerable assemblies) { // this.assemblies = assemblies? .Where(a => a != null && !a.IsDynamic) .Distinct() .ToArray() ?? []; // blueprints = new Lazy>( Scan, LazyThreadSafetyMode.ExecutionAndPublication ); } public IReadOnlyList Blueprints => blueprints.Value; // #region Private ... private IReadOnlyList Scan() { // var result = new List(); var seenNames = new HashSet(StringComparer.Ordinal); foreach (var asm in assemblies) { // foreach (var type in SafeGetTypes(asm)) { // if (type.GetCustomAttribute() == null) { continue; } // foreach (var method in type.GetMethods( BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) { // var attr = method.GetCustomAttribute(); if (attr == null) { continue; } // var name = string.IsNullOrWhiteSpace(attr.Name) ? method.Name : attr.Name.Trim(); if (!seenNames.Add(name)) { // throw new InvalidOperationException( $"Duplicate MCP tool name '{name}' detected in {type.FullName}.{method.Name}."); } // var description = attr.Description ?? method.GetCustomAttribute()?.Description ?? string.Empty; // var requiredPolicy = attr.RequiredPolicy ?? string.Empty; // var requiredAuthenticated = attr.RequiresAuthentication; // result.Add(new XAiMcpToolBlueprint { Name = name, Method = method, DeclaringType = type, Description = description, RequiredPolicy = requiredPolicy, InputSchema = BuildInputSchema(method), RequiresAuthentication = requiredAuthenticated }); } } } // return result; } private static JObject BuildInputSchema(MethodInfo method) { // var properties = new JObject(); var required = new JArray(); // foreach (var p in method.GetParameters()) { // // CancellationToken doesnt exists in Schema ... if (p.ParameterType == typeof(CancellationToken)) { continue; } // var attr = p.GetCustomAttribute(); var jsonName = attr?.Name ?? p.Name; var description = attr?.Description ?? p.GetCustomAttribute()?.Description ?? string.Empty; // var propSchema = new JObject { ["type"] = MapJsonType(p.ParameterType) }; // if (!string.IsNullOrWhiteSpace(description)) { propSchema["description"] = description; } // // Define Items Array ... var underlying = Nullable.GetUnderlyingType(p.ParameterType) ?? p.ParameterType; if (underlying.IsArray) { // propSchema["items"] = new JObject { ["type"] = MapJsonType(underlying.GetElementType()) }; } // properties[jsonName] = propSchema; // Check is Required ... var isRequired = attr?.Required ?? true; if (isRequired) { required.Add(jsonName); } } // var result = new JObject { ["type"] = "object", ["required"] = required, ["properties"] = properties, }; // return result; } private static string MapJsonType(Type type) { // var t = Nullable.GetUnderlyingType(type) ?? type; // if (t == typeof(string) || t == typeof(char) || t == typeof(Guid) || t == typeof(DateTime)) { return "string"; } // if (t == typeof(int) || t == typeof(long) || t == typeof(short) || t == typeof(byte)) { return "integer"; } // if (t == typeof(float) || t == typeof(double) || t == typeof(decimal)) { return "number"; } // if (t == typeof(bool)) { return "boolean"; } // if (t.IsArray || typeof(System.Collections.IEnumerable).IsAssignableFrom(t)) { return "array"; } // if (t.IsEnum) { return "string"; } // return "object"; } private static IEnumerable SafeGetTypes(Assembly asm) { // try { return asm.GetTypes(); } catch (ReflectionTypeLoadException ex) { return ex.Types.Where(t => t != null)!; } } #endregion } }