92 lines
2.7 KiB
C#
92 lines
2.7 KiB
C#
using System.Threading;
|
|
using xCommons.Providers;
|
|
using Newtonsoft.Json.Linq;
|
|
using xAiService.Interfaces;
|
|
using System.Threading.Tasks;
|
|
using xCommons.Configurations;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Http;
|
|
using xIdentityService.Interfaces;
|
|
using Microsoft.Extensions.Logging;
|
|
using xIdentityService.Controllers;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
namespace xAiService.Controllers
|
|
{
|
|
[Route("mcp")]
|
|
[AllowAnonymous]
|
|
public abstract class XAiMcpControllerBase : XBaseIdentityApiV1Controller
|
|
{
|
|
private readonly IXAiMcpHandler mcpHandler;
|
|
|
|
protected XAiMcpControllerBase(
|
|
IXAiMcpHandler mcpHandler,
|
|
XAppConfiguration appConfiguration,
|
|
IXIdentityProvider identityProvider,
|
|
ILogger<XAiMcpControllerBase> logger,
|
|
XValidationProvider validationProvider
|
|
) : base(logger, appConfiguration, identityProvider, validationProvider)
|
|
{
|
|
this.mcpHandler = mcpHandler;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handle MCP ...
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public virtual async Task<IActionResult> HandleMcp(
|
|
[FromBody] JObject request,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var userInfo = await GetUserInfo();
|
|
var connectionId = GetConnectionId();
|
|
|
|
//
|
|
var response = await mcpHandler.ProcessAsync(
|
|
request: request,
|
|
userInfo: userInfo,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return Ok(response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handle SSE EndPoint ...
|
|
/// </summary>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("sse")]
|
|
public virtual async Task SseEndpoint(
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
Response.Headers.Add("Content-Type", "text/event-stream");
|
|
Response.Headers.Add("Cache-Control", "no-cache");
|
|
|
|
//
|
|
await Response
|
|
.WriteAsync(
|
|
"event: endpoint\ndata: /mcp\n\n",
|
|
cancellationToken
|
|
);
|
|
|
|
//
|
|
await Response.Body.FlushAsync(cancellationToken);
|
|
|
|
//
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
{
|
|
await Task.Delay(1000, cancellationToken);
|
|
}
|
|
}
|
|
}
|
|
} |