add all requirement for XAiMcpServer Implementation ...

This commit is contained in:
2026-11-05 10:43:38 +03:30
parent 2b99958f4c
commit e9a5e24943
8 changed files with 645 additions and 18 deletions
+127
View File
@@ -0,0 +1,127 @@
using System;
using Newtonsoft.Json.Linq;
namespace xAiService.Helpers
{
public static class XAiMcpArgumentHelper
{
/// <summary>
/// Bind Parameters ...
/// </summary>
/// <param name="token"></param>
/// <param name="targetType"></param>
/// <returns></returns>
public static object Bind(
JToken token,
Type targetType
)
{
//
var underlying = Nullable.GetUnderlyingType(targetType) ?? targetType;
//
// Null Handling ...
if (token == null || token.Type == JTokenType.Null || token.Type == JTokenType.Undefined)
{
//
if (!targetType.IsValueType || Nullable.GetUnderlyingType(targetType) != null)
{
return null;
}
//
return Activator.CreateInstance(targetType);
}
//
// Primitive Types ...
//
if (underlying == typeof(string))
{
return token.ToString();
}
//
if (underlying == typeof(int))
{
return token.Value<int>();
}
//
if (underlying == typeof(long))
{
return token.Value<long>();
}
//
if (underlying == typeof(short))
{
return token.Value<short>();
}
//
if (underlying == typeof(float))
{
return token.Value<float>();
}
//
if (underlying == typeof(double))
{
return token.Value<double>();
}
//
if (underlying == typeof(decimal))
{
return token.Value<decimal>();
}
//
if (underlying == typeof(bool))
{
return token.Value<bool>();
}
//
if (underlying == typeof(Guid))
{
return token.Value<Guid>();
}
//
if (underlying == typeof(DateTime))
{
return token.Value<DateTime>();
}
//
// Enum ...
if (underlying.IsEnum)
{
return Enum.Parse(underlying, token.ToString(), ignoreCase: true);
}
//
// Arrays ...
if (underlying.IsArray && token is JArray jarr)
{
//
var elementType = underlying.GetElementType();
var arr = Array.CreateInstance(elementType, jarr.Count);
for (var i = 0; i < jarr.Count; i++)
{
arr.SetValue(Bind(jarr[i], elementType), i);
}
//
return arr;
}
//
// Fallback: JSON Deserialize
return token.ToObject(underlying);
}
}
}
+105
View File
@@ -0,0 +1,105 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;
namespace xAiService.Helpers
{
public static class XAiMcpResultExtractionHelper
{
/// <summary>
/// Extract Specified Object Result of Method Invoking ...
/// </summary>
/// <param name="raw"></param>
/// <param name="returnType"></param>
/// <returns></returns>
public static async Task<string> ExtractAsync(
object raw,
Type returnType
)
{
//
// Task / Task<T>
if (raw is Task task)
{
//
await task.ConfigureAwait(false);
if (returnType.IsGenericType &&
returnType.GetGenericTypeDefinition() == typeof(Task<>))
{
//
var value = returnType.GetProperty("Result").GetValue(task);
return Format(value);
}
//
return string.Empty;
}
//
// ValueTask (non-generic)
if (returnType == typeof(ValueTask))
{
//
await ((ValueTask)raw).ConfigureAwait(false);
return string.Empty;
}
//
// ValueTask<T> ...
if (returnType.IsGenericType &&
returnType.GetGenericTypeDefinition() == typeof(ValueTask<>))
{
//
var asTask = (Task)returnType.GetMethod("AsTask").Invoke(raw, null);
await asTask.ConfigureAwait(false);
var value = asTask.GetType().GetProperty("Result").GetValue(asTask);
//
return Format(value);
}
//
// void / T Syncron ...
return Format(raw);
}
//
#region Private ...
/// <summary>
/// Format Extracted Response ...
/// /// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static string Format(object value)
{
//
if (value == null)
{
return string.Empty;
}
//
if (value is string s)
{
return s;
}
//
if (value is JToken jt)
{
return jt.ToString(Formatting.None);
}
//
if (value.GetType().IsPrimitive || value is decimal)
{
return value.ToString();
}
//
return JToken.FromObject(value).ToString(Formatting.None);
}
#endregion
}
}