examples/

directory
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0

README

Examples

Every example here is a complete program with a test. The tests run offline - in process, or against a local stand-in backend - so go test ./examples/... needs no network and no credentials.

Run any example from the repository root with go run ./examples/<path>. Servers speak stdio by default; add -addr 127.0.0.1:8080 for Streamable HTTP on /mcp (except mux, which owns its listener on 127.0.0.1:8080). Each directory's README shows real output.

Example What it shows Run
quickstart The smallest server: a struct of functions and mcp.Main; schema from the Go signature go run ./examples/quickstart
curated Import a REST API with mcp.FromREST, keep two operations with Only, pin an argument with Fix BACKEND=http://127.0.0.1:8080 go run ./examples/curated
github A GitHub REST endpoint served as a tool, with a static credential header GITHUB_TOKEN=... go run ./examples/github
generated A committed Go model generated from an OpenAPI document by iqmcp gen go run ./examples/generated
deploy A production-shaped server (API-key bearer auth, /healthz, deny-by-default curation) with a Dockerfile and a Kubernetes manifest go test ./examples/deploy; build and run steps in its README
gallery/search Paginated, read-only search with structured results go run ./examples/gallery/search
gallery/approval A destructive tool that runs only after the client confirms (mcp.WithClientApproval) go run ./examples/gallery/approval
gallery/binary Returning bytes as an io.Reader, served as an embedded binary resource go run ./examples/gallery/binary
gallery/job A long-running job streaming progress via an iter.Seq result go run ./examples/gallery/job
gallery/mux mcp.Handler mounted in your own net/http mux beside other routes go run ./examples/gallery/mux
gallery/offline Testing a server in process with the mcptest harness go test -v ./examples/gallery/offline
gallery/import A typed REST backend proxied as accounts.get / accounts.list BACKEND=http://127.0.0.1:8080 go run ./examples/gallery/import
gallery/openapi An embedded OpenAPI document served with mcp.FromOpenAPI BACKEND=http://127.0.0.1:8080 go run ./examples/gallery/openapi
gallery/composite One tool composed from two, with per-step scope checks and an identity source per transport SHELF_OPERATOR=ada SHELF_TOKEN=... go run ./examples/gallery/composite
hero The tooling behind the README's terminal animation (a separate module) make hero

The examples that need BACKEND call a REST service you provide; their tests bring their own stand-in, and their READMEs show the endpoints a stand-in has to answer.

Driving an example from the command line

The repository's iqmcp command lists and calls the tools of a built server binary over stdio:

go build -o /tmp/search ./examples/gallery/search
go run ./cmd/iqmcp inspect -- /tmp/search
go run ./cmd/iqmcp call --tool search --args '{"q":"deploy","page":1,"per_page":10}' -- /tmp/search

The generated schema marks all three search arguments as required, so the call passes page and per_page too.

inspect and call start the binary with a scrubbed environment (only PATH and PWD), so point them at a built binary rather than go run, and expect examples that need environment variables to use their defaults. For a server already listening over Streamable HTTP, pass --url instead - a local or private address too - and --bearer-env NAME when it needs a bearer token, which the command reads from the environment variable NAME:

go run ./cmd/iqmcp inspect --url http://127.0.0.1:8080/mcp
SHELF_TOKEN=s3cret go run ./cmd/iqmcp call --url http://127.0.0.1:8080/mcp --bearer-env SHELF_TOKEN --tool summary

The permission covers that host alone: a redirect to another local or private address is still refused, the token is never forwarded to another host, and cloud-metadata, link-local and multicast addresses stay refused whatever is typed.

More

  • Example gallery index - each scenario and the test that proves it, including the scenarios that need a live identity provider or database and are documented as guidance only.
  • Recipes - quickstart, auth, OAuth, resilience, result shaping, code generation, client configuration and the CLI.
  • Deployment - containers, Kubernetes, proxies and TLS.

Directories

Path Synopsis
Command curated is DESIGN_SPEC.md §4's second snippet's shape made runnable: one expression imports a REST backend with mcp.FromREST, curates it with Only and Fix - addressed by the Go field paths the snippet uses - and hands it to mcp.Main.
Command curated is DESIGN_SPEC.md §4's second snippet's shape made runnable: one expression imports a REST backend with mcp.FromREST, curates it with Only and Fix - addressed by the Go field paths the snippet uses - and hands it to mcp.Main.
Command deploy is a container-ready MCP server: the shape docs/deployment.md recommends for a Streamable HTTP service behind a platform probe.
Command deploy is a container-ready MCP server: the shape docs/deployment.md recommends for a Streamable HTTP service behind a platform probe.
gallery
approval command
Command approval serves one destructive tool beside one read-only tool and proves the E26 approval gate through the public pipeline: the read-only tool runs with nobody asked, and the destructive tool runs only once the calling client confirms it - mcp.WithClientApproval asks every client that declares elicitation "approve this destructive operation: publish", and denies a client that cannot be asked.
Command approval serves one destructive tool beside one read-only tool and proves the E26 approval gate through the public pipeline: the read-only tool runs with nobody asked, and the destructive tool runs only once the calling client confirms it - mcp.WithClientApproval asks every client that declares elicitation "approve this destructive operation: publish", and denies a client that cannot be asked.
binary command
Command binary serves one tool returning binary bytes as an io.Reader: the adapter renders the stream as one embedded octet-stream blob.
Command binary serves one tool returning binary bytes as an io.Reader: the adapter renders the stream as one embedded octet-stream blob.
composite command
Command composite serves one ordinary typed composite tool beside its two constituent tools: reading the shelf and fetching one book compose into a reading summary.
Command composite serves one ordinary typed composite tool beside its two constituent tools: reading the shelf and fetching one book compose into a reading summary.
import command
Command import proxies a typed specification end to end: a ledger-shaped REST backend described the iqlink way is imported by mcp.FromREST and served over stdio - the typed-import half of DESIGN_SPEC.md K46's import scenarios, dependency-free and offline.
Command import proxies a typed specification end to end: a ledger-shaped REST backend described the iqlink way is imported by mcp.FromREST and served over stdio - the typed-import half of DESIGN_SPEC.md K46's import scenarios, dependency-free and offline.
job command
Command job serves a long-running job as a streaming tool: the Go implementation returns an iter.Seq of progress strings and the adapter accumulates the elements into the result, reporting progress per element.
Command job serves a long-running job as a streaming tool: the Go implementation returns an iter.Seq of progress strings and the adapter accumulates the elements into the result, reporting progress per element.
mux command
Command mux mounts the package's Streamable HTTP handler inside an existing net/http mux: the MCP server lives beside the application's own routes - here a /healthz liveness probe, which mcp.WithHealthPath gives only to a listener mcp.Main owns - instead of owning the listener.
Command mux mounts the package's Streamable HTTP handler inside an existing net/http mux: the MCP server lives beside the application's own routes - here a /healthz liveness probe, which mcp.WithHealthPath gives only to a listener mcp.Main owns - instead of owning the listener.
offline command
Command offline drives a server with the testing harness (mcptest) and nothing else: no listener, no network, no subprocess.
Command offline drives a server with the testing harness (mcptest) and nothing else: no listener, no network, no subprocess.
openapi command
Command openapi binds one OpenAPI JSON document as a runtime-linkable API value and serves it over stdio - DESIGN_SPEC.md K46's "OpenAPI-to-MCP in 10 lines", dependency-free and offline.
Command openapi binds one OpenAPI JSON document as a runtime-linkable API value and serves it over stdio - DESIGN_SPEC.md K46's "OpenAPI-to-MCP in 10 lines", dependency-free and offline.
search command
Command search serves a paginated, read-only search tool over an in-memory catalogue: page and per_page arguments slice the matches, and the result carries the total so the caller can page to the end.
Command search serves a paginated, read-only search tool over an in-memory catalogue: page and per_page arguments slice the matches, and the result carries the total so the caller can page to the end.
Package main is the S3-09 static-generation example (ADR 0036 § static generation): the generated model in api_gen.go is committed source produced from spec.json by the iqmcp gen command below - never hand-written, never fetched from the network.
Package main is the S3-09 static-generation example (ADR 0036 § static generation): the generated model in api_gen.go is committed source produced from spec.json by the iqmcp gen command below - never hand-written, never fetched from the network.
Command github is DESIGN_SPEC.md K46's "GitHub via rest.Import" example and D17's one-line promise made runnable: a GitHub-shaped REST API imported by mcp.FromREST and served over stdio.
Command github is DESIGN_SPEC.md K46's "GitHub via rest.Import" example and D17's one-line promise made runnable: a GitHub-shaped REST API imported by mcp.FromREST and served over stdio.
Command quickstart is DESIGN_SPEC.md §4's first snippet, made runnable: the Weather structure and the one-line mcp.Main call serve stdio exactly as the specification writes them - protocol frames on stdout, run with no flags, or Streamable HTTP with -addr host:port.
Command quickstart is DESIGN_SPEC.md §4's first snippet, made runnable: the Weather structure and the one-line mcp.Main call serve stdio exactly as the specification writes them - protocol frames on stdout, run with no flags, or Streamable HTTP with -addr host:port.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL