Here's an excerpt of a plugin I made. (see the attached zip which contains a gdscript.) It may contain errors and many things you might not need or better do differently. But it perhaps gives a general idea how SurfaceTool can be used to generate meshes by code.
(As it is only part of the plugin, the script will not be directly runnable.)