Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,11 @@ In case multi-cluster support is enabled (default) and you have access to multip

<summary>kiali</summary>

- **kiali_graph** - Check the status of my mesh by querying Kiali graph
- **kiali_get_mesh_graph** - Returns the topology of a specific namespaces, health, status of the mesh and namespaces. Use this for high-level overviews
- `graphType` (`string`) - Type of graph to return: 'versionedApp', 'app', 'service', 'workload', 'mesh'. Default: 'versionedApp'
- `namespace` (`string`) - Optional single namespace to include in the graph (alternative to namespaces)
- `namespaces` (`string`) - Optional comma-separated list of namespaces to include in the graph

- **kiali_mesh_status** - Get the status of mesh components including Istio, Kiali, Grafana, Prometheus and their interactions, versions, and health status
- `rateInterval` (`string`) - Rate interval for fetching (e.g., '10m', '5m', '1h'). Default: '60s'

- **kiali_istio_config** - Get all Istio configuration objects in the mesh including their full YAML resources and details

Expand Down Expand Up @@ -389,8 +389,6 @@ In case multi-cluster support is enabled (default) and you have access to multip
- `namespace` (`string`) - Optional single namespace to retrieve validations from (alternative to namespaces)
- `namespaces` (`string`) - Optional comma-separated list of namespaces to retrieve validations from

- **kiali_namespaces** - Get all namespaces in the mesh that the user has access to

- **kiali_services_list** - Get all services in the mesh across specified namespaces with health and Istio resource information
- `namespaces` (`string`) - Comma-separated list of namespaces to get services from (e.g. 'bookinfo' or 'bookinfo,default'). If not provided, will list services from all accessible namespaces

Expand Down Expand Up @@ -429,12 +427,6 @@ In case multi-cluster support is enabled (default) and you have access to multip
- `step` (`string`) - Step between data points in seconds (e.g., '15'). Optional, defaults to 15 seconds
- `workload` (`string`) **(required)** - Name of the workload to get metrics for

- **kiali_health** - Get health status for apps, workloads, and services across specified namespaces in the mesh. Returns health information including error rates and status for the requested resource type
- `namespaces` (`string`) - Comma-separated list of namespaces to get health from (e.g. 'bookinfo' or 'bookinfo,default'). If not provided, returns health for all accessible namespaces
- `queryTime` (`string`) - Unix timestamp (in seconds) for the prometheus query. If not provided, uses current time. Optional
- `rateInterval` (`string`) - Rate interval for fetching error rate (e.g., '10m', '5m', '1h'). Default: '10m'
- `type` (`string`) - Type of health to retrieve: 'app', 'service', or 'workload'. Default: 'app'

- **workload_logs** - Get logs for a specific workload's pods in a namespace. Only requires namespace and workload name - automatically discovers pods and containers. Optionally filter by container name, time range, and other parameters. Container is auto-detected if not specified.
- `container` (`string`) - Optional container name to filter logs. If not provided, automatically detects and uses the main application container (excludes istio-proxy and istio-init)
- `namespace` (`string`) **(required)** - Namespace containing the workload
Expand Down Expand Up @@ -488,4 +480,4 @@ Compile the project and run the Kubernetes MCP server with [mcp-inspector](https
make build
# Run the Kubernetes MCP server with mcp-inspector
npx @modelcontextprotocol/inspector@latest $(pwd)/kubernetes-mcp-server
```
```
133 changes: 133 additions & 0 deletions pkg/kiali/get_mesh_graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package kiali

import (
"context"
"encoding/json"
"strings"
"sync"
)

type GetMeshGraphResponse struct {
Graph json.RawMessage `json:"graph,omitempty"`
Health json.RawMessage `json:"health,omitempty"`
MeshStatus json.RawMessage `json:"mesh_status,omitempty"`
Namespaces json.RawMessage `json:"namespaces,omitempty"`
Errors map[string]string `json:"errors,omitempty"`
}

// GetMeshGraph fetches multiple Kiali endpoints in parallel and returns a combined response.
// Each field in the response corresponds to one API call result.
// - graph: /api/namespaces/graph (optionally filtered by namespaces)
// - health: /api/clusters/health (optionally filtered by namespaces and queryParams)
// - status(mesh):/api/mesh/graph
// - namespaces: /api/namespaces
func (k *Kiali) GetMeshGraph(ctx context.Context, namespaces []string, queryParams map[string]string) (string, error) {
cleaned := make([]string, 0, len(namespaces))
for _, ns := range namespaces {
ns = strings.TrimSpace(ns)
if ns != "" {
cleaned = append(cleaned, ns)
}
}

resp := GetMeshGraphResponse{
Errors: make(map[string]string),
}

var wg sync.WaitGroup
wg.Add(4)

// Graph
go func() {
defer wg.Done()
data, err := k.getGraph(ctx, cleaned, queryParams)
if err != nil {
resp.Errors["graph"] = err.Error()
return
}
resp.Graph = data
}()

// Health
go func() {
defer wg.Done()
data, err := k.getHealth(ctx, cleaned, queryParams)
if err != nil {
resp.Errors["health"] = err.Error()
return
}
resp.Health = data
}()

// Mesh status
go func() {
defer wg.Done()
data, err := k.getMeshStatus(ctx)
if err != nil {
resp.Errors["mesh_status"] = err.Error()
return
}
resp.MeshStatus = data
}()

// Namespaces
go func() {
defer wg.Done()
data, err := k.getNamespaces(ctx)
if err != nil {
resp.Errors["namespaces"] = err.Error()
return
}
resp.Namespaces = data
}()

wg.Wait()

// If no errors occurred, omit the errors map in the final JSON
if len(resp.Errors) == 0 {
resp.Errors = nil
}

encoded, err := json.Marshal(resp)
if err != nil {
return "", err
}
return string(encoded), nil
}

// getGraph wraps the Graph call and returns raw JSON.
func (k *Kiali) getGraph(ctx context.Context, namespaces []string, queryParams map[string]string) (json.RawMessage, error) {
out, err := k.Graph(ctx, namespaces, queryParams)
if err != nil {
return nil, err
}
return json.RawMessage([]byte(out)), nil
}

// getHealth wraps the Health call and returns raw JSON.
func (k *Kiali) getHealth(ctx context.Context, namespaces []string, queryParams map[string]string) (json.RawMessage, error) {
nsParam := strings.Join(namespaces, ",")
out, err := k.Health(ctx, nsParam, queryParams)
if err != nil {
return nil, err
}
return json.RawMessage([]byte(out)), nil
}

// getMeshStatus wraps the MeshStatus call and returns raw JSON.
func (k *Kiali) getMeshStatus(ctx context.Context) (json.RawMessage, error) {
out, err := k.MeshStatus(ctx)
if err != nil {
return nil, err
}
return json.RawMessage([]byte(out)), nil
}

// getNamespaces wraps the ListNamespaces call and returns raw JSON.
func (k *Kiali) getNamespaces(ctx context.Context) (json.RawMessage, error) {
out, err := k.ListNamespaces(ctx)
if err != nil {
return nil, err
}
return json.RawMessage([]byte(out)), nil
}
15 changes: 12 additions & 3 deletions pkg/kiali/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ import (
// Graph calls the Kiali graph API using the provided Authorization header value.
// `namespaces` may contain zero, one or many namespaces. If empty, the API may return an empty graph
// or the server default, depending on Kiali configuration.
func (k *Kiali) Graph(ctx context.Context, namespaces []string) (string, error) {
func (k *Kiali) Graph(ctx context.Context, namespaces []string, queryParams map[string]string) (string, error) {
u, err := url.Parse(GraphEndpoint)
if err != nil {
return "", err
}
q := u.Query()
// Static graph parameters per requirements
q.Set("duration", "60s")
q.Set("graphType", "versionedApp")
// Defaults with optional overrides via queryParams
duration := "60s"
graphType := "versionedApp"
if v, ok := queryParams["rateInterval"]; ok && strings.TrimSpace(v) != "" {
duration = strings.TrimSpace(v)
}
if v, ok := queryParams["graphType"]; ok && strings.TrimSpace(v) != "" {
graphType = strings.TrimSpace(v)
}
q.Set("duration", duration)
q.Set("graphType", graphType)
q.Set("includeIdleEdges", "false")
q.Set("injectServiceNodes", "true")
q.Set("boxBy", "cluster,namespace,app")
Expand Down
15 changes: 14 additions & 1 deletion pkg/kiali/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"
"net/http"
"net/url"
"strings"
)

// Health returns health status for apps, workloads, and services across namespaces.
// Parameters:
// - namespaces: comma-separated list of namespaces (optional, if empty returns health for all accessible namespaces)
// - queryParams: optional query parameters map for filtering health data (e.g., "type", "rateInterval", "queryTime")
// - type: health type - "app", "service", or "workload" (default: "app")
// - rateInterval: rate interval for fetching error rate (default: "10m")
// - rateInterval: rate interval for fetching error rate (default: "1m")
// - queryTime: Unix timestamp for the prometheus query (optional)
func (k *Kiali) Health(ctx context.Context, namespaces string, queryParams map[string]string) (string, error) {
// Build query parameters
Expand All @@ -33,6 +34,18 @@ func (k *Kiali) Health(ctx context.Context, namespaces string, queryParams map[s
}
}

// Ensure health "type" aligns with graphType (versionedApp -> app)
healthType := "app"
if gt, ok := queryParams["graphType"]; ok && strings.TrimSpace(gt) != "" {
v := strings.TrimSpace(gt)
if strings.EqualFold(v, "versionedApp") {
healthType = "app"
} else {
healthType = v
}
}
q.Set("type", healthType)

u.RawQuery = q.Encode()
endpoint := u.String()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/containers/kubernetes-mcp-server/pkg/api"
)

func initGraph() []api.ServerTool {
func GetMeshGraph() []api.ServerTool {
ret := make([]api.ServerTool, 0)
ret = append(ret, api.ServerTool{
Tool: api.Tool{
Name: "kiali_graph",
Description: "Check the status of my mesh by querying Kiali graph",
Name: "kiali_get_mesh_graph",
Description: "Returns the topology of a specific namespaces, health, status of the mesh and namespaces. Use this for high-level overviews",
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
Expand All @@ -27,22 +27,30 @@ func initGraph() []api.ServerTool {
Type: "string",
Description: "Optional comma-separated list of namespaces to include in the graph",
},
"rateInterval": {
Type: "string",
Description: "Rate interval for fetching (e.g., '10m', '5m', '1h'). Default: '60s'",
},
"graphType": {
Type: "string",
Description: "Type of graph to return: 'versionedApp', 'app', 'service', 'workload', 'mesh'. Default: 'versionedApp'",
},
},
Required: []string{},
},
Annotations: api.ToolAnnotations{
Title: "Graph: Mesh status",
Title: "Topology: Mesh, Graph, Health, and Status",
ReadOnlyHint: ptr.To(true),
DestructiveHint: ptr.To(false),
IdempotentHint: ptr.To(false),
OpenWorldHint: ptr.To(true),
},
}, Handler: graphHandler,
}, Handler: getMeshGraphHandler,
})
return ret
}

func graphHandler(params api.ToolHandlerParams) (*api.ToolCallResult, error) {
func getMeshGraphHandler(params api.ToolHandlerParams) (*api.ToolCallResult, error) {

// Parse arguments: allow either `namespace` or `namespaces` (comma-separated string)
namespaces := make([]string, 0)
Expand Down Expand Up @@ -77,8 +85,17 @@ func graphHandler(params api.ToolHandlerParams) (*api.ToolCallResult, error) {
}
namespaces = unique
}

// Extract optional query parameters
queryParams := make(map[string]string)
if rateInterval, ok := params.GetArguments()["rateInterval"].(string); ok && rateInterval != "" {
queryParams["rateInterval"] = rateInterval
}
if graphType, ok := params.GetArguments()["graph_type"].(string); ok && graphType != "" {
queryParams["graphType"] = graphType
}
k := params.NewKiali()
content, err := k.Graph(params.Context, namespaces)
content, err := k.GetMeshGraph(params.Context, namespaces, queryParams)
if err != nil {
return api.NewToolCallResult("", fmt.Errorf("failed to retrieve mesh graph: %v", err)), nil
}
Expand Down
80 changes: 0 additions & 80 deletions pkg/toolsets/kiali/health.go

This file was deleted.

Loading