diff --git a/Controllers/McpController.cs b/Controllers/McpController.cs index a622988..7e93b35 100644 --- a/Controllers/McpController.cs +++ b/Controllers/McpController.cs @@ -4,9 +4,16 @@ using xAiService.Controllers; using xCommons.Configurations; using xIdentityService.Interfaces; using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using xIdentityHelper; +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; +using System.Threading; namespace xAiMcpServer.Controllers { + [Authorize] public class McpController : XAiMcpControllerBase { public McpController( @@ -17,5 +24,48 @@ namespace xAiMcpServer.Controllers XValidationProvider validationProvider ) : base(mcpHandler, appConfiguration, identityProvider, logger, validationProvider) { } + + // + #region Override ... + /// + /// Handle MCP ... + /// + /// + /// + /// + [HttpPost] + [Authorize(Policy = XPolicies.EnabledUser)] + public override async Task HandleMcp( + [FromBody] JObject request, + CancellationToken cancellationToken = default + ) + { + // + var result = await base + .HandleMcp( + request: request, + cancellationToken: cancellationToken + ); + + // + return result; + } + + /// + /// Handle SSE EndPoint ... + /// + /// + /// + [HttpGet("sse")] + [AllowAnonymous] + public override async Task SseEndpoint( + CancellationToken cancellationToken = default + ) + { + // + await base + .SseEndpoint(cancellationToken); + } + #endregion } } \ No newline at end of file diff --git a/DI/DiExtensions.cs b/DI/DiExtensions.cs index 85d1fbb..fc84a68 100644 --- a/DI/DiExtensions.cs +++ b/DI/DiExtensions.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.DependencyInjection; +using xAiMcpServer.Tools; namespace xAiMcpServer.DI { @@ -82,5 +83,15 @@ namespace xAiMcpServer.DI // ref: https://github.com/aspnet/Docs/issues/2384 source.UseForwardedHeaders(forwardOptions); } + + /// + /// Register Tools which required Dependency Injection Services ... + /// + /// + public static void AddXAiMcpTools(this IServiceCollection source) + { + // + source.AddScoped(); + } } } \ No newline at end of file diff --git a/Startup.cs b/Startup.cs index bb4c884..f57e184 100644 --- a/Startup.cs +++ b/Startup.cs @@ -89,6 +89,10 @@ namespace xAiMcpServer lifeTime: lifeTime, configuration: Configuration ); + + // + // Register MCP Tools in DI ... + services.AddXAiMcpTools(); } public void Configure( diff --git a/Tools/XUserTools.cs b/Tools/XUserTools.cs new file mode 100644 index 0000000..263ab69 --- /dev/null +++ b/Tools/XUserTools.cs @@ -0,0 +1,48 @@ +using xIdentityHelper; +using xModels.Extensions; +using xCommons.Extensions; +using xAiModels.Attributes; +using xAiService.Interfaces; +using System.Threading.Tasks; +using xIdentityService.Interfaces; + +namespace xAiMcpServer.Tools +{ + [XAiMcpToolType] + public class XUserTools + { + // + private readonly IXPersonService personService; + private readonly IXAiMcpUserContext userContext; + + public XUserTools( + IXAiMcpUserContext userContext, + IXPersonService personService + ) + { + // + this.userContext = userContext; + this.personService = personService; + } + + [XAiMcpTool( + RequiresAuthentication = true, + RequiredPolicy = XPolicies.EnabledUser, + Description = "Get information about the currently logged-in user.")] + public async Task WhoAmI() + { + // + var user = userContext.UserInfo; + if (user.IsNullOrDefault()) + { + return "Anonymous"; + } + + // + var person = await personService.GetPerson(user.UserId); + + // + return person?.GetFullname() ?? user.UserId; + } + } +} \ No newline at end of file diff --git a/xAiMcpServer.csproj b/xAiMcpServer.csproj index d7836df..9d9220c 100644 --- a/xAiMcpServer.csproj +++ b/xAiMcpServer.csproj @@ -40,6 +40,7 @@ +