From c686750e2aaa9c84f5079e7709dd8e92310b2aa7 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Thu, 5 Nov 2026 10:44:17 +0330 Subject: [PATCH] Refactor MCP Server and Complete it ... --- Controllers/StartupController.cs | 18 ------------------ Program.cs | 29 +++++++++++++++++------------ Startup.cs | 19 ++++--------------- Tools/XEchoTools.cs | 14 ++++++++------ appsettings.json | 5 +++-- 5 files changed, 32 insertions(+), 53 deletions(-) diff --git a/Controllers/StartupController.cs b/Controllers/StartupController.cs index db92745..c621785 100644 --- a/Controllers/StartupController.cs +++ b/Controllers/StartupController.cs @@ -1,10 +1,7 @@ using xCommons.Providers; -using xCommons.Extensions; using xCommons.Controllers; -using xExceptions.Constants; using xCommons.Configurations; using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Authorization; @@ -44,20 +41,5 @@ namespace xAiApi.Controllers // return Ok(message); } - - /// - /// Global Exception Handler Endpoint (Production) ... - /// Returns a Generic Error without Leaking Exception Details ... - /// - /// - [AllowAnonymous] - [Route("error")] - [ApiExplorerSettings(IgnoreApi = true)] - public virtual ActionResult Error() - { - // - var error = XException.ActionFailed.ToXError(); - return StatusCode(StatusCodes.Status500InternalServerError, error); - } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index efb709d..6689146 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,6 @@ -using System; -using Grapevine; +using System.Text; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; namespace xAiMcpServer { @@ -8,19 +9,23 @@ namespace xAiMcpServer public static void Main(string[] args) { // - var server = RestServerBuilder - .From() - .Build(); - - // - server.Start(); + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); // - Console.WriteLine("MCP Server running on https://localhost:7001/mcp"); - Console.ReadLine(); - + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) + { + // + var result = Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + // - server.Stop(); + return result; } } } diff --git a/Startup.cs b/Startup.cs index 1f803b7..bb4c884 100644 --- a/Startup.cs +++ b/Startup.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using Grapevine; using System.Net.Http; using xAiMcpServer.DI; using Newtonsoft.Json; @@ -9,7 +8,6 @@ using xIdentityService.DI; using xCommons.Extensions; using xAiService.Extensions; using xCommons.Configurations; -using Grapevine.Extensions.Mcp; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; @@ -91,21 +89,12 @@ namespace xAiMcpServer lifeTime: lifeTime, configuration: Configuration ); - - // - // Register MCP Server ... - services - .AddMcpServer() - .WithHttpTransport() - .WithToolsFromAssembly(); } - public void ConfigureServer(IRestServer server) - { - server.Prefixes.Add("https://localhost:7001/"); - } - - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + public void Configure( + IApplicationBuilder app, + IWebHostEnvironment env + ) { // // Configure the HTTP request pipeline ... diff --git a/Tools/XEchoTools.cs b/Tools/XEchoTools.cs index 8d0bde5..af69f96 100644 --- a/Tools/XEchoTools.cs +++ b/Tools/XEchoTools.cs @@ -1,14 +1,16 @@ -using System.ComponentModel; -using ModelContextProtocol.Server; + +using xAiModels.Attributes; namespace xAiMcpServer.Tools { - [McpServerToolType] + [XAiMcpToolType] public static class XEchoTools { - [McpServerTool, - Description("Echoes the message back to the client.")] - public static string Echo(string message) + [XAiMcpTool(Description = "Echoes the string back to the client give message in string")] + public static string Echo( + [XAiMcpToolParam(Description = "string value which specified message")] + string message + ) { // var result = $"hello {message}"; diff --git a/appsettings.json b/appsettings.json index be150f6..1c711bb 100644 --- a/appsettings.json +++ b/appsettings.json @@ -34,9 +34,10 @@ }, "Logging": { "LogLevel": { - "Default": "Information", "Microsoft": "Warning", + "Default": "Information", "Microsoft.Hosting.Lifetime": "Information" } - } + }, + "AllowedHosts": "*" }