Files
xAiService/Controllers/XAiMcpControllerBase.cs
T
saherelm 2b99958f4c Add MCP Handler Integration
- افزودن XAiMcpControllerBase.
- افزودن IXAiMcpHandler و پیاده‌سازی XAiMcpHandler.
- افزودن DI extensionهای لازم برای ثبت handler و تغییر فایل پروژهٔ سرویس.
2026-11-04 17:51:13 +03:30

84 lines
2.4 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;
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);
}
}
}
}