config

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 8 Imported by: 0 modules, 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 config is the driven adapter for the JSON config file — the only state toolhost keeps. Approval is a list of qualified names in this file; `toolhost approve` edits it, `serve` reads it at boot.

Index

Constants

View Source
const (
	DefaultListen   = "127.0.0.1:8080"
	DefaultAuditLog = "toolhost_audit.jsonl"

	// DefaultAuditMaxMB is the audit log's rotation cap.
	DefaultAuditMaxMB = 10

	// ModeStateless is the default serving mode — sessionless Streamable
	// HTTP. ModeStateful opts into session-bearing mode for older clients
	// that need held sessions for pushed notifications/tools/list_changed.
	ModeStateless = "stateless"
	ModeStateful  = "stateful"

	// DefaultCallTimeout bounds upstream calls when the config doesn't say.
	DefaultCallTimeout = "60s"
)

Variables

This section is empty.

Functions

func ConfigHome added in v0.0.2

func ConfigHome() string

ConfigHome is toolhost's canonical home — $XDG_CONFIG_HOME/toolhost, else ~/.config/toolhost. Config, audit log, and token store live here when no project-local toolhost.json exists.

func DefaultPath added in v0.0.2

func DefaultPath(explicit string) string

DefaultPath resolves which config a command reads when -c isn't given: an explicit flag wins; a ./toolhost.json in the working directory beats the canonical home (a directory holding a config IS its own context).

func ResolveEnv

func ResolveEnv(s string) (string, error)

ResolveEnv expands an "env:NAME" reference; anything else passes through unchanged. Unset or empty fails closed — a config pointing at a missing variable is broken, not merely incomplete.

Credential fields stay as references inside File — Save round-trips the reference, never the resolved secret, so toolhost.json can carry pointers and be committed safely. Resolution happens at the boundary: ResolvedToken for the gateway token, Backend.Resolved for upstream creds.

Types

type Auth

type Auth struct {
	// Type: "none" (default), "bearer" (a static token sent as
	// Authorization: Bearer — the API-key shape), "client_credentials"
	// (machine OAuth; token endpoint discovered via the server's Protected
	// Resource Metadata), "oauth" (interactive auth-code + PKCE; run
	// `toolhost auth <backend>` to grant).
	Type string `json:"type"`

	// bearer:
	Token string `json:"token,omitempty"`

	// client_credentials + oauth:
	ClientID     string   `json:"client_id,omitempty"`
	ClientSecret string   `json:"client_secret,omitempty"`
	Scopes       []string `json:"scopes,omitempty"`

	// oauth only: loopback redirect the AS redirects the browser to.
	// Must match the registered/registered-via-DCR redirect URI.
	RedirectURL string `json:"redirect_url,omitempty"`
}

Auth configures how toolhost authenticates TO an upstream server — orthogonal to the bearer token that authenticates agents to toolhost.

type Backend

type Backend struct {
	// Transport is "stdio" (local subprocess), "http" (remote streamable-HTTP
	// MCP server), or "sse" (legacy HTTP+SSE MCP server).
	Transport string `json:"transport"`

	// stdio transport:
	Command string            `json:"command,omitempty"`
	Args    []string          `json:"args,omitempty"`
	Env     map[string]string `json:"env,omitempty"`
	// EnvAllowlist names host environment variables forwarded to the
	// subprocess beyond the safe baseline (PATH, HOME, ...). Credentials
	// should reach a backend by being named — here, or via env values
	// holding "env:" references — never by inheriting everything.
	EnvAllowlist []string `json:"env_allowlist,omitempty"`
	// EnvInherit forwards the entire host environment: every credential in
	// the gateway's shell reaches the backend. The least-safe option —
	// prefer Env + EnvAllowlist.
	EnvInherit bool `json:"env_inherit,omitempty"`

	// http/sse transports:
	URL     string            `json:"url,omitempty"`
	Headers map[string]string `json:"headers,omitempty"`
	Auth    *Auth             `json:"auth,omitempty"`

	// Passthrough marks this upstream as a federated toolhost gateway: its
	// tool names arrive already qualified ("cbm__search_graph") and are
	// trusted as-is instead of being re-namespaced. Its own toolhost__*
	// meta-tools are dropped — the outer gateway has its own control plane.
	Passthrough bool `json:"passthrough,omitempty"`

	// CallTimeout overrides the top-level call_timeout for this backend —
	// a Go duration ("30s", "2m"). A client-supplied tighter deadline still
	// wins; this bounds how long a hung backend may hold a call.
	CallTimeout string `json:"call_timeout,omitempty"`
}

Backend describes one upstream MCP server.

func (*Backend) Resolved

func (b *Backend) Resolved() (*Backend, error)

Resolved returns a copy of the backend with every "env:" reference in headers and auth fields resolved. The receiver is unchanged — the file's in-memory shape keeps references, never secrets.

type File

type File struct {
	Listen   string              `json:"listen"`
	Token    string              `json:"token"`
	AuditLog string              `json:"audit_log"`
	Backends map[string]*Backend `json:"backends"`
	// Mode selects the front-door serving mode — "stateless" (default) or
	// "stateful". Stateless is the primary design (SEP-2567 sessionless +
	// the 2026-07-28 spec): no Mcp-Session-Id, every request independent.
	// Legacy-protocol clients still work sessionlessly there — initialize,
	// tools/list, tools/call each stand alone; they cannot receive
	// tools/list_changed without a held session. Modern clients can subscribe
	// through subscriptions/listen. "stateful" is the opt-in compat mode for
	// older clients that must hold Mcp-Session-Id.
	Mode string `json:"mode,omitempty"`
	// Approved is the qualified-name list ("backend__tool") that Resolve
	// intersects with discovery. discovered ≠ approved: everything not on
	// this list is invisible and uncallable.
	Approved []string `json:"approved"`
	// ApprovedSchemas binds an approval to the contract reviewed at
	// approve time: qualified name → "sha256:…" of the tool's canonical
	// input+output schema. If the upstream's schema changes, the approval
	// lapses — the tool drops off the surface until a human re-approves
	// (which pins the new schema). Approved names with no entry here are
	// unbound: approvals made before schema binding keep working until
	// re-approved.
	ApprovedSchemas map[string]string `json:"approved_schemas,omitempty"`
	// Enabled is the live-surface allowlist, third tier of the invariant:
	// nil (absent) means every approved tool is enabled; a non-nil list
	// means only enabled∩approved is visible. Pointer so "absent" and
	// "explicitly empty" stay distinct — [] must not collapse into all.
	Enabled *[]string `json:"enabled,omitempty"`

	// CallTimeout bounds every upstream tool call — a Go duration,
	// default "60s". A caller's tighter deadline still wins. Per-backend
	// call_timeout overrides it. A hung backend can never hold a call
	// forever.
	CallTimeout string `json:"call_timeout,omitempty"`

	// Requested is the agent's pending ask: qualified names an agent put
	// on the record via toolhost__request, with its reason. Humans review
	// it (`toolhost status`) and approve consumes entries. Agents can
	// write it; only humans turn requests into approvals.
	Requested []Request `json:"requested,omitempty"`

	// AuditMaxMB caps the audit log size in MiB — past it the log rotates
	// to <audit_log>.1 (one generation kept; evidence is append-mostly and
	// recent history is what introspection needs). Absent or non-positive
	// means the default.
	AuditMaxMB int `json:"audit_max_mb,omitempty"`
}

File is the on-disk config document.

func Load

func Load(path string) (*File, error)

func (*File) Approve

func (f *File) Approve(pins map[string]string) ([]string, error)

Approve adds qualified names bound to their schema pins (name → SchemaHash of the contract reviewed; "" = unbound). Returns the names that changed: newly approved, or already-approved with a refreshed pin — the latter is how a drifted schema gets re-affirmed, and it MUST count as a change or the caller skips saving the pin. Approved names drop out of `requested` — the request has been answered.

func (*File) ApprovedSet

func (f *File) ApprovedSet() map[string]bool

ApprovedSet is the lookup form Resolve consumes.

func (*File) Disable

func (f *File) Disable(names ...string) ([]string, error)

Disable hides approved tools without un-approving them. The first disable materializes the allowlist as approved ∖ names — after that, the live surface is always explicit.

func (*File) Enable

func (f *File) Enable(names ...string) ([]string, error)

Enable adds qualified names to the live surface. No-op (nil) while no allowlist exists — without one, every approved tool is already enabled. Enabled names must be approved first; enabling is never a backdoor around approval.

func (*File) EnableAll

func (f *File) EnableAll()

EnableAll clears the allowlist — every approved tool is enabled again.

func (*File) EnableOnly

func (f *File) EnableOnly(names ...string) ([]string, error)

EnableOnly replaces the live surface with exactly the given names — the "I need just these tools" form. All must be approved.

func (*File) EnabledSet

func (f *File) EnabledSet() (map[string]bool, bool)

EnabledSet returns the allowlist and whether one is configured — (nil, false) means "all approved tools are enabled".

func (*File) IsApproved

func (f *File) IsApproved(qualified string) bool

func (*File) IsEnabled

func (f *File) IsEnabled(qualified string) bool

IsEnabled reports whether a qualified tool is on the live surface: enabled (no list, or listed) AND approved.

func (*File) RequestTools

func (f *File) RequestTools(reason string, names ...string) ([]string, error)

RequestTools records agent approval requests: qualified names, deduped, newest-last. Requesting is not approval — a human still has to approve.

func (*File) RequestedSet

func (f *File) RequestedSet() map[string]bool

RequestedSet is the lookup form — name → pending request.

func (*File) ResolvedToken

func (f *File) ResolvedToken() (string, error)

ResolvedToken resolves the gateway bearer token.

func (*File) Revoke

func (f *File) Revoke(names ...string) []string

Revoke removes qualified names and their schema pins; returns those actually removed.

func (*File) Save

func (f *File) Save(path string) error

Save writes the config back atomically (0600 — it carries the token) — tmp+rename so the serve watcher never reads a torn file.

type Request

type Request struct {
	Name   string    `json:"name"`
	Reason string    `json:"reason,omitempty"`
	At     time.Time `json:"at"`
}

Request is one agent-filed approval request.

Jump to

Keyboard shortcuts

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