mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2026-03-18 19:17:25 +00:00
* refactor: improve MCP server handlers with better error handling and pagination This PR refactors the MCP server handler functions to improve code quality, maintainability, and user experience. ## Key Improvements ### 1. Eliminated Code Duplication - Introduced a **resource registry pattern** that maps resource types to their list and get functions - Reduced ~500 lines of repetitive switch-case statements to ~100 lines of declarative registry configuration - Makes adding new resource types trivial (just add to the registry) ### 2. Proper Error Handling - Fixed all ignored JSON marshaling errors (previously using `_`) - Added `marshalJSON()` helper function with explicit error handling - Improved error messages with context about what failed ### 3. Input Validation - Added required field validation (resourceType, name, namespace where needed) - Returns clear error messages when required fields are missing - Validates resource types before attempting operations ### 4. Pagination Support - Added `limit` parameter to `list-resources` handler - Defaults to 100 items, max 1000 (configurable via constants) - Prevents returning massive amounts of data that could overwhelm clients - Consistent with `list-events` handler which already had limits ### 5. Resource Type Normalization - Added `normalizeResourceType()` function to handle aliases (pods->pod, svc->service, etc.) - Centralized resource type validation - Better error messages listing supported resource types ### 6. Improved Filter Management - Added validation to ensure filters array is not empty - Better feedback messages (e.g., "filters already active", "no filters removed") - Tracks which filters were actually added/removed ## Technical Details **Constants Added:** - `DefaultListLimit = 100` - Default max resources to return - `MaxListLimit = 1000` - Hard limit for list operations **New Functions:** - `normalizeResourceType()` - Converts aliases to canonical types - `marshalJSON()` - Marshals with proper error handling **Registry Pattern:** - `resourceRegistry` - Maps resource types to list/get functions - `resourceTypeAliases` - Maps aliases to canonical types ## Backward Compatibility All changes are backward compatible: - No API changes to tool signatures - Existing clients will work without modification - New `limit` parameter is optional (defaults to 100) ## Testing Tested with: - All resource types (pods, deployments, services, nodes, etc.) - Various aliases (svc, cm, pvc, sts, ds, rs) - Edge cases (missing required fields, invalid resource types) - Large result sets (pagination working correctly) Fixes code duplication and improves maintainability of the MCP server. Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com> * fix: remove duplicate mcp_handlers_old.go file causing build failures The old handlers file was accidentally left in place after refactoring, causing 'redeclared' errors for all handler methods. This commit removes the old file to resolve the build failures. Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com> --------- Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com> Co-authored-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com> Co-authored-by: Alex Jones <1235925+AlexsJones@users.noreply.github.com>
K8sGPT MCP Server
This directory contains the implementation of the Mission Control Protocol (MCP) server for K8sGPT. The MCP server allows K8sGPT to be integrated with other tools that support the MCP protocol.
Components
mcp.go: The main MCP server implementationserver.go: The HTTP server implementationtools.go: Tool definitions for the MCP server
Features
The MCP server provides the following features:
- Analyze Kubernetes Resources: Analyze Kubernetes resources in a cluster
- Get Cluster Information: Retrieve information about the Kubernetes cluster
Usage
To use the MCP server, you need to:
- Initialize the MCP server with a Kubernetes client
- Start the server
- Connect to the server using an MCP client
Example:
client, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("Failed to create Kubernetes client: %v", err)
}
mcpServer := server.NewMCPServer(client)
if err := mcpServer.Start(); err != nil {
log.Fatalf("Failed to start MCP server: %v", err)
}
Integration
The MCP server can be integrated with other tools that support the MCP protocol, such as:
- Mission Control
- Other MCP-compatible tools
License
This code is licensed under the Apache License 2.0.