clusters
Retrieve and manage a list of Kubernetes clusters using the OCM MCP Server, enabling centralized interaction and observability of multiple clusters.
Instructions
Retrieves a list of Kubernetes clusters (also known as managed clusters or spoke clusters).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/tools/clusters.ts:18-65 (handler)The handler function `listClusters` that lists and formats Kubernetes managed clusters using the Open Cluster Management API, populating a clusterToServerAPIMap.export async function listClusters({ }): Promise<CallToolResult> { const response = await client.list<k8s.KubernetesObject>("cluster.open-cluster-management.io/v1", "ManagedCluster") if (!response || response.items.length == 0) { console.warn("no managed clusters on the current cluster") return { content: [{ type: "text", text: "no managed clusters available on the current cluster" }], } } clusterToServerAPIMap = new Map( response.items.map((item: any) => { const name: string = item.metadata?.name; const server: string = item.spec?.managedClusterClientConfigs?.[0]?.url; return [name, server]; }) ); // Format table header let result = `NAME HUB ACCEPTED MANAGED CLUSTER URLS JOINED AVAILABLE AGE\n`; // Process each cluster and format the output response.items.forEach((item: any) => { const name: string = item.metadata?.name || "Unknown"; const hubAccepted: string = item.spec?.hubAcceptsClient ? "true" : "false"; const server: string = item.spec?.managedClusterClientConfigs?.[0]?.url || "N/A"; // Extract conditions const joinedCondition = item.status?.conditions?.find((c: any) => c.type === "ManagedClusterJoined")?.status || "False"; const availableCondition = item.status?.conditions?.find((c: any) => c.type === "ManagedClusterConditionAvailable")?.status || "False"; // Calculate cluster age const creationTimestamp = item.metadata?.creationTimestamp; const age = creationTimestamp ? getClusterAge(creationTimestamp) : "N/A"; // Append formatted row result += `${name.padEnd(10)} ${hubAccepted.padEnd(14)} ${server.padEnd(80)} ${joinedCondition.padEnd(8)} ${availableCondition.padEnd(10)} ${age}\n`; }); return { content: [{ type: "text", text: result }], } }
- src/index.ts:23-28 (registration)Registration of the "clusters" tool in the MCP server, wrapping the listClusters handler.server.tool( "clusters", listClusterDesc, listClustersArgs, // should be a Zod schema, e.g., z.object({...}) async (args, extra) => listClusters(args) // ensure listClusters matches (args, extra) => ... )
- src/tools/clusters.ts:16-16 (schema)Empty input schema for the "clusters" tool (no required arguments).export const listClustersArgs = {}
- src/tools/clusters.ts:15-15 (schema)Tool description used in registration.export const listClusterDesc = "Retrieves a list of Kubernetes clusters (also known as managed clusters or spoke clusters)."
- src/tools/clusters.ts:67-73 (helper)Helper function to calculate the age of a cluster in days, used in the handler output formatting.function getClusterAge(creationTimestamp: string): string { const createdDate = new Date(creationTimestamp); const now = new Date(); const diffMs = now.getTime() - createdDate.getTime(); const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24)); return `${diffDays}d`; }