367 lines
11 KiB
Plaintext
367 lines
11 KiB
Plaintext
|
|
### FILE: C:\Users\saherelm\Documents\Projects\xSaherelmWorkspace\xAiMcpServer\Program.cs
|
|
|
|
using System.Text;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
namespace xAiMcpServer
|
|
{
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
//
|
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|
|
|
//
|
|
CreateHostBuilder(args).Build().Run();
|
|
}
|
|
|
|
public static IHostBuilder CreateHostBuilder(string[] args)
|
|
{
|
|
//
|
|
var result = Host.CreateDefaultBuilder(args)
|
|
.ConfigureWebHostDefaults(webBuilder =>
|
|
{
|
|
webBuilder.UseStartup<Startup>();
|
|
});
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|
|
### FILE: C:\Users\saherelm\Documents\Projects\xSaherelmWorkspace\xAiMcpServer\Startup.cs
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Net.Http;
|
|
using xAiMcpServer.DI;
|
|
using Newtonsoft.Json;
|
|
using System.Reflection;
|
|
using xIdentityService.DI;
|
|
using xCommons.Extensions;
|
|
using xAiService.Extensions;
|
|
using xCommons.Configurations;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Newtonsoft.Json.Serialization;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using IdentityModel.AspNetCore.OAuth2Introspection;
|
|
|
|
namespace xAiMcpServer
|
|
{
|
|
public class Startup
|
|
{
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
//
|
|
// Register MCP Handler ...
|
|
services.AddXAiMcpHandler();
|
|
|
|
//
|
|
// Register Validation Provider and all XCommons Module Services ...
|
|
services.AddXCommons(Configuration);
|
|
|
|
//
|
|
// Register App Configuration ...
|
|
services.AddXAppConfiguration(Configuration);
|
|
var appConfiguration = services.GetRegisteredService<XAppConfiguration>();
|
|
|
|
//
|
|
// Register Allowed Origins ...
|
|
services.AddXCors(appConfiguration.AllowedOrigins);
|
|
|
|
//
|
|
// Register Swagger ...
|
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
services.AddXSwagger(Configuration, xmlFilePath: xmlPath);
|
|
|
|
//
|
|
// Add Api V1 Versioning ...
|
|
services.AddXApiV1Versioning();
|
|
|
|
//
|
|
// OAuthIntrospectin Http Client Handler ...
|
|
// this is for handling SSLErrors ...
|
|
services.AddHttpClient(OAuth2IntrospectionDefaults.BackChannelHttpClientName)
|
|
.ConfigurePrimaryHttpMessageHandler(() =>
|
|
{
|
|
return new HttpClientHandler
|
|
{
|
|
ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true
|
|
};
|
|
});
|
|
|
|
//
|
|
// Register Authorizations ...
|
|
services.AddXAuthorization();
|
|
|
|
//
|
|
services.AddControllers()
|
|
.AddNewtonsoftJson(x =>
|
|
{
|
|
x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|
x.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|
});
|
|
|
|
//
|
|
var lifeTime = ServiceLifetime.Scoped;
|
|
|
|
//
|
|
// Register XIdentity Service ...
|
|
services.AddXIdentityService(
|
|
lifeTime: lifeTime,
|
|
configuration: Configuration
|
|
);
|
|
}
|
|
|
|
public void Configure(
|
|
IApplicationBuilder app,
|
|
IWebHostEnvironment env
|
|
)
|
|
{
|
|
//
|
|
// Configure the HTTP request pipeline ...
|
|
var isDevelopmentEnvironment = env.IsDevelopment();
|
|
if (isDevelopmentEnvironment)
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
else
|
|
{
|
|
app.UseHsts();
|
|
}
|
|
|
|
//
|
|
// Force Https Redirection ...
|
|
app.UseHttpsRedirection();
|
|
|
|
//
|
|
// Use Swagger Middleware (Only in Development) ...
|
|
if (isDevelopmentEnvironment)
|
|
{
|
|
app.UseXSwagger();
|
|
}
|
|
|
|
//
|
|
// Using Cors (before Proxy so Browser Requests get CORS Headers) ...
|
|
app.UseXCors();
|
|
|
|
//
|
|
// Using Routing ...
|
|
app.UseRouting();
|
|
|
|
//
|
|
// Use xIdentityService (Authentication) ...
|
|
app.UseXIdentityService();
|
|
|
|
//
|
|
// Use Authorization ...
|
|
app.UseAuthorization();
|
|
|
|
//
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
### FILE: C:\Users\saherelm\Documents\Projects\xSaherelmWorkspace\xAiMcpServer\Controllers\McpController.cs
|
|
|
|
using xCommons.Providers;
|
|
using xAiService.Interfaces;
|
|
using xAiService.Controllers;
|
|
using xCommons.Configurations;
|
|
using xIdentityService.Interfaces;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace xAiMcpServer.Controllers
|
|
{
|
|
public class McpController : XAiMcpControllerBase
|
|
{
|
|
public McpController(
|
|
IXAiMcpHandler mcpHandler,
|
|
ILogger<McpController> logger,
|
|
XAppConfiguration appConfiguration,
|
|
IXIdentityProvider identityProvider,
|
|
XValidationProvider validationProvider
|
|
) : base(mcpHandler, appConfiguration, identityProvider, logger, validationProvider)
|
|
{ }
|
|
}
|
|
}
|
|
|
|
### FILE: C:\Users\saherelm\Documents\Projects\xSaherelmWorkspace\xAiMcpServer\Controllers\StartupController.cs
|
|
|
|
using xCommons.Providers;
|
|
using xCommons.Controllers;
|
|
using xCommons.Configurations;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
namespace xAiApi.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Runing when application start ...
|
|
/// </summary>
|
|
[Route("")]
|
|
[AllowAnonymous]
|
|
public class StartupController : XBaseController
|
|
{
|
|
|
|
public StartupController(
|
|
ILogger<StartupController> logger,
|
|
XAppConfiguration appConfiguration,
|
|
XValidationProvider validationProvider
|
|
) : base(
|
|
logger,
|
|
appConfiguration,
|
|
validationProvider
|
|
)
|
|
{ }
|
|
|
|
/// <summary>
|
|
/// Show Configured Welcome Message
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("")]
|
|
[AllowAnonymous]
|
|
public virtual ActionResult<string> Index()
|
|
{
|
|
//
|
|
var controllerName = GetControllerName();
|
|
var message = $"{AppConfiguration.WelcomeMessage}";
|
|
|
|
//
|
|
return Ok(message);
|
|
}
|
|
}
|
|
}
|
|
|
|
### FILE: C:\Users\saherelm\Documents\Projects\xSaherelmWorkspace\xAiMcpServer\DI\DiExtensions.cs
|
|
|
|
using xIdentityHelper;
|
|
using xCommons.Extensions;
|
|
using xCommons.Authorization;
|
|
using System.Collections.Generic;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace xAiMcpServer.DI
|
|
{
|
|
public static class DiExtensions
|
|
{
|
|
/// <summary>
|
|
/// Register Authorization Policies
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="policies"></param>
|
|
public static void AddXAuthorization(
|
|
this IServiceCollection services,
|
|
IDictionary<string, AuthorizationPolicy> policies = null
|
|
)
|
|
{
|
|
//
|
|
services.AddAuthorization(options =>
|
|
{
|
|
//
|
|
if (!policies.IsNull())
|
|
{
|
|
//
|
|
// Fill Policies ...
|
|
using (var policiesEnumerator = policies.GetEnumerator())
|
|
{
|
|
while (policiesEnumerator.MoveNext())
|
|
{
|
|
var policyDescriptor = policiesEnumerator.Current;
|
|
options.AddPolicy(policyDescriptor.Key, policyDescriptor.Value);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
var xPolicies = XAuthorizationHelper.GetXAuthorizationPolicies();
|
|
if (!xPolicies.IsNull())
|
|
{
|
|
//
|
|
// Fill Policies ...
|
|
using (var policiesEnumerator = xPolicies.GetEnumerator())
|
|
{
|
|
while (policiesEnumerator.MoveNext())
|
|
{
|
|
var policyDescriptor = policiesEnumerator.Current;
|
|
options.AddPolicy(policyDescriptor.Key, policyDescriptor.Value);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
//
|
|
services.AddSingleton<IAuthorizationHandler, RequiredRolesHandler>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use Forward Headers Options for resolving behind a proxy issues ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static void UseXForwardOptions(this IApplicationBuilder source)
|
|
{
|
|
//
|
|
var forwardOptions = new ForwardedHeadersOptions
|
|
{
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto,
|
|
RequireHeaderSymmetry = false
|
|
};
|
|
|
|
//
|
|
forwardOptions.KnownNetworks.Clear();
|
|
forwardOptions.KnownProxies.Clear();
|
|
|
|
//
|
|
// ref: https://github.com/aspnet/Docs/issues/2384
|
|
source.UseForwardedHeaders(forwardOptions);
|
|
}
|
|
}
|
|
}
|
|
|
|
### FILE: C:\Users\saherelm\Documents\Projects\xSaherelmWorkspace\xAiMcpServer\Tools\XEchoTools.cs
|
|
|
|
|
|
using xAiModels.Attributes;
|
|
|
|
namespace xAiMcpServer.Tools
|
|
{
|
|
[XAiMcpToolType]
|
|
public static class XEchoTools
|
|
{
|
|
[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}";
|
|
return result;
|
|
}
|
|
}
|
|
}
|