in mcp.go [63:126]
func (s *kubectlMCPServer) handleToolCall(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log := klog.FromContext(ctx)
name := request.Params.Name
command := request.Params.Arguments["command"].(string)
modifiesResource := request.Params.Arguments["modifies_resource"].(string)
log.Info("Received tool call", "tool", name, "command", command, "modifies_resource", modifiesResource)
ctx = context.WithValue(ctx, "kubeconfig", s.kubectlConfig)
ctx = context.WithValue(ctx, "work_dir", s.workDir)
tool := tools.Lookup(name)
if tool == nil {
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("Error: Tool %s not found", name),
},
},
}, nil
}
output, err := tool.Run(ctx, map[string]any{
"command": command,
})
if err != nil {
log.Error(err, "Error running tool call")
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("Error: %v", err),
},
},
IsError: true,
}, nil
}
result, err := tools.ToolResultToMap(output)
if err != nil {
log.Error(err, "Error converting tool call output to result")
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("Error: %v", err),
},
},
IsError: true,
}, nil
}
log.Info("Tool call output", "tool", name, "result", result)
return &mcp.CallToolResult{
Content: []mcp.Content{
mcp.TextContent{
Type: "text",
Text: fmt.Sprintf("%v", result),
},
},
}, nil
}