Add MCP Handler Integration
- افزودن XAiMcpControllerBase. - افزودن IXAiMcpHandler و پیادهسازی XAiMcpHandler. - افزودن DI extensionهای لازم برای ثبت handler و تغییر فایل پروژهٔ سرویس.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
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;
|
||||
|
||||
namespace xAiService.Controllers
|
||||
{
|
||||
[Route("mcp")]
|
||||
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 async Task<IActionResult> HandleMcp(
|
||||
[FromBody] JObject request,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var response = await mcpHandler.ProcessAsync(
|
||||
request,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle SSE EndPoint ...
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("sse")]
|
||||
public 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user