typegen

package
v0.0.1-dev.53 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 50 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package typegen turns MCP server sources into typed clients written into the projects that call them.

A Consumer is a project directory that receives clients for the servers it names as Sources. Sources can be supplied explicitly or read from sh.holon.requires with DeclaredSources. The caller chooses which targets receive them; unresolved sources are errors. Connect opens a session with a source's running server instead.

Index

Constants

View Source
const (
	TargetTS = "ts"
	TargetUI = "ui"
	TargetPy = "py"
	TargetGo = "go"
)

Targets name the language and runtime a consumer receives clients for; slots names where they go.

View Source
const ContractFile = "holon-contract.json"

ContractFile is a server's committed contract document, beside its server.json: what holon types generate --contract prints for it. It stands in for running the server when a package the server imports is not installed.

View Source
const GoRuntimeModule = "github.com/holon-sh/sdk-go"

GoRuntimeModule is the runtime module that generated Go clients import.

View Source
const PythonRuntime = "holon-sh"

PythonRuntime is the Python package that generated Python clients import.

Variables

View Source
var ErrNoRuntime = errors.New("declares no runtime: depend on holon-sh or holon-ui in package.json, on holon-sh in pyproject.toml, or on " + GoRuntimeModule + " in go.mod")

ErrNoRuntime identifies a project without a declared Holon SDK runtime.

View Source
var Manifests = map[string]string{TargetTS: "package.json", TargetUI: "package.json", TargetPy: "pyproject.toml", TargetGo: "go.mod"}

Manifests are the files that declare each target's runtime dependency.

View Source
var NodePackages = map[string]string{TargetTS: "holon-sh", TargetUI: "holon-ui"}

Functions

func AcquireLocks

func AcquireLocks(consumers []Consumer) (func(), error)

AcquireLocks locks each consumer directory until the returned release function is called. A failure releases every acquired lock.

func AddPackageMember

func AddPackageMember(data []byte, object, name, value string) ([]byte, error)

AddPackageMember returns the package.json data with name set to value as the last member of its top-level object member object, which it adds last when absent, laid out as the file lays out its first member and leaving the rest as written; nil when object already has name.

func Check

func Check(ctx context.Context, c Consumer, options Options) error

func Connect

func Connect(ctx context.Context, source Source, options Options) (*mcp.ClientSession, error)

Connect opens an MCP session with a source's running server: an endpoint, or a registry version's streamable-http remote. An endpoint with an Alias sends <ALIAS>_MCP_TOKEN as its bearer token, as generation does. The client's timeout bounds the registry fetch and the handshake; later requests, such as a tool call that runs for minutes, are bounded by ctx alone. The caller closes the session.

func DeclaredTargets

func DeclaredTargets(dir string) ([]string, error)

DeclaredTargets are the slots a project's SDK dependencies declare.

func DockerCredential

func DockerCredential(env []string) (auth.CredentialFunc, error)

DockerCredential looks registry credentials up in the Docker config, by the host being contacted. DOCKER_CONFIG and HOME entries in env override this process's own.

func Generate

func Generate(ctx context.Context, consumers []Consumer, options Options) error

Generate maps #holon-sh and #holon-ui to the clients in package.json, installs missing declared SDK runtimes with the project's package manager, prepares clients, and writes the project slots.

func Install

func Install(ctx context.Context, dir string, options Options) ([][]string, error)

Install runs, in dir, the development install Kiln plans for the app there, and returns the commands that prepare it.

func MapClients

func MapClients(consumers []Consumer, options Options) error

func Requires

func Requires(dir string) (map[string]mcpregistry.Requirement, error)

Requires returns the dependencies the server.json in dir declares in sh.holon.requires, by alias.

func ServerKey

func ServerKey(serverName string) string

ServerKey is the client name derived from a server name: its last segment, lower-cased, with runs of other characters as hyphens, trimmed to a 49-character label.

func SnapshotSource

func SnapshotSource(name, dir, destination string) (string, error)

SnapshotSource copies dir's source into destination, or only fingerprints it when destination is empty.

func SourceEntry

func SourceEntry(root string) (string, error)

SourceEntry returns the one server entry relative to root, or "" when there is none. More than one entry is an error.

func ValidServerName

func ValidServerName(name string) bool

Types

type Consumer

type Consumer struct {
	// Name signs the generated files' banners and labels output.
	Name string
	// Dir is the absolute project directory.
	Dir string
	// Server is the consumer's own MCP server, inside Dir, or empty for none. It supplies the
	// consumer's identity, contract (Metadata) and check configuration. Browser clients call it
	// under its derived name; an Alias replaces that name and adds the client to every other target.
	Server Path
	Alias  string
	// Targets are the clients to write.
	// A server can have no targets when only its contract is needed.
	Targets []string
	// Sources are the servers that become apps.<alias> clients.
	Sources []Source
}

Consumer is a project directory that receives typed clients.

func AppConsumer

func AppConsumer(name, dir string) (*Consumer, error)

AppConsumer is the consumer for the app kept in dir, without its dependencies: the slots its SDK dependencies declare and its own MCP server, kept in dir or dir/server as the directory holding its entry. Only an entry that mentions MCP counts, since a project can keep a plain web server under the same file name, and more than one is an error. It returns nil for an app with neither, which has nothing to generate or check.

type Document

type Document struct {
	*mcpcontract.Metadata
	Data   []byte
	Digest string
}

Document retains the bytes a registry digest names. Copy Data when publishing; marshaling Metadata again would give a different digest for some valid documents.

func FetchDocument

func FetchDocument(ctx context.Context, blobs registry.BlobStore, reference string) (*Document, error)

FetchDocument reads a blob by digest, bounds its size and verifies its bytes. The same reader serves an image's own document and its pinned dependencies.

func NewDocument

func NewDocument(metadata *mcpcontract.Metadata) (*Document, error)

NewDocument serializes a newly observed contract. Schema numbers retain their precision.

func ParseDocument

func ParseDocument(data []byte) (*Document, error)

ParseDocument validates a document and retains its exact bytes.

func ReadDocument

func ReadDocument(ctx context.Context, repository *remote.Repository, descriptor ocispec.Descriptor) (*Document, error)

ReadDocument reads an image index's contract annotation, then the named blob. The contract manifest remains in the index for copying and garbage collection; it is not part of the read path.

func ResolveDocument

func ResolveDocument(ctx context.Context, source Source, options Options) (*Document, error)

ResolveDocument returns one source's contract document without writing anything: a path source is extracted by running its server, a registry or endpoint source is fetched, and a supplied document is returned as it is.

func (*Document) String

func (d *Document) String() string

type Endpoint

type Endpoint string

Endpoint is a live MCP endpoint; with an Alias, <ALIAS>_MCP_TOKEN supplies a bearer token for its origin.

func NewEndpoint

func NewEndpoint(url string) (Endpoint, error)

NewEndpoint accepts an HTTP(S) MCP URL without credentials or a fragment.

type Options

type Options struct {
	Env            []string
	HTTPClient     *http.Client
	Stdout, Stderr io.Writer
	SignIn         SignIn
	// Run runs one server through its language's contract runner, toolingDir/<language>/runner.mjs or
	// extract.py, with args (contract ROOT ENTRY), copying the runner's stdout to stdout. A failure the
	// runner reports is a *RunnerError. It defaults to the host's Node.js or Python interpreter, which
	// runs the server in its own directory and stops it after 30 seconds.
	Run func(ctx context.Context, language, toolingDir string, args []string, stdout io.Writer) error
}

type PackageJSON

type PackageJSON struct {
	Name, Version, Main                                                   string
	Scripts                                                               map[string]string
	Dependencies, DevDependencies, PeerDependencies, OptionalDependencies map[string]string
}

PackageJSON is what Holon reads of a package.json.

func (PackageJSON) Declares

func (p PackageJSON) Declares(pkg string) bool

func (PackageJSON) Launches

func (p PackageJSON) Launches() bool

Launches reports whether the package starts itself, through main or a start script, so that Holon runs it instead of importing its server entry.

type Path

type Path struct {
	Dir string
	// Served is what its running server answered, when it already has.
	Served *Served
}

Path is the absolute path of a server entry file, or of a directory holding exactly one.

func (Path) String

func (p Path) String() string

type Prepared

type Prepared struct {
	// contains filtered or unexported fields
}

Prepared holds each consumer's generated clients and the documents they pin; preparation never modifies consumers.

func Prepare

func Prepare(ctx context.Context, consumers []Consumer, options Options) (*Prepared, error)

Prepare extracts every path source once, resolves every other source once, and generates the client files for each consumer and target. Nothing is written to the consumers.

func (*Prepared) CheckEntry

func (p *Prepared) CheckEntry(consumer string) (entry, language string, ok bool)

CheckEntry is the consumer's own server entry, relative to the consumer directory, and its language, for a type check against the dependencies installed in its image. It reports false for consumers without an extracted server.

func (*Prepared) Documents

func (p *Prepared) Documents() map[string]*Document

Documents returns the own and dependency documents by digest, preserving fetched bytes.

func (*Prepared) Metadata

func (p *Prepared) Metadata() map[string]*mcpcontract.Metadata

Metadata returns each consumer's own contract document (identity, extracted contract and the expectations it holds of its sources) by consumer name.

func (*Prepared) Servers

func (p *Prepared) Servers() map[string]*mcpregistry.Descriptor

Servers returns how each consumer's own server describes itself, by consumer name.

func (*Prepared) Write

func (p *Prepared) Write(stdout io.Writer) error

Write validates all consumer slots before writing any of them, then reports each written target on stdout. Callers hold AcquireLocks and install dependencies before Prepare.

func (*Prepared) WriteClients

func (p *Prepared) WriteClients(consumer, dir string) error

WriteClients writes a consumer's clients into dir, a build context, each file with its StableTime, and nothing for an unknown one. It refuses to overwrite files it did not generate.

type Registry

type Registry protocol.RegistryRef

Registry is an MCP registry server version, read from its OCI package's document or its streamable-http remote.

func NewRegistry

func NewRegistry(ref protocol.RegistryRef) (Registry, error)

NewRegistry accepts a registry server version with a namespaced name and an exact version.

func (Registry) String

func (r Registry) String() string

type RunnerError

type RunnerError struct {
	Status int
	Stderr string
}

RunnerError is a contract runner's failure: the status it exited with and what it wrote to stderr. Status 2 is a diagnostic; status 3 means the server stopped on an uninstalled package, named on the last line.

func (*RunnerError) Error

func (e *RunnerError) Error() string

type Served

type Served struct {
	ServerInfo   json.RawMessage         `json:"serverInfo"`
	Capabilities *mcp.ServerCapabilities `json:"capabilities"`
	Lists        map[string]ServedList   `json:"lists"`
}

Served is what a running server answers to initialize and to each catalog list.

func ServedContract

func ServedContract(ctx context.Context, endpoint string, client *http.Client) (*Served, error)

ServedContract lists what the server at endpoint serves: its serverInfo, capabilities and every page of each catalog it declares.

type ServedList

type ServedList struct {
	Pages []json.RawMessage `json:"pages"`
	Error string            `json:"error,omitempty"`
}

type Server

type Server interface {
	// contains filtered or unexported methods
}

Server is where a contract comes from: a Path, an Endpoint, a Registry or a *Document.

type SignIn

type SignIn func(ctx context.Context, origin string) (string, error)

type Source

type Source struct {
	// Alias names the client (apps.<alias>). Empty derives it: the last segment of the server's
	// name (example.com/orders → orders), or the live server's serverInfo name for an Endpoint.
	Alias  string
	Server Server
}

Source is one MCP server whose contract becomes a client, supplied explicitly or declared in sh.holon.requires.

func ParseSource

func ParseSource(arg string) (Source, error)

ParseSource reads one [ALIAS=]PATH or URL argument. Paths are resolved against the working directory.

func RequiredSource

func RequiredSource(alias string, r mcpregistry.Requirement, path Path) (Source, error)

RequiredSource is the source an sh.holon.requires entry names: path when given, else its URL or registry version.

type SourceRules

type SourceRules struct {
	// contains filtered or unexported fields
}

SourceRules decide which files in an application directory are its source.

func ReadSourceRules

func ReadSourceRules(root string) (*SourceRules, error)

ReadSourceRules reads the source rules of the application at root.

func (*SourceRules) Ignored

func (s *SourceRules) Ignored(path string, dir bool) (ignored, below bool, err error)

Ignored reports whether path is left out of the source, and whether everything below it is too.

Jump to

Keyboard shortcuts

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