Add Support for Default Swagger Parameters Values in appSetting and Add Support for MCPServers Connecting and Handling Function Tooling ...
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user