Add Kubernetes and Jira MCP tools with auto-registration
This commit is contained in:
@@ -6,17 +6,17 @@ using ModelContextProtocol.Server;
|
||||
namespace LazyBear.MCP.Services.Kubernetes;
|
||||
|
||||
[McpServerToolType]
|
||||
public sealed class K8sPodsTools(K8sClientProvider clientProvider, IConfiguration configuration)
|
||||
public sealed class K8sPodsTools(K8sClientProvider clientProvider, IConfiguration configuration, ILogger<K8sPodsTools>? 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";
|
||||
private const int MaxTailLines = 10;
|
||||
private const int MinTailLines = 10;
|
||||
|
||||
[McpServerTool, Description("Список подов в namespace")]
|
||||
public async Task<string> ListPods(
|
||||
[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))
|
||||
{
|
||||
@@ -55,6 +55,8 @@ public sealed class K8sPodsTools(K8sClientProvider clientProvider, IConfiguratio
|
||||
[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))
|
||||
{
|
||||
@@ -88,6 +90,18 @@ public sealed class K8sPodsTools(K8sClientProvider clientProvider, IConfiguratio
|
||||
int? tailLines = 100,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ValidateResourceName(name, nameof(name));
|
||||
ValidateNamespace(@namespace ?? _defaultNamespace, nameof(@namespace));
|
||||
|
||||
if (tailLines < MinTailLines)
|
||||
{
|
||||
tailLines = MinTailLines;
|
||||
}
|
||||
if (tailLines > MaxTailLines)
|
||||
{
|
||||
tailLines = MaxTailLines;
|
||||
}
|
||||
|
||||
var ns = ResolveNamespace(@namespace);
|
||||
if (!TryGetClient(out var client, out var clientError))
|
||||
{
|
||||
@@ -100,7 +114,7 @@ public sealed class K8sPodsTools(K8sClientProvider clientProvider, IConfiguratio
|
||||
name,
|
||||
ns,
|
||||
container: container,
|
||||
tailLines: tailLines,
|
||||
tailLines: (int?)tailLines,
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
using var reader = new StreamReader(logStream);
|
||||
@@ -118,49 +132,4 @@ public sealed class K8sPodsTools(K8sClientProvider clientProvider, IConfiguratio
|
||||
return FormatError("get_pod_logs", ns, ex, name);
|
||||
}
|
||||
}
|
||||
|
||||
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