Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
937e30dd9d |
@@ -28,7 +28,6 @@ namespace xAiApi.Controllers
|
|||||||
/// Flow: Ingest Files -> SessionId -> Ask / AskStream -> Delete Session
|
/// Flow: Ingest Files -> SessionId -> Ask / AskStream -> Delete Session
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[Route("[controller]")]
|
|
||||||
public class ChatWithDocumentsController : XBaseIdentityApiV1Controller
|
public class ChatWithDocumentsController : XBaseIdentityApiV1Controller
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ namespace xAiApi.Controllers
|
|||||||
/// Flow: Ingest Files -> Ask / AskStream (Retrieve from DB -> Generate) -> Documents / Delete
|
/// Flow: Ingest Files -> Ask / AskStream (Retrieve from DB -> Generate) -> Documents / Delete
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[Route("[controller]")]
|
|
||||||
public class KnowledgeController : XBaseIdentityApiV1Controller
|
public class KnowledgeController : XBaseIdentityApiV1Controller
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -0,0 +1,433 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using xModels.Dtos;
|
||||||
|
using xModels.Base;
|
||||||
|
using xIdentityHelper;
|
||||||
|
using System.Threading;
|
||||||
|
using xCommons.Providers;
|
||||||
|
using xAiModels.Models.Dtos;
|
||||||
|
using xAiModels.Controllers;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xCommons.Configurations;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using xAiModels.Interfaces.Dtos;
|
||||||
|
using xAiModels.Models.Entities;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.EntityFrameworkCore.Query;
|
||||||
|
|
||||||
|
namespace xAiApi.Controllers
|
||||||
|
{
|
||||||
|
[Authorize]
|
||||||
|
public class McpServerController : XAiMcpServerControllerBase
|
||||||
|
{
|
||||||
|
public McpServerController(
|
||||||
|
ILogger<McpServerController> logger,
|
||||||
|
XAppConfiguration appConfiguration,
|
||||||
|
XValidationProvider validationProvider,
|
||||||
|
IXAiMcpServerRepositoryService repositoryService,
|
||||||
|
Func<IQueryable<XAiMcpServer>, IOrderedQueryable<XAiMcpServer>> defaultOrderBuilder = null,
|
||||||
|
Func<IQueryable<XAiMcpServer>, IIncludableQueryable<XAiMcpServer, object>> defaultIncludeBuilder = null
|
||||||
|
) : base(
|
||||||
|
logger,
|
||||||
|
appConfiguration,
|
||||||
|
validationProvider,
|
||||||
|
repositoryService,
|
||||||
|
defaultOrderBuilder,
|
||||||
|
defaultIncludeBuilder
|
||||||
|
)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Actions ...
|
||||||
|
//
|
||||||
|
#region Add ...
|
||||||
|
/// <summary>
|
||||||
|
/// Add Specified Item ...
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
|
public override async Task<ActionResult<XAiMcpServerDto>> Add(
|
||||||
|
[FromBody] XAiMcpServerDto item,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await base.Add(
|
||||||
|
item: item,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add or Update Specified Item ...
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost("AddOrUpdate")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
|
public override async Task<ActionResult<XAiMcpServerDto>> AddOrUpdate(
|
||||||
|
[FromBody] XAiMcpServerDto item,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await base.AddOrUpdate(
|
||||||
|
item: item,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add a Collection of Items ...
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost("AddMany")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
|
public override async Task<ActionResult> AddMany(
|
||||||
|
[FromBody] XBaseRangeRequest<XAiMcpServerDto> request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await base.AddMany(
|
||||||
|
request: request,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Retrieve ...
|
||||||
|
/// <summary>
|
||||||
|
/// Get Specified Item ...
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="ignoreSoftDeleteds"></param>
|
||||||
|
/// <param name="useDefaultIncludes"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
|
public override async Task<ActionResult<XAiMcpServerDto>> Get(
|
||||||
|
[FromRoute] Guid id,
|
||||||
|
[FromQuery] bool ignoreSoftDeleteds = true,
|
||||||
|
[FromQuery] bool useDefaultIncludes = false,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await base.Get(
|
||||||
|
id: id,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
ignoreSoftDeleteds: ignoreSoftDeleteds,
|
||||||
|
useDefaultIncludes: useDefaultIncludes
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get All Items ...
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="ignoreSoftDeleteds"></param>
|
||||||
|
/// <param name="useDefaultOrders"></param>
|
||||||
|
/// <param name="useDefaultIncludes"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
|
public override async Task<ActionResult<IEnumerable<XAiMcpServerDto>>> GetAll(
|
||||||
|
[FromQuery] bool ignoreSoftDeleteds = true,
|
||||||
|
[FromQuery] bool useDefaultOrders = false,
|
||||||
|
[FromQuery] bool useDefaultIncludes = false,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await base.GetAll(
|
||||||
|
useDefaultOrders: useDefaultOrders,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
ignoreSoftDeleteds: ignoreSoftDeleteds,
|
||||||
|
useDefaultIncludes: useDefaultIncludes
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Find Specified Item ...
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="query"></param>
|
||||||
|
/// <param name="ignoreSoftDeleteds"></param>
|
||||||
|
/// <param name="useDefaultIncludes"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("FindOne/{query}")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
|
public override async Task<ActionResult<XAiMcpServerDto>> FindOne(
|
||||||
|
[FromRoute] string query,
|
||||||
|
[FromQuery] bool ignoreSoftDeleteds = true,
|
||||||
|
[FromQuery] bool useDefaultIncludes = false,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await base.FindOne(
|
||||||
|
query: query,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
ignoreSoftDeleteds: ignoreSoftDeleteds,
|
||||||
|
useDefaultIncludes: useDefaultIncludes
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Find Many Items ...
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="query"></param>
|
||||||
|
/// <param name="ignoreSoftDeleteds"></param>
|
||||||
|
/// <param name="useDefaultOrders"></param>
|
||||||
|
/// <param name="useDefaultIncludes"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("FindMany/{query}")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
|
public override async Task<ActionResult<IEnumerable<XAiMcpServerDto>>> FindMany(
|
||||||
|
[FromRoute] string query,
|
||||||
|
[FromQuery] bool ignoreSoftDeleteds = true,
|
||||||
|
[FromQuery] bool useDefaultOrders = false,
|
||||||
|
[FromQuery] bool useDefaultIncludes = false,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await base.FindMany(
|
||||||
|
query: query,
|
||||||
|
useDefaultOrders: useDefaultOrders,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
ignoreSoftDeleteds: ignoreSoftDeleteds,
|
||||||
|
useDefaultIncludes: useDefaultIncludes
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve Items Based on Query Mechanism ...
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="query"></param>
|
||||||
|
/// <param name="ignoreSoftDeleteds"></param>
|
||||||
|
/// <param name="useDefaultOrders"></param>
|
||||||
|
/// <param name="useDefaultIncludes"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("Query")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
|
public override async Task<ActionResult<XQueryResult<XAiMcpServerDto>>> Query(
|
||||||
|
[FromQuery] XQuery query,
|
||||||
|
[FromQuery] bool ignoreSoftDeleteds = true,
|
||||||
|
[FromQuery] bool useDefaultOrders = false,
|
||||||
|
[FromQuery] bool useDefaultIncludes = false,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await base.Query(
|
||||||
|
query: query,
|
||||||
|
useDefaultOrders: useDefaultOrders,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
ignoreSoftDeleteds: ignoreSoftDeleteds,
|
||||||
|
useDefaultIncludes: useDefaultIncludes
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Update ...
|
||||||
|
/// <summary>
|
||||||
|
/// Update Item ...
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
|
public override async Task<ActionResult<XAiMcpServerDto>> Update(
|
||||||
|
[FromRoute] Guid id,
|
||||||
|
[FromBody] XAiMcpServerDto item,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await base.Update(
|
||||||
|
id: id,
|
||||||
|
item: item,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update a Collection of items ...
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost("UpdateMany")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
|
public override async Task<ActionResult<bool>> UpdateMany(
|
||||||
|
[FromBody] XBaseRangeRequest<XAiMcpServerDto> request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await base.UpdateMany(
|
||||||
|
request: request,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Exists ...
|
||||||
|
/// <summary>
|
||||||
|
///Check an Item Exists or not ...
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="ignoreSoftDeleteds"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("{id}/IsExists")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
|
public override async Task<ActionResult<bool>> IsExists(
|
||||||
|
[FromRoute] Guid id,
|
||||||
|
[FromQuery] bool ignoreSoftDeleteds = true,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await base.IsExists(
|
||||||
|
id: id,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
ignoreSoftDeleteds: ignoreSoftDeleteds
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Remove ...
|
||||||
|
/// <summary>
|
||||||
|
/// Remove an Items ...
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="softDelete"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
|
public override async Task<ActionResult<XAiMcpServerDto>> Remove(
|
||||||
|
[FromRoute] Guid id,
|
||||||
|
[FromQuery] bool softDelete = false,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await base.Remove(
|
||||||
|
id: id,
|
||||||
|
softDelete: softDelete,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove Many Items ...
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="softDelete"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost("RemoveMany")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
|
public override async Task<ActionResult> RemoveMany(
|
||||||
|
[FromBody] XBaseRangeRequest<XAiMcpServerDto> request,
|
||||||
|
[FromQuery] bool softDelete = false,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = await base.RemoveMany(
|
||||||
|
request: request,
|
||||||
|
softDelete: softDelete,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
using xAiApi.Interfaces;
|
using xAiApi.Interfaces;
|
||||||
using xCommons.Providers;
|
using xCommons.Providers;
|
||||||
using xCommons.Configurations;
|
using xCommons.Configurations;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using xIdentityService.Interfaces;
|
using xIdentityService.Interfaces;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
@@ -9,7 +8,6 @@ using Microsoft.AspNetCore.Authorization;
|
|||||||
namespace xAiApi.Controllers
|
namespace xAiApi.Controllers
|
||||||
{
|
{
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[Route("[controller]")]
|
|
||||||
public class XAiController : XAiServiceControllerBase<IXAiService>
|
public class XAiController : XAiServiceControllerBase<IXAiService>
|
||||||
{
|
{
|
||||||
public XAiController(
|
public XAiController(
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ using Microsoft.AspNetCore.Authorization;
|
|||||||
namespace xAiApi.Controllers
|
namespace xAiApi.Controllers
|
||||||
{
|
{
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[Route("[controller]")]
|
|
||||||
public abstract class XAiServiceControllerBase<TAiService> : XBaseIdentityApiV1Controller
|
public abstract class XAiServiceControllerBase<TAiService> : XBaseIdentityApiV1Controller
|
||||||
where TAiService : IXAiServiceBase
|
where TAiService : IXAiServiceBase
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using xDataService.Db;
|
||||||
using xAiApi.Constants;
|
using xAiApi.Constants;
|
||||||
using xAiModels.Providers;
|
using xAiModels.Providers;
|
||||||
using xDataService.Configuration;
|
using xTagService.Providers;
|
||||||
using xDataService.Db;
|
|
||||||
using xDataService.Providers;
|
using xDataService.Providers;
|
||||||
using xFileService.Providers;
|
using xFileService.Providers;
|
||||||
using xStringService.Providers;
|
using xStringService.Providers;
|
||||||
using xTagService.Providers;
|
using xDataService.Configuration;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace xAiApi.Database
|
namespace xAiApi.Database
|
||||||
{
|
{
|
||||||
@@ -41,6 +41,7 @@ namespace xAiApi.Database
|
|||||||
nameof(XAiVectorEntityRegisterar),
|
nameof(XAiVectorEntityRegisterar),
|
||||||
nameof(XAiMessageEntityRegisterar),
|
nameof(XAiMessageEntityRegisterar),
|
||||||
nameof(XAiProjectEntityRegisterar),
|
nameof(XAiProjectEntityRegisterar),
|
||||||
|
nameof(XAiMcpServerEntityRegisterar),
|
||||||
nameof(XAiConversationEntityRegisterar),
|
nameof(XAiConversationEntityRegisterar),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace xAiApi.Interfaces
|
||||||
|
{
|
||||||
|
public interface IXAiMcpServerProvider
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,980 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using xModels.Dtos;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Text.Json;
|
||||||
|
using xAiApi.Interfaces;
|
||||||
|
using xCommons.Providers;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xAiModels.Constants;
|
||||||
|
using xAiModels.Extensions;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using xAiModels.Models.Dtos;
|
||||||
|
using xIdentityModels.Models;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using xAiModels.Models.Entities;
|
||||||
|
using xAiModels.Interfaces.Dtos;
|
||||||
|
using xIdentityHelper.Extensions;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
|
using ModelContextProtocol.Client;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using xAiModels.Interfaces.Entities;
|
||||||
|
using xAiModels.Interfaces.Enrichers;
|
||||||
|
|
||||||
|
namespace xAiApi.Providers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// MCP Manager / Provider ...
|
||||||
|
/// </summary>
|
||||||
|
public class XAiMcpServerProvider : IXAiMcpServerProvider, IAsyncDisposable
|
||||||
|
{
|
||||||
|
private readonly IXIdentityProvider identityProvider;
|
||||||
|
private readonly ILogger<XAiMcpServerProvider> logger;
|
||||||
|
private readonly XValidationProvider validationProvider;
|
||||||
|
private readonly IXAiMcpServerEnricher mcpServerEnricher;
|
||||||
|
private readonly IXAiMcpServerServiceProvider mcpServerServiceProvider;
|
||||||
|
private readonly IXAiMcpServerRepositoryProvider mcpServerRepositoryProvider;
|
||||||
|
|
||||||
|
private readonly Dictionary<Guid, McpClient> clients = [];
|
||||||
|
private readonly Dictionary<Guid, XAiMcpServerDto> mcpServers = [];
|
||||||
|
private readonly Dictionary<Guid, IList<McpClientTool>> tools = [];
|
||||||
|
private readonly SemaphoreSlim initLock = new(1, 1);
|
||||||
|
private bool isInitialized = false;
|
||||||
|
|
||||||
|
public XAiMcpServerProvider(
|
||||||
|
IXIdentityProvider identityProvider,
|
||||||
|
ILogger<XAiMcpServerProvider> logger,
|
||||||
|
XValidationProvider validationProvider,
|
||||||
|
IXAiMcpServerEnricher mcpServerEnricher,
|
||||||
|
IXAiMcpServerServiceProvider mcpServerServiceProvider,
|
||||||
|
IXAiMcpServerRepositoryProvider mcpServerRepositoryProvider
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
this.logger = logger;
|
||||||
|
this.identityProvider = identityProvider;
|
||||||
|
this.mcpServerEnricher = mcpServerEnricher;
|
||||||
|
this.validationProvider = validationProvider;
|
||||||
|
this.mcpServerServiceProvider = mcpServerServiceProvider;
|
||||||
|
this.mcpServerRepositoryProvider = mcpServerRepositoryProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask DisposeAsync()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
foreach (var client in clients.Values)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await client.DisposeAsync();
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
initLock.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Manager ...
|
||||||
|
public async Task<IList<McpClientTool>> GetAvailableToolsAsync(
|
||||||
|
bool forceOwner = false,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (userInfo.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogInformation("there isn't any McpServer for non authenticated user ...");
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!userInfo.IsAdmin())
|
||||||
|
{
|
||||||
|
forceOwner = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Initialization ...
|
||||||
|
if (isInitialized)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var selectedServers = mcpServers
|
||||||
|
.Where(s =>
|
||||||
|
s.Value.OwnerId == userInfo.UserId ||
|
||||||
|
s.Value.Scope == XAiMcpServerUsageScope.All
|
||||||
|
)
|
||||||
|
.Select(s => s.Key);
|
||||||
|
var result = tools
|
||||||
|
.Where(c => selectedServers.Contains(c.Key))
|
||||||
|
.SelectMany(c => c.Value);
|
||||||
|
return [.. result];
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
await initLock.WaitAsync(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (isInitialized)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var selectedServers = mcpServers
|
||||||
|
.Where(s =>
|
||||||
|
s.Value.OwnerId == userInfo.UserId ||
|
||||||
|
s.Value.Scope == XAiMcpServerUsageScope.All
|
||||||
|
)
|
||||||
|
.Select(s => s.Key);
|
||||||
|
var result = tools
|
||||||
|
.Where(c => selectedServers.Contains(c.Key))
|
||||||
|
.SelectMany(c => c.Value);
|
||||||
|
return [.. result];
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var servers = await mcpServerServiceProvider
|
||||||
|
.FindManyAsync(
|
||||||
|
orderBuilder: null,
|
||||||
|
includeBuilder: null,
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
predicate: x => x.Enabled,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
if (servers.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var tasks = servers
|
||||||
|
.Select(s =>
|
||||||
|
ConnectAndValidateAsync(s, cancellationToken));
|
||||||
|
await Task.WhenAll(tasks);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
isInitialized = true;
|
||||||
|
return [.. tools.Values.SelectMany(t => t)];
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
initLock.Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> TestServerAsync(
|
||||||
|
XAiMcpServerDto server,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||||
|
cts.CancelAfter(TimeSpan.FromSeconds(server.TimeoutSeconds));
|
||||||
|
|
||||||
|
//
|
||||||
|
var client = await CreateClientAsync(server, cts.Token);
|
||||||
|
await client.PingAsync(cancellationToken: cts.Token);
|
||||||
|
var serverTools = await client.ListToolsAsync(cancellationToken: cts.Token);
|
||||||
|
|
||||||
|
//
|
||||||
|
var names = string.Join(", ", serverTools.Select(t => t.Name));
|
||||||
|
return $"✓ Server: '{server.Name}' is Ready to Use, {serverTools.Count} Tools: {names}";
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
return $"✗ Error: {ex.Message} ...";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task RefreshAsync()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogInformation("MCP: Reinitialize Mcp Servers ...");
|
||||||
|
foreach (var client in clients.Values)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await client.DisposeAsync();
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
tools.Clear();
|
||||||
|
clients.Clear();
|
||||||
|
mcpServers.Clear();
|
||||||
|
|
||||||
|
//
|
||||||
|
isInitialized = false;
|
||||||
|
await GetAvailableToolsAsync(cancellationToken: CancellationToken.None);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Actions ...
|
||||||
|
/// <summary>
|
||||||
|
/// Add an Mcp Server ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<XAiMcpServerDto> AddMcpServer(
|
||||||
|
XAiMcpServerDto item,
|
||||||
|
string connectionId = null,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validation ...
|
||||||
|
validationProvider
|
||||||
|
.GroupValidationBuilder()
|
||||||
|
.AddNotNull(item)
|
||||||
|
.AddNotNull(userInfo)
|
||||||
|
.AddUrl(item.Endpoint)
|
||||||
|
.AddNotEmpty(item.Endpoint)
|
||||||
|
.AddNotEmpty(userInfo.UserId)
|
||||||
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Normalize Scope ...
|
||||||
|
if (!userInfo.IsAdmin())
|
||||||
|
{
|
||||||
|
item.Scope = XAiMcpServerUsageScope.Owner;
|
||||||
|
}
|
||||||
|
item.OwnerId = userInfo.UserId;
|
||||||
|
item.CreatedOn = DateTime.UtcNow;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Normalizing Timeout ...
|
||||||
|
if (item.TimeoutSeconds < XAiModelsConstants.XAiMcpServerMinTimeoutSeconds)
|
||||||
|
{
|
||||||
|
item.TimeoutSeconds = XAiModelsConstants.XAiMcpServerMinTimeoutSeconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate for Add ...
|
||||||
|
if (!item.IsValidForAddOrUpdate())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await mcpServerServiceProvider
|
||||||
|
.AddAsync(
|
||||||
|
item: item,
|
||||||
|
saveChanges: true,
|
||||||
|
connectionId: connectionId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!result.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Handle Enrichment ...
|
||||||
|
result = await mcpServerEnricher.Enrich(
|
||||||
|
source: result,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update Specified Mcp Server ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<XAiMcpServerDto> UpdateMcpServer(
|
||||||
|
Guid id,
|
||||||
|
XAiMcpServerDto item,
|
||||||
|
string connectionId = null,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validation ...
|
||||||
|
validationProvider
|
||||||
|
.GroupValidationBuilder()
|
||||||
|
.AddIsGuid(id)
|
||||||
|
.AddNotNull(item)
|
||||||
|
.AddIsGuid(item.Id)
|
||||||
|
.AddNotNull(userInfo)
|
||||||
|
.AddUrl(item.Endpoint)
|
||||||
|
.AddNotNull(item.CreatedOn)
|
||||||
|
.AddNotEmpty(item.Endpoint)
|
||||||
|
.AddNotEmpty(userInfo.UserId)
|
||||||
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Normalize Scope ...
|
||||||
|
if (!userInfo.IsAdmin())
|
||||||
|
{
|
||||||
|
item.Scope = XAiMcpServerUsageScope.Owner;
|
||||||
|
}
|
||||||
|
item.UpdatedAt = DateTime.UtcNow;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Ownership ...
|
||||||
|
if (!userInfo.IsAdmin() &&
|
||||||
|
userInfo.UserId != item.OwnerId)
|
||||||
|
{
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Normalizing Timeout ...
|
||||||
|
if (item.TimeoutSeconds < XAiModelsConstants.XAiMcpServerMinTimeoutSeconds)
|
||||||
|
{
|
||||||
|
item.TimeoutSeconds = XAiModelsConstants.XAiMcpServerMinTimeoutSeconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate for Add ...
|
||||||
|
if (!item.IsValidForAddOrUpdate())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await mcpServerServiceProvider
|
||||||
|
.UpdateAsync(
|
||||||
|
id: id,
|
||||||
|
item: item,
|
||||||
|
saveChanges: true,
|
||||||
|
connectionId: connectionId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!result.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Handle Enrichment ...
|
||||||
|
result = await mcpServerEnricher.Enrich(
|
||||||
|
source: result,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove Specified Mcp Server ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<XAiMcpServerDto> RemoveMcpServer(
|
||||||
|
Guid id,
|
||||||
|
string connectionId = null,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
validationProvider
|
||||||
|
.GroupValidationBuilder()
|
||||||
|
.AddIsGuid(id)
|
||||||
|
.AddNotNull(userInfo)
|
||||||
|
.AddNotEmpty(userInfo.UserId)
|
||||||
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check McpServer Exists ...
|
||||||
|
var exists = await mcpServerServiceProvider.GetAsync(
|
||||||
|
id: id,
|
||||||
|
includeBuilder: null,
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
if (exists.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Ownership ...
|
||||||
|
if (!userInfo.IsAdmin() &&
|
||||||
|
userInfo.UserId != exists.OwnerId)
|
||||||
|
{
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await mcpServerServiceProvider.RemoveAsync(
|
||||||
|
id: id,
|
||||||
|
softDelete: true,
|
||||||
|
saveChanges: true,
|
||||||
|
connectionId: connectionId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!result.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Handle Enrichment ...
|
||||||
|
result = await mcpServerEnricher.Enrich(
|
||||||
|
source: result,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Disable Specified Mcp Server ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> DisableMcpServer(
|
||||||
|
Guid id,
|
||||||
|
string connectionId = null,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
validationProvider
|
||||||
|
.GroupValidationBuilder()
|
||||||
|
.AddIsGuid(id)
|
||||||
|
.AddNotNull(userInfo)
|
||||||
|
.AddNotEmpty(userInfo.UserId)
|
||||||
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check McpServer Exists ...
|
||||||
|
var exists = await mcpServerServiceProvider.GetAsync(
|
||||||
|
id: id,
|
||||||
|
includeBuilder: null,
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
if (exists.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Ownership ...
|
||||||
|
if (!userInfo.IsAdmin() &&
|
||||||
|
userInfo.UserId != exists.OwnerId)
|
||||||
|
{
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!exists.Enabled)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
exists.Enabled = false;
|
||||||
|
|
||||||
|
//
|
||||||
|
exists = await mcpServerServiceProvider.UpdateAsync(
|
||||||
|
id: id,
|
||||||
|
item: exists,
|
||||||
|
saveChanges: true,
|
||||||
|
connectionId: connectionId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
var result = !exists.IsNullOrDefault() && !exists.Enabled;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Enable Specified Mcp Server ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> EnableMcpServer(
|
||||||
|
Guid id,
|
||||||
|
string connectionId = null,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
validationProvider
|
||||||
|
.GroupValidationBuilder()
|
||||||
|
.AddIsGuid(id)
|
||||||
|
.AddNotNull(userInfo)
|
||||||
|
.AddNotEmpty(userInfo.UserId)
|
||||||
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check McpServer Exists ...
|
||||||
|
var exists = await mcpServerServiceProvider.GetAsync(
|
||||||
|
id: id,
|
||||||
|
includeBuilder: null,
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
if (exists.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Ownership ...
|
||||||
|
if (!userInfo.IsAdmin() &&
|
||||||
|
userInfo.UserId != exists.OwnerId)
|
||||||
|
{
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (exists.Enabled)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
exists.Enabled = true;
|
||||||
|
|
||||||
|
//
|
||||||
|
exists = await mcpServerServiceProvider.UpdateAsync(
|
||||||
|
id: id,
|
||||||
|
item: exists,
|
||||||
|
saveChanges: true,
|
||||||
|
connectionId: connectionId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
var result = !exists.IsNullOrDefault() && exists.Enabled;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Toggle Specified Mcp Server ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> ToggleMcpServer(
|
||||||
|
Guid id,
|
||||||
|
string connectionId = null,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
validationProvider
|
||||||
|
.GroupValidationBuilder()
|
||||||
|
.AddIsGuid(id)
|
||||||
|
.AddNotNull(userInfo)
|
||||||
|
.AddNotEmpty(userInfo.UserId)
|
||||||
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check McpServer Exists ...
|
||||||
|
var exists = await mcpServerServiceProvider.GetAsync(
|
||||||
|
id: id,
|
||||||
|
includeBuilder: null,
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
if (exists.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Ownership ...
|
||||||
|
if (!userInfo.IsAdmin() &&
|
||||||
|
userInfo.UserId != exists.OwnerId)
|
||||||
|
{
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
exists.Enabled = !exists.Enabled;
|
||||||
|
|
||||||
|
//
|
||||||
|
exists = await mcpServerServiceProvider.UpdateAsync(
|
||||||
|
id: id,
|
||||||
|
item: exists,
|
||||||
|
saveChanges: true,
|
||||||
|
connectionId: connectionId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
var result = !exists.IsNullOrDefault();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve all Mcp Server ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="forceOwner"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<IEnumerable<XAiMcpServerDto>> GetAll(
|
||||||
|
bool forceOwner = false,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
validationProvider
|
||||||
|
.GroupValidationBuilder()
|
||||||
|
.AddNotNull(userInfo)
|
||||||
|
.AddNotEmpty(userInfo.UserId)
|
||||||
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!userInfo.IsAdmin())
|
||||||
|
{
|
||||||
|
forceOwner = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = Enumerable.Empty<XAiMcpServerDto>();
|
||||||
|
Func<IQueryable<XAiMcpServer>, IOrderedQueryable<XAiMcpServer>> orderBuilder = x => x.OrderByDescending(y => y.CreatedOn);
|
||||||
|
if (forceOwner)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result = await mcpServerServiceProvider
|
||||||
|
.FindManyAsync(
|
||||||
|
predicate: x =>
|
||||||
|
x.OwnerId
|
||||||
|
.Equals(
|
||||||
|
userInfo.UserId,
|
||||||
|
StringComparison.InvariantCultureIgnoreCase
|
||||||
|
),
|
||||||
|
includeBuilder: null,
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
orderBuilder: orderBuilder,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result = await mcpServerServiceProvider
|
||||||
|
.GetAllAsync(
|
||||||
|
includeBuilder: null,
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
orderBuilder: orderBuilder,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (result.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Handle Enrichment ...
|
||||||
|
result = await mcpServerEnricher.EnrichRange(
|
||||||
|
sources: result,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Find Specified Mcp Server ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="predicate"></param>
|
||||||
|
/// <param name="forceOwner"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<XAiMcpServerDto> FindOne(
|
||||||
|
Expression<Func<XAiMcpServer, bool>> predicate,
|
||||||
|
bool forceOwner = false,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
validationProvider
|
||||||
|
.GroupValidationBuilder()
|
||||||
|
.AddNotNull(userInfo)
|
||||||
|
.AddNotEmpty(userInfo.UserId)
|
||||||
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!userInfo.IsAdmin())
|
||||||
|
{
|
||||||
|
forceOwner = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
Expression<Func<XAiMcpServer, bool>> predicator = null;
|
||||||
|
if (forceOwner)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
predicator = x =>
|
||||||
|
predicate.Compile()(x) &&
|
||||||
|
x.OwnerId
|
||||||
|
.Equals(
|
||||||
|
userInfo.UserId,
|
||||||
|
StringComparison.InvariantCultureIgnoreCase
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
predicator = x => predicate.Compile()(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await mcpServerServiceProvider.FindOneAsync(
|
||||||
|
includeBuilder: null,
|
||||||
|
predicate: predicator,
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!result.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Handle Enrichment ...
|
||||||
|
result = await mcpServerEnricher.Enrich(
|
||||||
|
source: result,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<XAiMcpServerDto>> FindMany(
|
||||||
|
Expression<Func<XAiMcpServer, bool>> predicate,
|
||||||
|
bool forceOwner = false,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
validationProvider
|
||||||
|
.GroupValidationBuilder()
|
||||||
|
.AddNotNull(userInfo)
|
||||||
|
.AddNotEmpty(userInfo.UserId)
|
||||||
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!userInfo.IsAdmin())
|
||||||
|
{
|
||||||
|
forceOwner = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
Expression<Func<XAiMcpServer, bool>> predicator = null;
|
||||||
|
if (forceOwner)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
predicator = x =>
|
||||||
|
predicate.Compile()(x) &&
|
||||||
|
x.OwnerId
|
||||||
|
.Equals(
|
||||||
|
userInfo.UserId,
|
||||||
|
StringComparison.InvariantCultureIgnoreCase
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
predicator = x => predicate.Compile()(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await mcpServerServiceProvider.FindManyAsync(
|
||||||
|
includeBuilder: null,
|
||||||
|
predicate: predicator,
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
orderBuilder: x => x.OrderByDescending(y => y.CreatedOn)
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
if (result.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Handle Enrichment ...
|
||||||
|
result = await mcpServerEnricher.EnrichRange(
|
||||||
|
sources: result,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<XQueryResult<XAiMcpServerDto>> Query(
|
||||||
|
XQuery query,
|
||||||
|
bool forceOwner = false,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
validationProvider
|
||||||
|
.GroupValidationBuilder()
|
||||||
|
.AddNotNull(userInfo)
|
||||||
|
.AddNotEmpty(userInfo.UserId)
|
||||||
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!userInfo.IsAdmin())
|
||||||
|
{
|
||||||
|
forceOwner = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
Expression<Func<XAiMcpServer, bool>> predicator = null;
|
||||||
|
if (forceOwner)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
predicator = x =>
|
||||||
|
x.OwnerId
|
||||||
|
.Equals(
|
||||||
|
userInfo.UserId,
|
||||||
|
StringComparison.InvariantCultureIgnoreCase
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await mcpServerServiceProvider.QueryAsync(
|
||||||
|
query: query,
|
||||||
|
includeBuilder: null,
|
||||||
|
predicate: predicator,
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
orderBuilder: x => x.OrderByDescending(y => y.CreatedOn)
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!result.IsNullOrDefault() && result.Items.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Handle Enrichment ...
|
||||||
|
result.Items = await mcpServerEnricher.EnrichRange(
|
||||||
|
sources: result.Items,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Private ...
|
||||||
|
private static Dictionary<string, string> ParseJsonDict(string json)
|
||||||
|
=> json.IsNullOrEmpty() ? null
|
||||||
|
: JsonSerializer.Deserialize<Dictionary<string, string>>(json);
|
||||||
|
|
||||||
|
private static List<string> ParseJsonList(string json)
|
||||||
|
=> json.IsNullOrEmpty() ? null
|
||||||
|
: JsonSerializer.Deserialize<List<string>>(json);
|
||||||
|
|
||||||
|
private static async Task<McpClient> CreateClientAsync(
|
||||||
|
XAiMcpServerDto s,
|
||||||
|
CancellationToken cancellationToken
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
IClientTransport transport = new HttpClientTransport(new HttpClientTransportOptions
|
||||||
|
{
|
||||||
|
Endpoint = new Uri(s.Endpoint!),
|
||||||
|
AdditionalHeaders = ParseJsonDict(s.Headers),
|
||||||
|
TransportMode = HttpTransportMode.AutoDetect
|
||||||
|
});
|
||||||
|
|
||||||
|
//
|
||||||
|
return await McpClient
|
||||||
|
.CreateAsync(
|
||||||
|
transport,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ConnectAndValidateAsync(
|
||||||
|
XAiMcpServerDto descriptor,
|
||||||
|
CancellationToken cancellationToken
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||||
|
cts.CancelAfter(TimeSpan.FromSeconds(descriptor.TimeoutSeconds));
|
||||||
|
|
||||||
|
//
|
||||||
|
var client = await CreateClientAsync(descriptor, cts.Token);
|
||||||
|
await client.PingAsync(cancellationToken: cts.Token);
|
||||||
|
var serverTools = await client
|
||||||
|
.ListToolsAsync(cancellationToken: cts.Token);
|
||||||
|
|
||||||
|
//
|
||||||
|
clients[descriptor.Id] = client;
|
||||||
|
tools[descriptor.Id] = serverTools;
|
||||||
|
mcpServers[descriptor.Id] = descriptor;
|
||||||
|
|
||||||
|
//
|
||||||
|
logger.LogInformation(
|
||||||
|
"MCP '{Name}': {Count} Tools Detected ...",
|
||||||
|
descriptor.Name,
|
||||||
|
serverTools.Count
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
logger.LogWarning(ex,
|
||||||
|
"MCP '{Name}': Connection Established Failed, Ignored ...",
|
||||||
|
descriptor.Name
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -57,6 +57,7 @@
|
|||||||
<PackageReference Include="RtfPipe" Version="2.0.7677.4303" />
|
<PackageReference Include="RtfPipe" Version="2.0.7677.4303" />
|
||||||
<PackageReference Include="HtmlAgilityPack" Version="1.13.0" />
|
<PackageReference Include="HtmlAgilityPack" Version="1.13.0" />
|
||||||
<PackageReference Include="Whisper.net.Runtime" Version="1.9.1" />
|
<PackageReference Include="Whisper.net.Runtime" Version="1.9.1" />
|
||||||
|
<PackageReference Include="ModelContextProtocol" Version="2.3.1" />
|
||||||
<PackageReference Include="OfficeIMO.PowerPoint" Version="3.5.1" />
|
<PackageReference Include="OfficeIMO.PowerPoint" Version="3.5.1" />
|
||||||
<PackageReference Include="OfficeIMO.OpenDocument" Version="3.5.1" />
|
<PackageReference Include="OfficeIMO.OpenDocument" Version="3.5.1" />
|
||||||
<PackageReference Include="DocumentFormat.OpenXml" Version="3.5.1" />
|
<PackageReference Include="DocumentFormat.OpenXml" Version="3.5.1" />
|
||||||
|
|||||||
Reference in New Issue
Block a user