Skip to content

Snippets and results

A snippet is a file in any package except main that declares exactly this function:

func run(ctx context.Context) (any, error)

kode-gopher adds the main function. It calls run, encodes the returned value as JSON, and turns a returned error or a panic into a structured result. The snippet never writes main, and doesn’t print its answer: it returns it.

package snippet
import (
"context"
"os"
container "cloud.google.com/go/container/apiv1"
"cloud.google.com/go/container/apiv1/containerpb"
)
func run(ctx context.Context) (any, error) {
c, err := container.NewClusterManagerClient(ctx)
if err != nil {
return nil, err
}
defer c.Close()
resp, err := c.ListClusters(ctx, &containerpb.ListClustersRequest{
Parent: "projects/" + os.Getenv("GOOGLE_CLOUD_PROJECT") + "/locations/-",
})
if err != nil {
return nil, err
}
var names []string
for _, cl := range resp.Clusters {
names = append(names, cl.Name)
}
return names, nil
}

A file in package main with its own func main() runs as is. The program owns stdout and stderr. For a structured result, it writes the same JSON shape the wrapper writes to /app/.kode-gopher/result.json:

{"kind": "ok", "value": ...}

The files argument takes a map from path to source, instead of code:

  • Files at the root form the entry package. For a snippet, exactly one of them declares run.
  • Files in subdirectories are helper packages. The module is named kode_gopher_user, so helper/format.go is imported as kode_gopher_user/helper.
  • A go.mod in the map replaces the image’s lockfile. The snippet then chooses its own versions, and pays for any recompilation.

Packages in the precompiled set build in seconds. Any other pure-Go package works too: the build runs go mod tidy first, and the result reports tidied: true.

extra_imports adds blank imports through a generated file. It’s rarely needed, as a hint for go mod tidy.

Every call returns:

FieldMeaning
phasebuild if compilation failed, with the compiler’s errors in stderr. run if it compiled and ran.
modewrapped for a snippet, verbatim for a full program.
exit_codeThe compiler’s exit code (build) or the program’s (run).
stdout, stderrOutput of the phase that ran last. Snippets can log to stdout freely, since the answer travels separately.
build_ms, duration_msBuild time, and build plus run time.
tidiedWhether the build needed go mod tidy.
resultThe structured result, below.
attachmentsMetadata (name, mime_type, bytes) for any images written under /app/.kode-gopher/attachments/, returned alongside ImageContent blocks.

The result.kind is one of:

KindWhenOther fields
okrun returned a valuevalue: the value, as JSON
errorrun returned an errormessage
panicrun panickedmessage, stack
marshal_errorthe value couldn’t be encoded as JSON (a channel or a func, for example)message, type

A result can be up to 256 KiB of JSON. A larger one is dropped, and so is a result.json that isn’t valid JSON. In both cases warnings says why, so return less data: filter, summarize, or page through it in the program, project responses into small structs or slices rather than returning raw *pb structs or full Kubernetes list objects (whose metadata.managedFields and last-applied-configuration annotations quickly fill 256 KiB), or stash raw datasets under /app/persist for follow-up calls in the same session. stdout and stderr are different: each is cut to its first and last 8 KiB, so the model sees the start and the end of long output.

The MCP response is flagged as an error (isError) when the build failed, the program exited non-zero, the result kind isn’t ok, or the result was dropped. The model can tell what to fix from phase and kind: compile errors mean changing the code, and error results usually mean a permission or API problem.

When a snippet renders a chart or downloads an image from Cloud Storage, write regular image/png, image/jpeg, or image/webp files under /app/.kode-gopher/attachments/ instead of base64-encoding bytes into result.value. Each run accepts up to 8 images and 2 MiB of combined raw image bytes, verified by file header signature rather than file extension. execute_go_code appends each image to the MCP tool result as a native ImageContent block and lists its metadata in attachments; symlinks, subdirectories, unsupported formats, or oversized files are skipped and reported in warnings.

The full schema is on the MCP tools page.