diff --git a/Constants/XAiMcpConstants.cs b/Constants/XAiMcpConstants.cs new file mode 100644 index 0000000..b74708a --- /dev/null +++ b/Constants/XAiMcpConstants.cs @@ -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 = ""; + } +} \ No newline at end of file diff --git a/Constants/XAiMcpMethods.cs b/Constants/XAiMcpMethods.cs new file mode 100644 index 0000000..9f627cf --- /dev/null +++ b/Constants/XAiMcpMethods.cs @@ -0,0 +1,16 @@ +using xExceptions.Attributes; + +namespace xAiModels.Constants +{ + public enum XAiMcpMethods + { + [StringValue("initialize")] + Initialize, + + [StringValue("tools/list")] + ToolsList, + + [StringValue("tools/call")] + ToolsCall, + } +} \ No newline at end of file diff --git a/Extensions/XAiModelsExtensions.cs b/Extensions/XAiModelsExtensions.cs index 89571ce..83aba46 100644 --- a/Extensions/XAiModelsExtensions.cs +++ b/Extensions/XAiModelsExtensions.cs @@ -524,5 +524,35 @@ namespace xAiModels.Extensions value: $"{XAuthorization.TokenIdentifier} {apiKey}" ); } + + /// + /// Converts To AITools ... + /// + /// + /// + public static IList ToAITools( + this IList source + ) + { + // + var result = new List(); + + // + foreach (var tool in source) + { + // + var declaration = AIFunctionFactory.CreateDeclaration( + name: tool.Name, + jsonSchema: tool.InputSchema, + description: tool.Description + ); + + // + result.Add(declaration); + } + + // + return result; + } } } \ No newline at end of file diff --git a/Helpers/XAiMcpHeaderBuilder.cs b/Helpers/XAiMcpHeaderBuilder.cs new file mode 100644 index 0000000..18b55c9 --- /dev/null +++ b/Helpers/XAiMcpHeaderBuilder.cs @@ -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 +{ + /// + /// a Helper Class for Prepare Headers of MCP Client ... + /// + public static class XAiMcpHeaderBuilder + { + /// + /// Build Headers for MCP Client Connection ... + /// + /// + /// + /// + /// + /// + /// + public static Dictionary Build( + XAppConfiguration appConfiguration, + string sessionId = null, + string connectionId = null, + XUserClaimsInfoDto userInfo = null, + string protocolVersion = XAiMcpConstants.XAiMcpProtocol + ) + { + // + var headers = new Dictionary + { + // + ["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; + } + } +} \ No newline at end of file diff --git a/Helpers/XAiMcpSseReader.cs b/Helpers/XAiMcpSseReader.cs new file mode 100644 index 0000000..36d976b --- /dev/null +++ b/Helpers/XAiMcpSseReader.cs @@ -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 + { + /// + /// Read an SSE Event ... + /// + /// + /// + /// + public static async IAsyncEnumerable 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(); + } + } + } +} \ No newline at end of file diff --git a/Models/XAiMcpJsonRpcError.cs b/Models/XAiMcpJsonRpcError.cs new file mode 100644 index 0000000..586b79e --- /dev/null +++ b/Models/XAiMcpJsonRpcError.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Models/XAiMcpJsonRpcRequest.cs b/Models/XAiMcpJsonRpcRequest.cs new file mode 100644 index 0000000..5beefec --- /dev/null +++ b/Models/XAiMcpJsonRpcRequest.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Models/XAiMcpJsonRpcResponse.cs b/Models/XAiMcpJsonRpcResponse.cs new file mode 100644 index 0000000..64d6643 --- /dev/null +++ b/Models/XAiMcpJsonRpcResponse.cs @@ -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; + } +} \ No newline at end of file diff --git a/Models/XAiMcpToolDefinition.cs b/Models/XAiMcpToolDefinition.cs new file mode 100644 index 0000000..e97f662 --- /dev/null +++ b/Models/XAiMcpToolDefinition.cs @@ -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; } + } +} \ No newline at end of file diff --git a/xAiModels.csproj b/xAiModels.csproj index a6b8550..60102a1 100644 --- a/xAiModels.csproj +++ b/xAiModels.csproj @@ -25,7 +25,6 @@ -