using System; using xModels.Dtos; using xIdentityHelper; using System.Threading; using xAiApi.Interfaces; using xCommons.Providers; using xCommons.Extensions; using xAiModels.Extensions; using xAiModels.Models.Dtos; using xExceptions.Constants; using System.Threading.Tasks; using xCommons.Configurations; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using xIdentityService.Interfaces; using Microsoft.Extensions.Logging; using xIdentityService.Controllers; using Microsoft.AspNetCore.Authorization; namespace xAiApi.Controllers { [Authorize] public class McpServersController : XBaseIdentityApiV1Controller { private readonly IXAiMcpServerProvider mcpServerProvider; public McpServersController( XAppConfiguration appConfiguration, IXIdentityProvider identityProvider, ILogger logger, XValidationProvider validationProvider, IXAiMcpServerProvider mcpServerProvider ) : base( logger, appConfiguration, identityProvider, validationProvider ) { this.mcpServerProvider = mcpServerProvider; } // #region Provider ... /// /// Retrieve Specified MCP Server ... /// /// /// /// [HttpGet("{id}")] [Authorize(Policy = XPolicies.EnabledUser)] public virtual async Task> GetMcpServer( [FromRoute] Guid id, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid || id.IsDefaultGuid()) { XException.InvalidArgs.Throw(); } // var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await mcpServerProvider.GetMcpServer( id: id, userInfo: userInfo, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Add MCP Server ... /// /// /// /// [HttpPost] [Authorize(Policy = XPolicies.EnabledUser)] public virtual async Task> AddMcpServer( [FromBody] XAiMcpServerDto item, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid || item.IsNullOrDefault()) { XException.InvalidArgs.Throw(); } // var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await mcpServerProvider.AddMcpServer( item: item, userInfo: userInfo, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Update Specified MCP Server ... /// /// /// /// /// [HttpPut("{id}")] [Authorize(Policy = XPolicies.EnabledUser)] public virtual async Task> UpdateMcpServer( [FromRoute] Guid id, [FromBody] XAiMcpServerDto item, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (id.IsDefaultGuid() || !ModelState.IsValid || !item.IsValidForAddOrUpdate()) { XException.InvalidArgs.Throw(); } // var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await mcpServerProvider.UpdateMcpServer( id: id, item: item, userInfo: userInfo, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Remove Specified MCP Server ... /// /// /// /// [HttpDelete("{id}")] [Authorize(Policy = XPolicies.EnabledUser)] public virtual async Task> RemoveMcpServer( [FromRoute] Guid id, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid || id.IsDefaultGuid()) { XException.InvalidArgs.Throw(); } // var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await mcpServerProvider.RemoveMcpServer( id: id, userInfo: userInfo, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Enable Specified MCP Server ... /// /// /// /// [HttpPost("{id}/Enable")] [Authorize(Policy = XPolicies.EnabledUser)] public virtual async Task> EnableMcpServer( [FromRoute] Guid id, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid || id.IsDefaultGuid()) { XException.InvalidArgs.Throw(); } // var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await mcpServerProvider.EnableMcpServer( id: id, userInfo: userInfo, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Disable Specified MCP Server ... /// /// /// /// [HttpPost("{id}/Disable")] [Authorize(Policy = XPolicies.EnabledUser)] public virtual async Task> DisableMcpServer( [FromRoute] Guid id, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid || id.IsDefaultGuid()) { XException.InvalidArgs.Throw(); } // var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await mcpServerProvider.DisableMcpServer( id: id, userInfo: userInfo, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Toggle Specified MCP Server ... /// /// /// /// [HttpPost("{id}/Toggle")] [Authorize(Policy = XPolicies.EnabledUser)] public virtual async Task> ToggleMcpServer( [FromRoute] Guid id, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid || id.IsDefaultGuid()) { XException.InvalidArgs.Throw(); } // var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await mcpServerProvider.ToggleMcpServer( id: id, userInfo: userInfo, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Get all Registered MCP Servers by handling Ownership ... /// /// /// [HttpGet] [Authorize(Policy = XPolicies.EnabledUser)] public virtual async Task>> GetMcpServers( [FromQuery] bool forceOwner = true, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } // var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await mcpServerProvider.GetAll( userInfo: userInfo, forceOwner: forceOwner, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Query Registered MCP Servers by handling Ownership ... /// /// /// /// /// [HttpGet("Query")] [Authorize(Policy = XPolicies.EnabledUser)] public virtual async Task>> QueryMcpServers( [FromQuery] XQuery query, [FromQuery] bool forceOwner = true, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } await ValidationProvider .GroupValidationBuilder() .AddNotNull(query) .ValidateGroupAsync(); // var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await mcpServerProvider.Query( query: query, userInfo: userInfo, forceOwner: forceOwner, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } #endregion /// /// Refresh MCP Servers ... /// /// /// [HttpPost("Refresh")] [Authorize(Policy = XPolicies.EnabledAdmin)] public async Task RefreshMcpServers( CancellationToken cancellationToken = default ) { // try { // var userInfo = await GetUserInfo(); // await mcpServerProvider.RefreshAsync( userInfo: userInfo, cancellationToken: cancellationToken ); // return Ok(); } catch(Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } } }