Add Support for Default Swagger Parameters Values in appSetting and Add Support for MCPServers Connecting and Handling Function Tooling ...

This commit is contained in:
2026-11-05 14:54:05 +03:30
parent c7efdd5525
commit 97acf0a123
13 changed files with 880 additions and 451 deletions
+152 -4
View File
@@ -47,16 +47,22 @@ namespace xAiApi.Providers
private readonly ILogger<XAIServiceBase> logger;
private readonly XAiApiConfiguration configuration;
private readonly XValidationProvider validationProvider;
private readonly IXAiMcpServerProvider mcpServerProvider;
private readonly IXFileContentExtractor fileContentExtractor;
//
private readonly bool useProvidedMcpServers;
protected XAIServiceBase(
IXAiRAGService ragProvider,
IXAiDataProvider dataProvider,
ILogger<XAIServiceBase> logger,
XAiApiConfiguration configuration,
XValidationProvider validationProvider,
IXAiMcpServerProvider mcpServerProvider,
IXFileContentExtractor fileContentExtractor,
string model = null
string model = null,
bool useProvidedMcpServers = true
)
{
//
@@ -64,8 +70,10 @@ namespace xAiApi.Providers
this.ragProvider = ragProvider;
this.dataProvider = dataProvider;
this.configuration = configuration;
this.mcpServerProvider = mcpServerProvider;
this.validationProvider = validationProvider;
this.fileContentExtractor = fileContentExtractor;
this.useProvidedMcpServers = useProvidedMcpServers;
//
// Prepare Embedding Model Descriptor ...
@@ -86,10 +94,12 @@ namespace xAiApi.Providers
/// Base Method for Communicate with LLM ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="userInfo"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<string> AskTextAsync(
string prompt,
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
)
{
@@ -103,6 +113,23 @@ namespace xAiApi.Providers
//
using var client = Descriptor.GetClient();
var history = GetHistory(prompt);
//
// Handle Provided MCP Servers ...
if (useProvidedMcpServers)
{
//
var availableTools = await mcpServerProvider
.GetAvailableToolsAsync(
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
Options.Tools = [.. availableTools .Cast<AITool>()];
}
//
var response = await client.GetResponseAsync(
options: Options,
messages: history,
@@ -131,10 +158,12 @@ namespace xAiApi.Providers
/// Base Method for Communicate with LLM ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="userInfo"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async IAsyncEnumerable<string> AskTextAsEnumerable(
string prompt,
XUserClaimsInfoDto userInfo = null,
[EnumeratorCancellation]
CancellationToken cancellationToken = default
)
@@ -149,6 +178,23 @@ namespace xAiApi.Providers
//
using var client = Descriptor.GetClient();
var history = GetHistory(prompt);
//
// Handle Provided MCP Servers ...
if (useProvidedMcpServers)
{
//
var availableTools = await mcpServerProvider
.GetAvailableToolsAsync(
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
Options.Tools = [.. availableTools .Cast<AITool>()];
}
//
var enumerable = client.GetStreamingResponseAsync(
options: Options,
messages: history
@@ -196,6 +242,23 @@ namespace xAiApi.Providers
//
using var client = Descriptor.GetClient();
var history = GetHistory(prompt, context);
//
// Handle Provided MCP Servers ...
if (useProvidedMcpServers)
{
//
var availableTools = await mcpServerProvider
.GetAvailableToolsAsync(
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
Options.Tools = [.. availableTools.Cast<AITool>()];
}
//
var response = await client.GetResponseAsync(
options: Options,
messages: history,
@@ -245,6 +308,23 @@ namespace xAiApi.Providers
//
using var client = Descriptor.GetClient();
var history = GetHistory(prompt, context);
//
// Handle Provided MCP Servers ...
if (useProvidedMcpServers)
{
//
var availableTools = await mcpServerProvider
.GetAvailableToolsAsync(
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
Options.Tools = [.. availableTools.Cast<AITool>()];
}
//
var enumerable = client.GetStreamingResponseAsync(
options: Options,
messages: history,
@@ -312,6 +392,23 @@ namespace xAiApi.Providers
//
using var client = Descriptor.GetClient();
//
// Handle Provided MCP Servers ...
if (useProvidedMcpServers)
{
//
var availableTools = await mcpServerProvider
.GetAvailableToolsAsync(
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
Options.Tools = [.. availableTools.Cast<AITool>()];
}
//
var response = await client
.GetResponseAsync(
options: Options,
@@ -376,6 +473,23 @@ namespace xAiApi.Providers
//
using var client = Descriptor.GetClient();
//
// Handle Provided MCP Servers ...
if (useProvidedMcpServers)
{
//
var availableTools = await mcpServerProvider
.GetAvailableToolsAsync(
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
Options.Tools = [.. availableTools.Cast<AITool>()];
}
//
var stream = client
.GetStreamingResponseAsync(
options: Options,
@@ -449,6 +563,23 @@ namespace xAiApi.Providers
//
using var client = Descriptor.GetClient();
//
// Handle Provided MCP Servers ...
if (useProvidedMcpServers)
{
//
var availableTools = await mcpServerProvider
.GetAvailableToolsAsync(
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
Options.Tools = [.. availableTools.Cast<AITool>()];
}
//
var response = await client.GetResponseAsync(
options: Options,
messages: prep.History,
@@ -536,6 +667,23 @@ namespace xAiApi.Providers
//
using var client = Descriptor.GetClient();
//
// Handle Provided MCP Servers ...
if (useProvidedMcpServers)
{
//
var availableTools = await mcpServerProvider
.GetAvailableToolsAsync(
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
Options.Tools = [.. availableTools.Cast<AITool>()];
}
//
var stream = client
.GetStreamingResponseAsync(
options: Options,
@@ -992,9 +1140,6 @@ namespace xAiApi.Providers
{
continue;
}
//
// TODO: Implement File Save ...
}
//
@@ -1133,6 +1278,9 @@ namespace xAiApi.Providers
//
var result = new List<ChatMessage>();
//
await Task.FromResult(true);
//
// Retrieve Introduction Prompt ...
var ragPrompt = configuration.GetPrompt(XAiApiConstants.XAiApiRagPromptName);
+171
View File
@@ -13,6 +13,7 @@ using xAiModels.Models.Dtos;
using xIdentityModels.Models;
using System.Threading.Tasks;
using System.Linq.Expressions;
using xAiModels.Providers.Hubs;
using xAiModels.Models.Entities;
using xAiModels.Interfaces.Dtos;
using xIdentityHelper.Extensions;
@@ -20,6 +21,7 @@ using System.Collections.Generic;
using xIdentityService.Interfaces;
using ModelContextProtocol.Client;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.SignalR;
using xAiModels.Interfaces.Entities;
using xAiModels.Interfaces.Enrichers;
@@ -30,6 +32,7 @@ namespace xAiApi.Providers
/// </summary>
public class XAiMcpServerProvider : IXAiMcpServerProvider, IAsyncDisposable
{
private readonly IHubContext<XAiDataProviderHub> hub;
private readonly IXIdentityProvider identityProvider;
private readonly ILogger<XAiMcpServerProvider> logger;
private readonly XValidationProvider validationProvider;
@@ -44,6 +47,7 @@ namespace xAiApi.Providers
private bool isInitialized = false;
public XAiMcpServerProvider(
IHubContext<XAiDataProviderHub> hub,
IXIdentityProvider identityProvider,
ILogger<XAiMcpServerProvider> logger,
XValidationProvider validationProvider,
@@ -53,6 +57,7 @@ namespace xAiApi.Providers
)
{
//
this.hub = hub;
this.logger = logger;
this.identityProvider = identityProvider;
this.mcpServerEnricher = mcpServerEnricher;
@@ -299,6 +304,15 @@ namespace xAiApi.Providers
cancellationToken: cancellationToken
);
//
// Sending Push ...
await SendPush(
connectionId: connectionId,
cancellationToken: cancellationToken,
payLoad: result.ToJSON(camelCase: true),
action: XAiDataProviderHubActions.McpServerCreated.GetStringValue()
);
//
await initLock.WaitAsync(cancellationToken);
try
@@ -315,6 +329,59 @@ namespace xAiApi.Providers
return result;
}
/// <summary>
/// Get an Mcp Server ...
/// </summary>
/// <param name="id"></param>
/// <param name="userInfo"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<XAiMcpServerDto> GetMcpServer(
Guid id,
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
)
{
//
// Validation ...
validationProvider
.GroupValidationBuilder()
.AddIsGuid(id)
.AddNotNull(userInfo)
.AddNotEmpty(userInfo.UserId)
.ValidateGroup();
//
var result = await mcpServerServiceProvider
.GetAsync(
id: id,
includeBuilder: null,
ignoreSoftDeleteds: true,
cancellationToken: cancellationToken
);
//
// Validate Ownership ...
if (!userInfo.IsAdmin() && result.OwnerId != userInfo.UserId)
{
XException.NotAllowed.Throw();
}
//
if (!result.IsNullOrDefault())
{
//
// Handle Enrichment ...
result = await mcpServerEnricher.Enrich(
source: result,
cancellationToken: cancellationToken
);
}
//
return result;
}
/// <summary>
/// Update Specified Mcp Server ...
/// </summary>
@@ -396,6 +463,15 @@ namespace xAiApi.Providers
cancellationToken: cancellationToken
);
//
// Sending Push ...
await SendPush(
connectionId: connectionId,
cancellationToken: cancellationToken,
payLoad: result.ToJSON(camelCase: true),
action: XAiDataProviderHubActions.McpServerUpdated.GetStringValue()
);
//
await initLock.WaitAsync(cancellationToken);
try
@@ -484,6 +560,15 @@ namespace xAiApi.Providers
cancellationToken: cancellationToken
);
//
// Sending Push ...
await SendPush(
connectionId: connectionId,
cancellationToken: cancellationToken,
payLoad: result.ToJSON(camelCase: true),
action: XAiDataProviderHubActions.McpServerRemoved.GetStringValue()
);
//
await initLock.WaitAsync(cancellationToken);
try
@@ -569,6 +654,15 @@ namespace xAiApi.Providers
//
if (result)
{
//
// Sending Push ...
await SendPush(
connectionId: connectionId,
cancellationToken: cancellationToken,
payLoad: exists.ToJSON(camelCase: true),
action: XAiDataProviderHubActions.McpServerUpdated.GetStringValue()
);
//
await initLock.WaitAsync(cancellationToken);
try
@@ -654,6 +748,15 @@ namespace xAiApi.Providers
//
if (result)
{
//
// Sending Push ...
await SendPush(
connectionId: connectionId,
cancellationToken: cancellationToken,
payLoad: exists.ToJSON(camelCase: true),
action: XAiDataProviderHubActions.McpServerUpdated.GetStringValue()
);
//
await initLock.WaitAsync(cancellationToken);
try
@@ -731,6 +834,15 @@ namespace xAiApi.Providers
//
if (result)
{
//
// Sending Push ...
await SendPush(
connectionId: connectionId,
cancellationToken: cancellationToken,
payLoad: exists.ToJSON(camelCase: true),
action: XAiDataProviderHubActions.McpServerUpdated.GetStringValue()
);
//
await initLock.WaitAsync(cancellationToken);
try
@@ -1114,5 +1226,64 @@ namespace xAiApi.Providers
}
}
#endregion
//
#region Hub Actions ...
/// <summary>
/// Send Custom Push Message ...
/// </summary>
/// <param name="action"></param>
/// <param name="payLoad"></param>
/// <param name="connectionId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task SendPush(
string action,
string payLoad,
string connectionId = null,
CancellationToken cancellationToken = default
)
{
//
try
{
//
// Collect Allowed Actions ...
var actions = new List<string>
{
XAiDataProviderHubActions.McpServerCreated.GetStringValue(),
XAiDataProviderHubActions.McpServerUpdated.GetStringValue(),
XAiDataProviderHubActions.McpServerRemoved.GetStringValue(),
};
//
// Validate Args ...
var isValid =
!action.IsNullOrEmpty() &&
actions.Contains(action);
if (!isValid)
{
return;
}
//
// Retrieve Connection Id ...
var clients = hub.Clients.All;
if (!connectionId.IsNullOrEmpty())
{
clients = hub.Clients.AllExcept(connectionId);
}
//
await clients.SendAsync(
action,
payLoad,
connectionId,
cancellationToken
);
}
catch { }
}
#endregion
}
}
+2
View File
@@ -16,6 +16,7 @@ namespace xAiApi.Providers
ILogger<XAiService> logger,
XAiApiConfiguration configuration,
XValidationProvider validationProvider,
IXAiMcpServerProvider mcpServerProvider,
IXFileContentExtractor fileContentExtractor
) : base(
ragProvider,
@@ -23,6 +24,7 @@ namespace xAiApi.Providers
logger,
configuration,
validationProvider,
mcpServerProvider,
fileContentExtractor,
XAiApiConstants.XAiDefaultModelName
)
+2
View File
@@ -17,6 +17,7 @@ namespace xAiApi.Providers
ILogger<XAIServiceBase> logger,
XAiApiConfiguration configuration,
XValidationProvider validationProvider,
IXAiMcpServerProvider mcpServerProvider,
IXFileContentExtractor fileContentExtractor,
ReasoningEffort effort = ReasoningEffort.Medium,
ReasoningOutput output = ReasoningOutput.Full
@@ -26,6 +27,7 @@ namespace xAiApi.Providers
logger,
configuration,
validationProvider,
mcpServerProvider,
fileContentExtractor,
XAiApiConstants.XAiDefaultThinkingModelName
)