Add Kubernetes and Jira MCP tools with auto-registration
This commit is contained in:
@@ -6,17 +6,14 @@ using ModelContextProtocol.Server;
|
||||
namespace LazyBear.MCP.Services.Kubernetes;
|
||||
|
||||
[McpServerToolType]
|
||||
public sealed class K8sNetworkTools(K8sClientProvider clientProvider, IConfiguration configuration)
|
||||
public sealed class K8sNetworkTools(K8sClientProvider clientProvider, IConfiguration configuration, ILogger<K8sNetworkTools>? logger = null) : KubernetesToolsBase(clientProvider, configuration, logger)
|
||||
{
|
||||
private readonly IKubernetes? _client = clientProvider.Client;
|
||||
private readonly string? _clientInitializationError = clientProvider.InitializationError;
|
||||
private readonly string _defaultNamespace = configuration["Kubernetes:DefaultNamespace"] ?? "default";
|
||||
|
||||
[McpServerTool, Description("Список service в namespace")]
|
||||
public async Task<string> ListServices(
|
||||
[Description("Namespace Kubernetes")] string? @namespace = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ValidateNamespace(@namespace ?? _defaultNamespace, nameof(@namespace));
|
||||
var ns = ResolveNamespace(@namespace);
|
||||
if (!TryGetClient(out var client, out var clientError))
|
||||
{
|
||||
@@ -58,6 +55,8 @@ public sealed class K8sNetworkTools(K8sClientProvider clientProvider, IConfigura
|
||||
[Description("Namespace Kubernetes")] string? @namespace = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ValidateResourceName(name, nameof(name));
|
||||
ValidateNamespace(@namespace ?? _defaultNamespace, nameof(@namespace));
|
||||
var ns = ResolveNamespace(@namespace);
|
||||
if (!TryGetClient(out var client, out var clientError))
|
||||
{
|
||||
@@ -91,6 +90,7 @@ public sealed class K8sNetworkTools(K8sClientProvider clientProvider, IConfigura
|
||||
[Description("Namespace Kubernetes")] string? @namespace = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ValidateNamespace(@namespace ?? _defaultNamespace, nameof(@namespace));
|
||||
var ns = ResolveNamespace(@namespace);
|
||||
if (!TryGetClient(out var client, out var clientError))
|
||||
{
|
||||
@@ -136,49 +136,4 @@ public sealed class K8sNetworkTools(K8sClientProvider clientProvider, IConfigura
|
||||
return FormatError("list_ingresses", ns, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private string ResolveNamespace(string? @namespace)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(@namespace) ? _defaultNamespace : @namespace;
|
||||
}
|
||||
|
||||
private static string BuildClientInitializationError()
|
||||
{
|
||||
return "Kubernetes клиент не инициализирован. Проверьте Kubernetes:KubeconfigPath или in-cluster окружение (KUBERNETES_SERVICE_HOST/KUBERNETES_SERVICE_PORT).";
|
||||
}
|
||||
|
||||
private bool TryGetClient(out IKubernetes client, out string error)
|
||||
{
|
||||
if (_client is null)
|
||||
{
|
||||
client = null!;
|
||||
var details = string.IsNullOrWhiteSpace(_clientInitializationError)
|
||||
? string.Empty
|
||||
: $" Детали: {_clientInitializationError}";
|
||||
error = BuildClientInitializationError() + details;
|
||||
return false;
|
||||
}
|
||||
|
||||
client = _client;
|
||||
error = string.Empty;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static string FormatError(string toolName, string @namespace, Exception exception, string? resourceName = null)
|
||||
{
|
||||
var resourcePart = string.IsNullOrWhiteSpace(resourceName) ? string.Empty : $", resource='{resourceName}'";
|
||||
|
||||
if (exception is HttpOperationException httpException)
|
||||
{
|
||||
var statusCode = httpException.Response?.StatusCode;
|
||||
var reason = httpException.Response?.ReasonPhrase;
|
||||
var body = string.IsNullOrWhiteSpace(httpException.Response?.Content)
|
||||
? "-"
|
||||
: httpException.Response!.Content;
|
||||
|
||||
return $"Ошибка Kubernetes в tool '{toolName}' (namespace='{@namespace}'{resourcePart}): status={(int?)statusCode ?? 0} {reason}, details={body}";
|
||||
}
|
||||
|
||||
return $"Ошибка Kubernetes в tool '{toolName}' (namespace='{@namespace}'{resourcePart}): {exception.GetType().Name}: {exception.Message}";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user