Handle Authentication and Authorization in xAiMcpServer ...
This commit is contained in:
@@ -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 ...
|
||||
/// <summary>
|
||||
/// Handle MCP ...
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public override async Task<IActionResult> HandleMcp(
|
||||
[FromBody] JObject request,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await base
|
||||
.HandleMcp(
|
||||
request: request,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle SSE EndPoint ...
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("sse")]
|
||||
[AllowAnonymous]
|
||||
public override async Task SseEndpoint(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
await base
|
||||
.SseEndpoint(cancellationToken);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register Tools which required Dependency Injection Services ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
public static void AddXAiMcpTools(this IServiceCollection source)
|
||||
{
|
||||
//
|
||||
source.AddScoped<XUserTools>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,6 +89,10 @@ namespace xAiMcpServer
|
||||
lifeTime: lifeTime,
|
||||
configuration: Configuration
|
||||
);
|
||||
|
||||
//
|
||||
// Register MCP Tools in DI ...
|
||||
services.AddXAiMcpTools();
|
||||
}
|
||||
|
||||
public void Configure(
|
||||
|
||||
@@ -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<string> WhoAmI()
|
||||
{
|
||||
//
|
||||
var user = userContext.UserInfo;
|
||||
if (user.IsNullOrDefault())
|
||||
{
|
||||
return "Anonymous";
|
||||
}
|
||||
|
||||
//
|
||||
var person = await personService.GetPerson(user.UserId);
|
||||
|
||||
//
|
||||
return person?.GetFullname() ?? user.UserId;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,7 @@
|
||||
<!-- Dependencies -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.11" />
|
||||
<PackageReference Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- For XML Documentation Support -->
|
||||
|
||||
Reference in New Issue
Block a user