bridge

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 19 Imported by: ? 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 bridge wraps foreign tool surfaces — local command-line programs, remote MCP servers, pasted tools/list payloads — as xyz command entries. One declarative config (xyz.json) then yields the same command on all three xyz-go fronts: CLI subcommands, HTTP routes and MCP tools.

The core primitive is NewTool: a JSON Schema subset plus an Invoker becomes a *spec.Entry the xyz-go frontends consume directly, with no compile-time struct and no reflection beyond what the frontends read from the field metadata. Everything else in this package (exec adapters, MCP proxies, config loading) produces schemas and invokers for it.

Index

Constants

View Source
const DefaultConfigName = "xyz.json"

DefaultConfigName is the file the gateway looks for in the current directory when XYZ_CONFIG is unset.

View Source
const Version = "0.1.0"

Version identities this bridge in the MCP client handshake.

Variables

This section is empty.

Functions

func Load

func Load(ctx context.Context, source string) (*registry.Registry, error)

Load reads one config source — a local path or an http(s) URL — and builds the registry. A comma-separated list merges several sources into one service (see LoadMany); an empty source falls back to LoadDefault's lookup order. URLs are fetched afresh on every process start — the "resolve once per run" model the gateway documents.

func LoadDefault

func LoadDefault(ctx context.Context) (*registry.Registry, error)

LoadDefault locates the config by convention: XYZ_CONFIG (a path or URL, comma-separated for several sources) wins, otherwise ./xyz.json in the current directory.

func LoadMany added in v0.1.3

func LoadMany(ctx context.Context, sources []string) (*registry.Registry, error)

LoadMany merges several sources (files and URLs mixed) into one registry — the "several CLIs, one service" shape: every tool of every config ends up in the same CLI tree, HTTP router and MCP server. Duplicate tool names across sources are errors, surfacing at startup instead of at first call.

func NewTool

func NewTool(name, summary, description string, input any, inv Invoker) (*spec.Entry, error)

NewTool is the dynamic entry factory with default channel bindings; see NewToolWith for the tunables.

func NewToolWith added in v0.1.3

func NewToolWith(name, summary, description string, input any, inv Invoker, opts ToolOpts) (*spec.Entry, error)

NewToolWith is the dynamic entry factory: a JSON Schema subset (input) plus an Invoker becomes a fully wired spec.Entry — the CLI subcommand (dotted segments become levels), the HTTP route and the MCP tool all derive from it, with no compile-time struct. A dotted-free name lands as a top-level command: HTTP POST /<prefix>/ls, CLI <app> ls.

func ParseAndBuild

func ParseAndBuild(ctx context.Context, data []byte) (*registry.Registry, error)

ParseAndBuild parses an xyz.json document and resolves its tools.

Types

type Config

type Config struct {
	// HTTPPrefix sets the route prefix for every tool of this config
	// (default "/tools"); "/api" turns them into /api/<dotted-path>.
	HTTPPrefix string `json:"http_prefix,omitempty"`
	// MCPPrefix sets the MCP tool-name prefix for every tool of this
	// config: tools are offered as <mcp_prefix><name>. Independent from
	// HTTPPrefix — set one, the other, or both, per channel's needs.
	MCPPrefix string       `json:"mcp_prefix,omitempty"`
	Tools     []ToolConfig `json:"tools"`
}

Config is the xyz.json document: a declarative list of tools, each carried by exactly one adapter. One config hosts as many tools as needed — they compose into a single registry, one service behind every frontend.

func ParseConfig

func ParseConfig(data []byte) (*Config, error)

ParseConfig parses the xyz.json document shape.

func (*Config) Build

func (c *Config) Build(ctx context.Context) (*registry.Registry, error)

Build resolves every declared tool into entries registered in a fresh registry. Exec tools bind immediately; MCP proxies launch their server processes here — once per process — and stay alive until it exits.

type ExecConfig

type ExecConfig struct {
	// Program is the executable. Args optionally carry {name} templates.
	Program string   `json:"program"`
	Args    []string `json:"args,omitempty"`
	// Env adds or overrides environment entries; values use the same
	// {name} interpolation. The inherited environment is preserved.
	Env map[string]string `json:"env,omitempty"`
	// Timeout bounds one invocation ("30s" default; the caller's context
	// deadline, when set, still wins).
	Timeout string `json:"timeout,omitempty"`
	// MaxOutput caps returned stdout bytes (default 1 MiB).
	MaxOutput int64 `json:"max_output,omitempty"`
}

ExecConfig declares a local command-line adapter. Every call spawns one process; Args and Env entries may reference input properties as {name} placeholders.

type Invoker

type Invoker func(ctx context.Context, args map[string]any) (any, error)

Invoker runs one tool call with schema-normalized arguments and returns the value every frontend renders: plain text for exec tools, a native JSON value for MCP proxies.

func ExecInvoker

func ExecInvoker(cfg ExecConfig) Invoker

ExecInvoker binds the config to an Invoker. The result is the trimmed stdout text; a non-zero exit makes the call fail with the program's stderr as the message, mapped through the xyz error taxonomy (internal, or unavailable on timeout / spawn failure).

type MCPConfig

type MCPConfig struct {
	Command string   `json:"command"`
	Args    []string `json:"args,omitempty"`
	// Tools limits proxying to these remote tool names; empty proxies all.
	Tools []string `json:"tools,omitempty"`
	// Prefix renames proxied tools to "<prefix><tool>"; empty falls back to
	// the config tool name plus "." as the namespace.
	Prefix string `json:"prefix,omitempty"`
}

MCPConfig declares a proxied stdio MCP server. The gateway launches the command once per process, performs the handshake and tools/list up front, and forwards every subsequent call through the official SDK client. The server process lives until the gateway exits (Close is offered for explicit teardown).

type MCPProxy

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

MCPProxy is a live proxied server connection. tools maps the displayed (namespaced) name to the remote tool description.

func StartMCPProxy

func StartMCPProxy(ctx context.Context, cfg MCPConfig, namespace string) (*MCPProxy, error)

StartMCPProxy launches the server process, completes the MCP handshake and lists its tools. Proxied names carry the namespace prefix.

func (*MCPProxy) Close

func (p *MCPProxy) Close()

Close kills the server process; the OS would reap it at gateway exit anyway, this is the explicit path.

func (*MCPProxy) InvokeTool

func (p *MCPProxy) InvokeTool(display string) Invoker

InvokeTool returns an Invoker that forwards calls to one proxied tool. Tool-level errors (isError results) surface as internal errors carrying the remote text; transport failures surface as unavailable, so the gateway inherits the same taxonomy it would produce natively.

type RemoteTool

type RemoteTool struct {
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	Input       map[string]any `json:"input,omitempty"` // raw inputSchema subset
}

RemoteTool is one tool discovered via tools/list, the shape consumed by ImportToolsList and carried by MCPProxy.

func ImportToolsList

func ImportToolsList(raw []byte) ([]RemoteTool, error)

ImportToolsList parses a tools/list response or a bare tool array into remote tool descriptors — the "paste a string" path: combine with an invoker (MCPProxy.InvokeTool, or custom) and NewTool to turn a pasted listing into three-channel commands with no config file at all.

type ToolConfig

type ToolConfig struct {
	Name        string         `json:"name"`
	Summary     string         `json:"summary,omitempty"`
	Description string         `json:"description,omitempty"`
	Input       map[string]any `json:"input,omitempty"` // JSON Schema subset for the arguments
	Exec        *ExecConfig    `json:"exec,omitempty"`  // local command adapter
	MCP         *MCPConfig     `json:"mcp,omitempty"`   // stdio MCP server proxy
	// MCPName pins this exec tool's MCP tool name outright (beats
	// Config.MCPPrefix). Proxied MCP tools use Config.MCPPrefix instead —
	// one proxy yields many tools, one pinned name cannot cover them.
	MCPName string `json:"mcp_name,omitempty"`
}

ToolConfig declares one dynamic tool.

type ToolOpts added in v0.1.3

type ToolOpts struct {
	// HTTPPrefix replaces the default "/tools" route prefix; leading and
	// trailing slashes are normalized away. The full route becomes
	// "<method> /<prefix>/<dotted-path>".
	HTTPPrefix string
	// MCPName overrides the MCP tool name offered to clients; empty keeps
	// the entry name. HTTP and CLI naming is never affected, so each
	// channel can carry its own prefix independently.
	MCPName string
	// CLISkip removes the whole command from the CLI frontend.
	CLISkip bool
}

ToolOpts tunes the per-tool channel bindings of a dynamic entry.

Jump to

Keyboard shortcuts

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