Skip to content

Commit 096e2e1

Browse files
committed
feat: add MCP prompts support
Add MCP prompts support to enable pre-defined workflow templates and guidance for AI assistants. Includes: - New PromptLoader for loading prompts from YAML files or embedded filesystems - Core prompt types (ServerPrompt, Prompt, PromptArgument, PromptMessage) - Template argument substitution with {{variable}} syntax - Required argument validation - built-in prompts for common Kubernetes workflows (troubleshooting, deployment, scaling, cluster health, networking, resource usage) - Integration with MCP server to register and serve prompts Signed-off-by: Nader Ziada <[email protected]>
1 parent 8d76426 commit 096e2e1

18 files changed

+1486
-6
lines changed

docs/PROMPTS.md

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
# MCP Prompts Support
2+
3+
The Kubernetes MCP Server now supports [MCP Prompts](https://modelcontextprotocol.io/docs/concepts/prompts), which provide pre-defined workflow templates and guidance to AI assistants. Prompts help standardize common Kubernetes operations and provide structured approaches to troubleshooting and deployment tasks.
4+
5+
## What are MCP Prompts?
6+
7+
MCP Prompts are pre-defined templates that guide AI assistants through specific workflows. They combine:
8+
- **Structured guidance**: Step-by-step instructions for common tasks
9+
- **Parameterization**: Arguments that customize the prompt for specific contexts
10+
- **Conversation templates**: Pre-formatted messages that guide the interaction
11+
12+
## Available Prompts
13+
14+
The Kubernetes MCP Server currently provides the following prompts in the `core` toolset:
15+
16+
### 1. `troubleshoot-pod`
17+
Guide for troubleshooting a failing or crashed pod in Kubernetes.
18+
19+
**Arguments:**
20+
- `namespace` (required): The namespace where the pod is located
21+
- `pod_name` (required): The name of the pod to troubleshoot
22+
23+
**Usage:**
24+
```
25+
When to use: Pod is failing, crashing, or not working as expected
26+
What it does: Systematically investigates pod status, events, logs, and resource constraints
27+
```
28+
29+
### 2. `deploy-application`
30+
Workflow for deploying a new application to Kubernetes.
31+
32+
**Arguments:**
33+
- `app_name` (required): The name of the application to deploy
34+
- `namespace` (optional): The namespace to deploy to (defaults to 'default')
35+
36+
**Usage:**
37+
```
38+
When to use: Deploying a new application to the cluster
39+
What it does: Guides through namespace setup, manifest creation, deployment, and verification
40+
```
41+
42+
### 3. `scale-deployment`
43+
Guide for scaling a deployment up or down.
44+
45+
**Arguments:**
46+
- `deployment_name` (required): The name of the deployment to scale
47+
- `namespace` (required): The namespace of the deployment
48+
- `replicas` (required): The desired number of replicas
49+
50+
**Usage:**
51+
```
52+
When to use: Need to scale application replicas
53+
What it does: Checks current status, scales deployment, and monitors the scaling process
54+
```
55+
56+
### 4. `investigate-cluster-health`
57+
Comprehensive workflow for investigating overall cluster health.
58+
59+
**Arguments:** None
60+
61+
**Usage:**
62+
```
63+
When to use: Want to understand overall cluster status and health
64+
What it does: Checks nodes, system pods, events, resource usage, and namespaces
65+
```
66+
67+
### 5. `debug-networking`
68+
Workflow for debugging networking issues between pods or services.
69+
70+
**Arguments:**
71+
- `source_pod` (optional): The source pod name
72+
- `source_namespace` (optional): The source pod namespace
73+
- `target_service` (optional): The target service name
74+
75+
**Usage:**
76+
```
77+
When to use: Experiencing connectivity or networking problems
78+
What it does: Investigates pod IPs, service configuration, network policies, DNS, and connectivity
79+
```
80+
81+
### 6. `review-resource-usage`
82+
Analyze resource usage across the cluster or specific namespace.
83+
84+
**Arguments:**
85+
- `namespace` (optional): Focus on specific namespace (leave empty for cluster-wide)
86+
87+
**Usage:**
88+
```
89+
When to use: Need to understand resource consumption and identify bottlenecks
90+
What it does: Analyzes CPU and memory usage, identifies top consumers, and provides recommendations
91+
```
92+
93+
## Creating Custom Prompts
94+
95+
Toolsets can provide their own prompts by implementing the `GetPrompts()` method and creating YAML definition files.
96+
97+
### YAML Prompt Definition Format
98+
99+
Prompts are defined in YAML files with the following structure:
100+
101+
```yaml
102+
- name: prompt-name
103+
description: Human-readable description of what this prompt does
104+
arguments:
105+
- name: argument_name
106+
description: What this argument is for
107+
required: true/false
108+
messages:
109+
- role: user
110+
content: |
111+
The user's initial message with {{argument_name}} placeholders
112+
- role: assistant
113+
content: |
114+
The assistant's response template
115+
```
116+
117+
### Example: Creating a Custom Prompt
118+
119+
**Step 1: Create a YAML file** (`pkg/toolsets/yourname/prompts.yaml`):
120+
121+
```yaml
122+
- name: check-pod-logs
123+
description: Retrieve and analyze logs from a specific pod
124+
arguments:
125+
- name: pod_name
126+
description: The name of the pod to check
127+
required: true
128+
- name: namespace
129+
description: The namespace of the pod
130+
required: false
131+
messages:
132+
- role: user
133+
content: |
134+
I need to check the logs for pod {{pod_name}}{{#namespace}} in namespace {{namespace}}{{/namespace}}.
135+
- role: assistant
136+
content: |
137+
I'll retrieve the logs for {{pod_name}}. Let me:
138+
1. Verify the pod exists and its status
139+
2. Retrieve the latest logs
140+
3. Analyze for errors or warnings
141+
```
142+
143+
**Step 2: Embed the YAML file** in your Go code:
144+
145+
```go
146+
package yourname
147+
148+
import (
149+
_ "embed"
150+
"github.com/containers/kubernetes-mcp-server/pkg/api"
151+
)
152+
153+
//go:embed prompts.yaml
154+
var promptsYAML []byte
155+
156+
func initPrompts() []api.ServerPrompt {
157+
loader := api.NewPromptLoader()
158+
if err := loader.LoadFromBytes(promptsYAML); err != nil {
159+
return nil
160+
}
161+
return loader.GetServerPrompts()
162+
}
163+
```
164+
165+
**Step 3: Implement `GetPrompts()`** in your toolset:
166+
167+
```go
168+
func (t *Toolset) GetPrompts(_ internalk8s.Openshift) []api.ServerPrompt {
169+
return initPrompts()
170+
}
171+
```
172+
173+
### Argument Substitution
174+
175+
Prompts support template variable substitution using `{{argument_name}}` syntax:
176+
177+
- `{{argument_name}}` - Replaced with the argument value
178+
- Simple string replacement is performed automatically by the prompt loader
179+
180+
### Advanced: Custom Prompt Handlers
181+
182+
For more complex prompt logic, you can create custom handlers instead of using YAML:
183+
184+
```go
185+
func customPromptHandler(params api.PromptHandlerParams) (*api.PromptCallResult, error) {
186+
args := params.GetArguments()
187+
188+
// Custom logic here
189+
namespace := args["namespace"]
190+
podName := args["pod_name"]
191+
192+
// Build dynamic messages
193+
messages := []api.PromptMessage{
194+
{
195+
Role: "user",
196+
Content: api.PromptContent{
197+
Type: "text",
198+
Text: fmt.Sprintf("Check pod %s in namespace %s", podName, namespace),
199+
},
200+
},
201+
{
202+
Role: "assistant",
203+
Content: api.PromptContent{
204+
Type: "text",
205+
Text: "I'll check the pod status...",
206+
},
207+
},
208+
}
209+
210+
return api.NewPromptCallResult("Custom prompt", messages, nil), nil
211+
}
212+
```
213+
214+
## Using Prompts with AI Assistants
215+
216+
When using the Kubernetes MCP Server with an AI assistant (like Claude):
217+
218+
1. **List available prompts**: The assistant can discover available prompts via the MCP protocol
219+
2. **Invoke a prompt**: The assistant can call a prompt with specific arguments
220+
3. **Follow the workflow**: The prompt provides structured guidance for the task
221+
222+
### Example Interaction
223+
224+
```
225+
User: "My pod is crashing, can you help?"

0 commit comments

Comments
 (0)