Change Requirements for Authorization and Authentication Handling ...
This commit is contained in:
@@ -37,15 +37,21 @@ namespace xAiService.Controllers
|
|||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> HandleMcp(
|
public virtual async Task<IActionResult> HandleMcp(
|
||||||
[FromBody] JObject request,
|
[FromBody] JObject request,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
//
|
||||||
|
var userInfo = await GetUserInfo();
|
||||||
|
var connectionId = GetConnectionId();
|
||||||
|
|
||||||
//
|
//
|
||||||
var response = await mcpHandler.ProcessAsync(
|
var response = await mcpHandler.ProcessAsync(
|
||||||
request,
|
request: request,
|
||||||
cancellationToken
|
userInfo: userInfo,
|
||||||
|
connectionId: connectionId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -58,7 +64,7 @@ namespace xAiService.Controllers
|
|||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("sse")]
|
[HttpGet("sse")]
|
||||||
public async Task SseEndpoint(
|
public virtual async Task SseEndpoint(
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ namespace xAiService.Extensions
|
|||||||
this IServiceCollection source
|
this IServiceCollection source
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
//
|
||||||
|
// Register MCP User Context ...
|
||||||
|
source.AddScoped<IXAiMcpUserContext, XAiMcpUserContext>();
|
||||||
|
|
||||||
//
|
//
|
||||||
// Register MCP Tool Scanner ...
|
// Register MCP Tool Scanner ...
|
||||||
source.AddSingleton(sp =>
|
source.AddSingleton(sp =>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using xIdentityModels.Models;
|
||||||
|
|
||||||
namespace xAiService.Interfaces
|
namespace xAiService.Interfaces
|
||||||
{
|
{
|
||||||
@@ -10,10 +11,14 @@ namespace xAiService.Interfaces
|
|||||||
/// Process Specified JSON-RPC Request ...
|
/// Process Specified JSON-RPC Request ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<JObject> ProcessAsync(
|
Task<JObject> ProcessAsync(
|
||||||
JObject request,
|
JObject request,
|
||||||
|
string connectionId = null,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
using xIdentityModels.Models;
|
||||||
|
|
||||||
|
namespace xAiService.Interfaces
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// User Context for MCP Servers ...
|
||||||
|
///
|
||||||
|
/// Automatic Filled in HTTP Request Execution Time ...
|
||||||
|
/// When Request Execution Ended, Disposed ...
|
||||||
|
///
|
||||||
|
/// - for Instanced Tools: Accessible using Dependency Injection ...
|
||||||
|
/// - for Static Tools: Accessible using Static Properties Current* ...
|
||||||
|
/// </summary>
|
||||||
|
public interface IXAiMcpUserContext
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Connection Id ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
string ConnectionId { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// User Claims ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
XUserClaimsInfoDto UserInfo { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fill Context ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
void Set(
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
string connectionId = null
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Clear Context ...
|
||||||
|
/// </summary>
|
||||||
|
void Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
+86
-11
@@ -5,6 +5,9 @@ using Newtonsoft.Json.Linq;
|
|||||||
using xAiService.Interfaces;
|
using xAiService.Interfaces;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using xIdentityModels.Models;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace xAiService.Services
|
namespace xAiService.Services
|
||||||
{
|
{
|
||||||
@@ -12,16 +15,19 @@ namespace xAiService.Services
|
|||||||
{
|
{
|
||||||
//
|
//
|
||||||
private readonly XAiMcpToolScanner scanner;
|
private readonly XAiMcpToolScanner scanner;
|
||||||
|
private readonly IXAiMcpUserContext userContext;
|
||||||
private readonly IServiceProvider serviceProvider;
|
private readonly IServiceProvider serviceProvider;
|
||||||
private readonly Dictionary<string, XAiMcpToolDescriptor> tools = new();
|
private readonly Dictionary<string, XAiMcpToolDescriptor> tools = new();
|
||||||
|
|
||||||
public XAiMcpHandler(
|
public XAiMcpHandler(
|
||||||
XAiMcpToolScanner scanner,
|
XAiMcpToolScanner scanner,
|
||||||
|
IXAiMcpUserContext userContext,
|
||||||
IServiceProvider serviceProvider
|
IServiceProvider serviceProvider
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
this.scanner = scanner;
|
this.scanner = scanner;
|
||||||
|
this.userContext = userContext;
|
||||||
this.serviceProvider = serviceProvider;
|
this.serviceProvider = serviceProvider;
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -32,27 +38,42 @@ namespace xAiService.Services
|
|||||||
/// Process Specified JSON-RPC Request ...
|
/// Process Specified JSON-RPC Request ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<JObject> ProcessAsync(
|
public async Task<JObject> ProcessAsync(
|
||||||
JObject request,
|
JObject request,
|
||||||
|
string connectionId = null,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
var method = request["method"]?.ToString();
|
// Filling User Context ...
|
||||||
var id = request["id"];
|
userContext.Set(userInfo, connectionId);
|
||||||
|
|
||||||
//
|
//
|
||||||
return method switch
|
try
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
"initialize" => HandleInitialize(id),
|
var id = request["id"];
|
||||||
"tools/list" => HandleToolsList(id),
|
var method = request["method"]?.ToString();
|
||||||
"tools/call" => await HandleToolCallAsync(request, id, cancellationToken),
|
|
||||||
"ping" => HandlePing(id),
|
//
|
||||||
_ => CreateError(id, -32601, $"Method not found: {method}")
|
return method switch
|
||||||
};
|
{
|
||||||
|
"initialize" => HandleInitialize(id),
|
||||||
|
"tools/list" => HandleToolsList(id, userInfo),
|
||||||
|
"tools/call" => await HandleToolCallAsync(request, id, userInfo, cancellationToken),
|
||||||
|
"ping" => HandlePing(id),
|
||||||
|
_ => CreateError(id, -32601, $"Method not found: {method}")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
userContext.Clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -74,11 +95,22 @@ namespace xAiService.Services
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private JObject HandleToolsList(JToken id)
|
private JObject HandleToolsList(
|
||||||
|
JToken id,
|
||||||
|
XUserClaimsInfoDto userInfo
|
||||||
|
)
|
||||||
{
|
{
|
||||||
var jsonTools = new JArray();
|
var jsonTools = new JArray();
|
||||||
foreach (var tool in tools.Values)
|
foreach (var tool in tools.Values)
|
||||||
{
|
{
|
||||||
|
//
|
||||||
|
if (!tool.RequiredPolicy.IsNullOrEmpty() &&
|
||||||
|
!CheckPolicy(userInfo, tool.RequiredPolicy))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
jsonTools.Add(new JObject
|
jsonTools.Add(new JObject
|
||||||
{
|
{
|
||||||
["name"] = tool.Name,
|
["name"] = tool.Name,
|
||||||
@@ -94,6 +126,7 @@ namespace xAiService.Services
|
|||||||
private async Task<JObject> HandleToolCallAsync(
|
private async Task<JObject> HandleToolCallAsync(
|
||||||
JObject request,
|
JObject request,
|
||||||
JToken id,
|
JToken id,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
CancellationToken ct
|
CancellationToken ct
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -108,7 +141,28 @@ namespace xAiService.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
var result = await tools[toolName].Execute(args, ct);
|
var tool = tools[toolName];
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check User Info Required or not ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Level 1 ...
|
||||||
|
if (userInfo.IsNull() && tool.RequiresAuthentication)
|
||||||
|
{
|
||||||
|
return CreateError(id, -32001, $"Authentication required for tool: {toolName}");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Level 2 ...
|
||||||
|
if (!tool.RequiredPolicy.IsNullOrEmpty() &&
|
||||||
|
!CheckPolicy(userInfo, tool.RequiredPolicy))
|
||||||
|
{
|
||||||
|
return CreateError(id, -32002, $"Access denied for tool: {toolName}");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await tool.Execute(args, ct);
|
||||||
return CreateResult(id, new JObject
|
return CreateResult(id, new JObject
|
||||||
{
|
{
|
||||||
["content"] = new JArray
|
["content"] = new JArray
|
||||||
@@ -205,6 +259,27 @@ namespace xAiService.Services
|
|||||||
{
|
{
|
||||||
return CreateResult(id, new JObject());
|
return CreateResult(id, new JObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool CheckPolicy(XUserClaimsInfoDto user, string policy)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = false;
|
||||||
|
|
||||||
|
//
|
||||||
|
if (user.IsNullOrDefault()) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Handle Authorization ...
|
||||||
|
result = user.Roles?.Any(r =>
|
||||||
|
r.Equals(
|
||||||
|
policy,
|
||||||
|
StringComparison.OrdinalIgnoreCase)) == true;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,6 +13,8 @@ namespace xAiService.Services
|
|||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Description { get; set; }
|
public string Description { get; set; }
|
||||||
public JObject InputSchema { get; set; }
|
public JObject InputSchema { get; set; }
|
||||||
|
public string RequiredPolicy { get; set; }
|
||||||
|
public bool RequiresAuthentication { get; set; }
|
||||||
|
|
||||||
internal MethodInfo Method { get; set; }
|
internal MethodInfo Method { get; set; }
|
||||||
internal Type DeclaringType { get; set; }
|
internal Type DeclaringType { get; set; }
|
||||||
@@ -30,7 +32,9 @@ namespace xAiService.Services
|
|||||||
Name = Name,
|
Name = Name,
|
||||||
Description = Description,
|
Description = Description,
|
||||||
InputSchema = InputSchema,
|
InputSchema = InputSchema,
|
||||||
Execute = BuildExecute(serviceProvider)
|
RequiredPolicy = RequiredPolicy,
|
||||||
|
Execute = BuildExecute(serviceProvider),
|
||||||
|
RequiresAuthentication = RequiresAuthentication,
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -44,7 +44,8 @@ namespace xAiService.Services
|
|||||||
foreach (var type in SafeGetTypes(asm))
|
foreach (var type in SafeGetTypes(asm))
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
if (type.GetCustomAttribute<XAiMcpToolTypeAttribute>() == null) {
|
if (type.GetCustomAttribute<XAiMcpToolTypeAttribute>() == null)
|
||||||
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,6 +79,13 @@ namespace xAiService.Services
|
|||||||
?? method.GetCustomAttribute<DescriptionAttribute>()?.Description
|
?? method.GetCustomAttribute<DescriptionAttribute>()?.Description
|
||||||
?? string.Empty;
|
?? string.Empty;
|
||||||
|
|
||||||
|
//
|
||||||
|
var requiredPolicy = attr.RequiredPolicy
|
||||||
|
?? string.Empty;
|
||||||
|
|
||||||
|
//
|
||||||
|
var requiredAuthenticated = attr.RequiresAuthentication;
|
||||||
|
|
||||||
//
|
//
|
||||||
result.Add(new XAiMcpToolBlueprint
|
result.Add(new XAiMcpToolBlueprint
|
||||||
{
|
{
|
||||||
@@ -85,12 +93,15 @@ namespace xAiService.Services
|
|||||||
Method = method,
|
Method = method,
|
||||||
DeclaringType = type,
|
DeclaringType = type,
|
||||||
Description = description,
|
Description = description,
|
||||||
InputSchema = BuildInputSchema(method)
|
RequiredPolicy = requiredPolicy,
|
||||||
|
InputSchema = BuildInputSchema(method),
|
||||||
|
RequiresAuthentication = requiredAuthenticated
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
using System.Threading;
|
||||||
|
using xAiService.Interfaces;
|
||||||
|
using xIdentityModels.Models;
|
||||||
|
|
||||||
|
namespace xAiService.Services
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// User Context for MCP Servers ...
|
||||||
|
///
|
||||||
|
/// Automatic Filled in HTTP Request Execution Time ...
|
||||||
|
/// When Request Execution Ended, Disposed ...
|
||||||
|
///
|
||||||
|
/// - for Instanced Tools: Accessible using Dependency Injection ...
|
||||||
|
/// - for Static Tools: Accessible using Static Properties Current* ...
|
||||||
|
/// </summary>
|
||||||
|
public class XAiMcpUserContext : IXAiMcpUserContext
|
||||||
|
{
|
||||||
|
//
|
||||||
|
private static readonly AsyncLocal<string> connectionIdAsyncLocal = new();
|
||||||
|
private static readonly AsyncLocal<XUserClaimsInfoDto> userAsyncLocal = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// User Claims ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public XUserClaimsInfoDto UserInfo => userAsyncLocal.Value;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Connection Id ...
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public string ConnectionId => connectionIdAsyncLocal.Value;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fill Context ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
public void Set(
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
string connectionId = null
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
userAsyncLocal.Value = userInfo;
|
||||||
|
connectionIdAsyncLocal.Value = connectionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Clear Context ...
|
||||||
|
/// </summary>
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
userAsyncLocal.Value = null;
|
||||||
|
connectionIdAsyncLocal.Value = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Static ...
|
||||||
|
public static XUserClaimsInfoDto CurrentUser => userAsyncLocal.Value;
|
||||||
|
public static string CurrentConnectionId => connectionIdAsyncLocal.Value;
|
||||||
|
public static bool IsAuthenticated => userAsyncLocal.Value != null;
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user