ai newsMi., 21. Okt.
EilmeldungQwen3 auf AWS Trainium trainieren: Drei Wege mit torchtitan

How to Build Zero-Dependency Single-Binary CLI Tools with AI Agents: A Deterministic Go Scaffolder MCP Server

How to Build Zero-Dependency Single-Binary CLI Tools with AI Agents: A Deterministic Go Scaffolder MCP Server Autonomous coding swarms ( Claude Desktop , Cursor , Devin , Roo-Code ) are increasingly tasked with generating backend utilities, system micro-tools, and automation CLIs. However, when age

dev.toMittwoch 20:04 · 21.10.5 Min. Lesezeit
Bild: dev.to

Autonom arbeitende Coding-Swarms (Claude Desktop, Cursor, Devin, Roo-Code) werden zunehmend damit beauftragt, Backend-Utilities, System-Micro-Tools und Automatisierungs-CLIs zu generieren.

Wenn Agents jedoch standardmaessig auf Python oder Node.js zurueckgreifen, verschlechtert sich die Developer Experience rapide:

In diesem Tutorial betrachten wir GoCLIForge MCP, einen Open-Source- und Enterprise-Model-Context-Protocol-(MCP)-Server, der AI-Agents befuehigt, null-abhaengige Go-CLI-Tools als Single Binary zu generieren, zu validieren, zu cross-kompilieren und zu verteilen, mit einer Ausfuehrungszeit im Sub-Millisekunden-Bereich.

Gos Standardbibliothek enthaelt flag.FlagSet, das isoliertes, thread-sicheres Parsen von Subcommand-Argumenten ermoeglicht, ohne externe Pakete importieren zu muessen.

┌────────────────────────────────────────────────────────┐ │ AI Coding Agents │ │ (Claude Desktop, Cursor, Devin, Roo-Code) │ └───────────────────────────▲────────────────────────────┘ │ JSON-RPC 2.0 (stdio) ┌───────────────────────────▼────────────────────────────┐ │ GoCLIForge Pro MCP Engine │ ├───────────────────────────┬────────────────────────────┤ │ 1. Zero-Dep CLI Scaffolder│ 4. Binary Symbol Optimizer │ │ (flag.FlagSet POSIX) │ (strip, dead-code elim) │ ├───────────────────────────┼────────────────────────────┤ │ 2.

Cross-Compiler Matrix │ 5. Embedded Asset Engine │ │ (Linux, Darwin, Windows│ (//go:embed SQLite/docs)│ ├───────────────────────────┼────────────────────────────┤ │ 3. Checksum & Tarballer │ 6.

GitHub Actions Workflow │ │ (SHA-256 manifest gen) │ (Automated multi-arch CI│ └───────────────────────────┴────────────────────────────┘ Enter fullscreen mode Exit fullscreen mode 1. Zero-Dependency Subcommand Routing Hier ist das saubere, idiomatische Muster, das GoCLIForge fuer modulare Subcommands generiert:

package main import ( "flag" "fmt" "os" ) func ExecuteRun ( args [] string ) int { fs:= flag. NewFlagSet ( "run", flag. ExitOnError ) fs. Usage = func () { fmt. Printf ( "Usage: myapp run [options] \n\n Options: \n " ) fs. PrintDefaults () } inputFile:= fs. String ( "file", "", "Path to input file" ) verbose:= fs. Bool ( "verbose", false, "Enable verbose logging" ) if err:= fs. Parse ( args ); err!= nil { fmt. Fprintf ( os. Stderr, "Error parsing flags: %v \n ", err ) return 1 } fmt. Printf ( "[OK] Running task with file=%s, verbose=%v \n ", * inputFile, * verbose ) return 0 } Enter fullscreen mode Exit fullscreen mode Und der primaere Dispatcher in main.go:

package main import ( "fmt" "os" ) var version = "1.0.0" func main () { args:= os. Args [ 1: ] if len ( args ) == 0 { fmt. Println ( "Usage: myapp <command> [arguments]" ) os.

Exit ( 0 ) } switch args [ 0 ] { case "-v", "--version", "version": fmt. Printf ( "myapp version %s \n ", version ) case "run": os. Exit ( ExecuteRun ( args [ 1: ])) default: fmt.

Fprintf ( os. Stderr, "Unknown command: %s \n ", args [ 0 ]) os. Exit ( 1 ) } } Enter fullscreen mode Exit fullscreen mode Die wichtigsten Vorteile: Keine Third-Party-Imports: Das gesamte Tool laesst sich mit go build in unter einer Sekunde kompilieren.

Statische Binary: Auf dem Rechner des Nutzers werden weder Go, libc, Python noch Node.js benoetigt. POSIX-konform: Akzeptiert die uebliche Syntax --flag=value und -flag value. 2.

Einbetten statischer Assets mit //go:embed Oft benoetigt ein CLI-Tool Templates, Dokumentation oder ein eingebettetes Datenbankschema. Gos natives embed-Paket packt Dateien direkt in die kompilierte Binary:

package assets import ( "embed" "fmt" ) //go:embed * var EmbeddedFS embed. FS // ReadAsset reads a file from the embedded filesystem func ReadAsset ( name string ) ([] byte, error ) { data, err:= EmbeddedFS. ReadFile ( name ) if err!= nil { return nil, fmt. Errorf ( "embedded asset %s not found: %w", name, err ) } return data, nil } Enter fullscreen mode Exit fullscreen mode 3. Deterministische Cross-Compilation-Matrix Go verfuegt ab Werk ueber native Cross-Compilation in der Toolchain. Durch Setzen von CGO_ENABLED=0, GOOS und GOARCH erzeugt Go eigenstaendige statische Executables fuer alle wichtigen Betriebssysteme:

# Linux x86_64 CGO_ENABLED = 0 GOOS = linux GOARCH = amd64 go build -trimpath -ldflags = "-s -w" -o dist/mycli-linux-amd64. # Linux ARM64 (Raspberry Pi, AWS Graviton) CGO_ENABLED = 0 GOOS = linux GOARCH = arm64 go build -trimpath -ldflags = "-s -w" -o dist/mycli-linux-arm64. # macOS Apple Silicon (M1/M2/M3/M4) CGO_ENABLED = 0 GOOS = darwin GOARCH = arm64 go build -trimpath -ldflags = "-s -w" -o dist/mycli-darwin-arm64. # Windows x86_64 CGO_ENABLED = 0 GOOS = windows GOARCH = amd64 go build -trimpath -ldflags = "-s -w" -o dist/mycli-windows-amd64.exe. Enter fullscreen mode Exit fullscreen mode Warum -ldflags="-s -w"? -s: Entfernt die Symboltabelle. -w: Entfernt DWARF-Debuginformationen. Ergebnis: Reduziert die Binary-Groesse um 35 bis 50 Prozent, ohne die Laufzeitleistung zu beeitraechtigen.

4. Einbindung von Claude Desktop und Cursor Die Integration von GoCLIForge MCP in die lokale Agent-Umgebung dauert weniger als 30 Sekunden.

{ "mcpServers": { "gocliforge": { "command": "python3", "args": [ "-m", "gocliforge.cli", "--stdio" ] } } } Enter fullscreen mode Exit fullscreen mode In Cursor (.cursor/mcp.json ): { "mcpServers": { "gocliforge": { "command": "python3", "args": [ "-m", "gocliforge.cli", "--stdio" ] } } } Enter fullscreen mode Exit fullscreen mode 5. Live-Agent-Demo-Prompts Sobald die Verbindung steht, koennen Ihre AI-Agents automatisierte Tooling-Aufgaben ausfuehren:

"Erstelle das Grundgeruest einer null-abhaengigen Go-CLI namens 'logpipe' mit den Subcommands 'tail' (Flags: --file, --follow) und 'filter' (Flags: --pattern, --regex)."

"Validiere die Struktur unserer Go-Codebasis und pruefe, dass keinerlei Third-Party-Abhaengigkeiten eingefuehrt wurden."

"Kompiliere statische Release-Binaries fuer Linux amd64, macOS Apple Silicon und Windows fuer dieses Projekt, mit entfernten Symbolen."

"Erzeuge eine GitHub-Actions-Release-Pipeline, die alle 5 Ziel-Binaries kompiliert und sie mit SHA-256-Checksummen veroeffentlicht."

Open-Source (Lite): Umfasst Projekt-Scaffolding, Generierung auf Basis der Standardbibliothek mit flag, Strukturvalidierung sowie Inspektion der Ziel-Matrix. 👉 GitHub Repository

Enterprise Pro ($14,99 USD): Schaltet automatisierte Cross-Compilation-Matrix-Builds frei, //go:embed-Asset-Bundling, Optimierung des Binary-Footprint sowie fertige GitHub-Actions-Release-Workflows mit SHA-256-Checksummen-Generierung. 👉 GoCLIForge Pro auf Gumroad erhalten

Welche Utilities haben Sie bereits in Go statt in Python oder Node.js gebaut? Teilen Sie Ihre Erfahrungen in den Kommentaren mit!

Automatisch übersetzt. Das Original auf Englisch ist unten aufklappbar.

Original (Englisch) anzeigen

Autonomous coding swarms ( Claude Desktop, Cursor, Devin, Roo-Code ) are increasingly tasked with generating backend utilities, system micro-tools, and automation CLIs.

However, when agents default to Python or Node.js, the developer experience degrades rapidly:

In this tutorial, we will explore GoCLIForge MCP, an open-source and enterprise Model Context Protocol (MCP) server that empowers AI agents to generate, validate, cross-compile, and distribute zero-dependency, single-binary Go CLI tools with sub-millisecond execution.

Go's standard library includes flag.FlagSet, which provides isolated, thread-safe subcommand argument parsing without importing external packages.

┌────────────────────────────────────────────────────────┐ │ AI Coding Agents │ │ (Claude Desktop, Cursor, Devin, Roo-Code) │ └───────────────────────────▲────────────────────────────┘ │ JSON-RPC 2.0 (stdio) ┌───────────────────────────▼────────────────────────────┐ │ GoCLIForge Pro MCP Engine │ ├───────────────────────────┬────────────────────────────┤ │ 1. Zero-Dep CLI Scaffolder│ 4. Binary Symbol Optimizer │ │ (flag.FlagSet POSIX) │ (strip, dead-code elim) │ ├───────────────────────────┼────────────────────────────┤ │ 2.

Cross-Compiler Matrix │ 5. Embedded Asset Engine │ │ (Linux, Darwin, Windows│ (//go:embed SQLite/docs)│ ├───────────────────────────┼────────────────────────────┤ │ 3. Checksum & Tarballer │ 6.

GitHub Actions Workflow │ │ (SHA-256 manifest gen) │ (Automated multi-arch CI│ └───────────────────────────┴────────────────────────────┘ Enter fullscreen mode Exit fullscreen mode 1. Zero-Dependency Subcommand Routing Here is the clean, idiomatic pattern generated by GoCLIForge for modular subcommands:

package main import ( "flag" "fmt" "os" ) func ExecuteRun ( args [] string ) int { fs:= flag. NewFlagSet ( "run", flag. ExitOnError ) fs. Usage = func () { fmt. Printf ( "Usage: myapp run [options] \n\n Options: \n " ) fs. PrintDefaults () } inputFile:= fs. String ( "file", "", "Path to input file" ) verbose:= fs. Bool ( "verbose", false, "Enable verbose logging" ) if err:= fs. Parse ( args ); err!= nil { fmt. Fprintf ( os. Stderr, "Error parsing flags: %v \n ", err ) return 1 } fmt. Printf ( "[OK] Running task with file=%s, verbose=%v \n ", * inputFile, * verbose ) return 0 } Enter fullscreen mode Exit fullscreen mode And the primary dispatcher in main.go:

package main import ( "fmt" "os" ) var version = "1.0.0" func main () { args:= os. Args [ 1: ] if len ( args ) == 0 { fmt. Println ( "Usage: myapp <command> [arguments]" ) os.

Exit ( 0 ) } switch args [ 0 ] { case "-v", "--version", "version": fmt. Printf ( "myapp version %s \n ", version ) case "run": os. Exit ( ExecuteRun ( args [ 1: ])) default: fmt.

Fprintf ( os. Stderr, "Unknown command: %s \n ", args [ 0 ]) os. Exit ( 1 ) } } Enter fullscreen mode Exit fullscreen mode Key Advantages: Zero Third-Party Imports: The entire tool compiles in <1 second with go build.

Static Binary: Does not require Go, libc, Python, or Node.js on the user's host. POSIX Compliant: Accepts standard --flag=value and -flag value syntax. 2.

Embedding Static Assets with //go:embed Often, a CLI tool requires templates, documentation, or an embedded database schema. Go's native embed package packs files directly into the compiled binary:

package assets import ( "embed" "fmt" ) //go:embed * var EmbeddedFS embed. FS // ReadAsset reads a file from the embedded filesystem func ReadAsset ( name string ) ([] byte, error ) { data, err:= EmbeddedFS. ReadFile ( name ) if err!= nil { return nil, fmt. Errorf ( "embedded asset %s not found: %w", name, err ) } return data, nil } Enter fullscreen mode Exit fullscreen mode 3. Deterministic Cross-Compilation Matrix Go has native cross-compilation built into the toolchain. By setting CGO_ENABLED=0, GOOS, and GOARCH, Go generates standalone static executables for all major operating systems:

# Linux x86_64 CGO_ENABLED = 0 GOOS = linux GOARCH = amd64 go build -trimpath -ldflags = "-s -w" -o dist/mycli-linux-amd64. # Linux ARM64 (Raspberry Pi, AWS Graviton) CGO_ENABLED = 0 GOOS = linux GOARCH = arm64 go build -trimpath -ldflags = "-s -w" -o dist/mycli-linux-arm64. # macOS Apple Silicon (M1/M2/M3/M4) CGO_ENABLED = 0 GOOS = darwin GOARCH = arm64 go build -trimpath -ldflags = "-s -w" -o dist/mycli-darwin-arm64. # Windows x86_64 CGO_ENABLED = 0 GOOS = windows GOARCH = amd64 go build -trimpath -ldflags = "-s -w" -o dist/mycli-windows-amd64.exe. Enter fullscreen mode Exit fullscreen mode Why -ldflags="-s -w"? -s: Strips the symbol table. -w: Strips DWARF debug information. Result: Reduces binary size by 35% to 50% with zero impact on runtime performance.

4. Configuring Claude Desktop & Cursor Integrating GoCLIForge MCP into your local agent environment takes less than 30 seconds.

{ "mcpServers": { "gocliforge": { "command": "python3", "args": [ "-m", "gocliforge.cli", "--stdio" ] } } } Enter fullscreen mode Exit fullscreen mode In Cursor (.cursor/mcp.json ): { "mcpServers": { "gocliforge": { "command": "python3", "args": [ "-m", "gocliforge.cli", "--stdio" ] } } } Enter fullscreen mode Exit fullscreen mode 5. Live Agent Demo Prompts Once connected, your AI agents can perform automated tooling tasks:

"Scaffold a zero-dependency Go CLI named 'logpipe' with subcommands 'tail' (flags: --file, --follow ) and 'filter' (flags: --pattern, --regex )."

"Validate our Go codebase structure and verify that zero third-party dependencies have been introduced."

"Compile static release binaries for Linux amd64, macOS Apple Silicon, and Windows for this project with stripped symbols."

"Generate a GitHub Actions release pipeline that compiles all 5 target binaries and publishes them with SHA-256 checksums."

Open-Source (Lite): Includes project scaffolding, standard library flag generation, structure validation, and target matrix inspection. 👉 GitHub Repository

Enterprise Pro ($14.99 USD): Unlocks automated cross-compilation matrix builds, //go:embed asset bundling, binary footprint optimization, and turnkey GitHub Actions release workflows with SHA-256 checksum generation. 👉 Get GoCLIForge Pro on Gumroad

What utilities have you built in Go instead of Python or Node.js? Share your experiences in the comments!

Quelle: dev.to (dev.to). Original öffnen