Files
xAiService/Services/XAiMcpToolScanner.cs
T

226 lines
6.6 KiB
C#

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<IReadOnlyList<XAiMcpToolBlueprint>> blueprints;
public XAiMcpToolScanner(IEnumerable<Assembly> assemblies)
{
//
this.assemblies = assemblies?
.Where(a => a != null && !a.IsDynamic)
.Distinct()
.ToArray() ?? [];
//
blueprints = new Lazy<IReadOnlyList<XAiMcpToolBlueprint>>(
Scan,
LazyThreadSafetyMode.ExecutionAndPublication
);
}
public IReadOnlyList<XAiMcpToolBlueprint> Blueprints => blueprints.Value;
//
#region Private ...
private IReadOnlyList<XAiMcpToolBlueprint> Scan()
{
//
var result = new List<XAiMcpToolBlueprint>();
var seenNames = new HashSet<string>(StringComparer.Ordinal);
foreach (var asm in assemblies)
{
//
foreach (var type in SafeGetTypes(asm))
{
//
if (type.GetCustomAttribute<XAiMcpToolTypeAttribute>() == null) {
continue;
}
//
foreach (var method in type.GetMethods(
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.Static))
{
//
var attr = method.GetCustomAttribute<XAiMcpToolAttribute>();
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<DescriptionAttribute>()?.Description
?? string.Empty;
//
result.Add(new XAiMcpToolBlueprint
{
Name = name,
Method = method,
DeclaringType = type,
Description = description,
InputSchema = BuildInputSchema(method)
});
}
}
}
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<XAiMcpToolParamAttribute>();
var jsonName = attr?.Name ?? p.Name;
var description = attr?.Description
?? p.GetCustomAttribute<DescriptionAttribute>()?.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<Type> SafeGetTypes(Assembly asm)
{
//
try
{
return asm.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
return ex.Types.Where(t => t != null)!;
}
}
#endregion
}
}