add all requirements for implementing XAiMcpClient ...

This commit is contained in:
2026-11-06 19:09:07 +03:30
parent 33dda6bee7
commit c53553e1cf
10 changed files with 309 additions and 1 deletions
+23
View File
@@ -0,0 +1,23 @@
namespace xAiModels.Constants
{
public struct XAiMcpConstants
{
//
public const string XAiMcpProtocol = "2024-11-05";
public const string XAiMcpClientName = "XAiMcpClient";
public const string XAiMcpSessionIdentifier = "Mcp-Session-Id";
public const string XAiMcpProtocolIdentifier = "MCP-Protocol-Version";
//
public const string TextKey = "text";
public const string ContentKey = "content";
public const string ServerInfoNameKey = "name";
public const string ServerInfoKey = "serverInfo";
public const string ServerInfoVersionKey = "version";
public const string ProtocolVersionKey = "protocolVersion";
//
public const string serverInfoNameValue = "xAiApi";
public const string serverInfoVersionValue = "";
}
}
+16
View File
@@ -0,0 +1,16 @@
using xExceptions.Attributes;
namespace xAiModels.Constants
{
public enum XAiMcpMethods
{
[StringValue("initialize")]
Initialize,
[StringValue("tools/list")]
ToolsList,
[StringValue("tools/call")]
ToolsCall,
}
}
+30
View File
@@ -524,5 +524,35 @@ namespace xAiModels.Extensions
value: $"{XAuthorization.TokenIdentifier} {apiKey}"
);
}
/// <summary>
/// Converts To AITools ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static IList<AITool> ToAITools(
this IList<XAiMcpToolDefinition> source
)
{
//
var result = new List<AITool>();
//
foreach (var tool in source)
{
//
var declaration = AIFunctionFactory.CreateDeclaration(
name: tool.Name,
jsonSchema: tool.InputSchema,
description: tool.Description
);
//
result.Add(declaration);
}
//
return result;
}
}
}
+74
View File
@@ -0,0 +1,74 @@
using xCommons.Constants;
using xCommons.Extensions;
using xAiModels.Constants;
using xIdentityModels.Models;
using xCommons.Configurations;
using System.Collections.Generic;
using xIdentityService.Constants;
namespace xAiModels.Helpers
{
/// <summary>
/// a Helper Class for Prepare Headers of MCP Client ...
/// </summary>
public static class XAiMcpHeaderBuilder
{
/// <summary>
/// Build Headers for MCP Client Connection ...
/// </summary>
/// <param name="appConfiguration"></param>
/// <param name="sessionId"></param>
/// <param name="connectionId"></param>
/// <param name="userInfo"></param>
/// <param name="protocolVersion"></param>
/// <returns></returns>
public static Dictionary<string, string> Build(
XAppConfiguration appConfiguration,
string sessionId = null,
string connectionId = null,
XUserClaimsInfoDto userInfo = null,
string protocolVersion = XAiMcpConstants.XAiMcpProtocol
)
{
//
var headers = new Dictionary<string, string>
{
//
["Accept"] = "application/json, text/event-stream",
["Content-Type"] = "application/json",
[XAiMcpConstants.XAiMcpProtocolIdentifier] = protocolVersion,
[XAuthorization.XPoweredBy] = appConfiguration.XPoweredValue ?? "xAiApi"
};
//
// Adding Authorization Headers ...
if (!userInfo.IsNullOrDefault() && !userInfo.AccessToken.IsNullOrEmpty())
{
//
headers[XAuthorization.Header] = $"{XAuthorization.TokenIdentifier} {userInfo.AccessToken}";
headers[XAuthorization.ExpiresAt] = userInfo.ExpiresAt.ToString();
//
if (!userInfo.RefreshToken.IsNullOrEmpty())
{
headers[XAuthorization.RefreshToken] = userInfo.RefreshToken;
}
}
//
// شناسه اتصال (برای Push Notification ها)
if (!connectionId.IsNullOrEmpty()) {
headers[XHeader.ConnectionId.GetStringValue()] = connectionId;
}
//
// Session Id ...
if (!sessionId.IsNullOrEmpty()) {
headers[XAiMcpConstants.XAiMcpSessionIdentifier] = sessionId;
}
//
return headers;
}
}
}
+86
View File
@@ -0,0 +1,86 @@
using System;
using System.IO;
using System.Text;
using System.Threading;
using xCommons.Extensions;
using xHttpService.Constants;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace xAiModels.Helpers
{
public static class XAiMcpSseReader
{
/// <summary>
/// Read an SSE Event ...
/// </summary>
/// <param name="stream"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async IAsyncEnumerable<string> ReadAsync(
Stream stream,
[EnumeratorCancellation]
CancellationToken cancellationToken = default
)
{
//
using var reader = new StreamReader(stream, Encoding.UTF8);
var dataBuffer = new StringBuilder();
while (!reader.EndOfStream)
{
//
var line = await reader.ReadLineAsync();
if (line is null)
{
break;
}
//
if (cancellationToken.IsCancellationRequested)
{
yield break;
}
//
// Empty Line equals End of an Event ...
if (line.IsNullOrEmpty())
{
//
if (dataBuffer.Length > 0)
{
//
yield return dataBuffer.ToString();
dataBuffer.Clear();
}
//
continue;
}
//
if (line.StartsWith(XSSEConstants.DataIdentifier, StringComparison.OrdinalIgnoreCase))
{
//
var data = line
.Substring(XSSEConstants.DataIdentifier.Length)
.TrimStart();
//
if (dataBuffer.Length > 0)
{
dataBuffer.Append('\n');
}
//
dataBuffer.Append(data);
}
}
//
if (dataBuffer.Length > 0)
{
yield return dataBuffer.ToString();
}
}
}
}
+17
View File
@@ -0,0 +1,17 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace xAiModels.Models
{
public class XAiMcpJsonRpcError
{
[JsonPropertyName("code")]
public int Code { get; set; }
[JsonPropertyName("message")]
public string Message { get; set; }
[JsonPropertyName("data")]
public JsonElement? Data { get; set; }
}
}
+21
View File
@@ -0,0 +1,21 @@
using System;
using System.Text.Json.Serialization;
namespace xAiModels.Models
{
public class XAiMcpJsonRpcRequest
{
[JsonPropertyName("jsonrpc")]
public string JsonRpc { get; set; } = "2.0";
[JsonPropertyName("id")]
public string Id { get; set; } = Guid.NewGuid().ToString("N");
[JsonPropertyName("method")]
public string Method { get; set; }
[JsonPropertyName("params")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object Params { get; set; }
}
}
+25
View File
@@ -0,0 +1,25 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace xAiModels.Models
{
public class XAiMcpJsonRpcResponse
{
[JsonPropertyName("jsonrpc")]
public string JsonRpc { get; set; }
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("result")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public JsonElement? Result { get; set; }
[JsonPropertyName("error")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public XAiMcpJsonRpcError Error { get; set; }
[JsonIgnore]
public bool IsSuccess => Error is null;
}
}
+17
View File
@@ -0,0 +1,17 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace xAiModels.Models
{
public class XAiMcpToolDefinition
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("description")]
public string Description { get; set; }
[JsonPropertyName("inputSchema")]
public JsonElement InputSchema { get; set; }
}
}
-1
View File
@@ -25,7 +25,6 @@
<!-- Local Modules -->
<ItemGroup>
<!-- <PackageReference Include="xDashboard.xStringService" Version="1.0.0" /> -->
</ItemGroup>
<!-- Local Dependencies -->