connectors

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 22 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 connectors is how Blitz's agents reach the systems a team works with (spec_connectors_047): databases now, email and others later. A connector is described, not coded: a category (Databases) and its kinds (PostgreSQL, SQLite…) are YAML descriptors, the built-in ones embedded and more read from <config>/connectors/kinds and a workspace's .agents/connectors/kinds. A kind names the backend that serves it, a Go Backend registered at start (Toolbox for databases), and the capabilities it offers (test, try, queries, schema, query-builder), which the backend must have.

A connection is one file, <name>.yaml, in <config>/connectors/<category> (every workspace) or .agents/connectors/<category> (this one), written by the kind's backend; its secret settings are Blitz secrets, never in the file. Each category's connections are served to the agents as one MCP server per backend.

Index

Constants

View Source
const (
	FieldText   = "text"
	FieldNumber = "number"
	FieldSecret = "secret"
	FieldList   = "list"
	FieldPath   = "path"
)

Field types.

View Source
const (
	Positional = "$1"
	Named      = "@name"
)

Placeholder styles.

View Source
const (
	ScopeWorkspace = config.ConnectorScopeWorkspace
	ScopeGlobal    = config.ConnectorScopeGlobal
)

Scopes.

View Source
const (
	OriginBuiltin   = "builtin"
	OriginGlobal    = ScopeGlobal
	OriginWorkspace = ScopeWorkspace
)

Origins of descriptors.

View Source
const (
	KindsFolder      = config.ConnectorKindsFolder
	CategoriesFolder = config.ConnectorCategoriesFolder
)

Descriptor folders, in a connectors folder.

Folder is the connectors' folder, in the config folder and in a workspace's .agents: its kinds and categories folders hold descriptors, and a folder per category its connections.

Variables

Capabilities are every capability, in the order they're shown.

View Source
var ErrNoTables = errors.New("no tables")

ErrNoTables is a schema read that found no table it could read.

View Source
var ErrUnknown = errors.New("no such connection")

ErrUnknown is a connection that isn't there.

FieldTypes are the types a field can have ("" is text).

View Source
var ParamTypes = []string{"string", "integer", "float", "boolean"}

ParamTypes are the types a parameter may have.

Scopes are the connection scopes, the global one first: what Dirs lists in order.

View Source
var TestTimeout = 20 * time.Second

TestTimeout bounds connecting to a connection to test, try or read it.

Functions

func AllFiles

func AllFiles(d Dirs) []string

AllFiles are every descriptor and connection file the connectors in d read, for the settings watch.

func Backends

func Backends() []string

Backends are the registered backends' IDs, sorted.

func Check

func Check(b Backend, k *Kind, c Connection) []string

Check lists what keeps c, of kind k (nil for a custom one), from being written: its name, its kind's required and number settings, and what its backend says.

func Delete

func Delete(cat Catalog, d Dirs, r Ref) error

Delete removes a connection and its secrets: every secret its category's kinds could have given it, so none is left behind even when its file can't be read.

func Dir

func Dir(prefixDir, workspace, scope string) string

Dir is a scope's connectors folder ("" for a workspace scope with no workspace).

func Fingerprint

func Fingerprint(files []string) string

Fingerprint changes when the files do (added, removed, changed).

func GlobalDir

func GlobalDir(prefixDir string) string

GlobalDir is the global connectors folder.

func HasSecret

func HasSecret(d Dirs, cat Category, c Connection, field string) bool

HasSecret says whether a connection's secret field has a value, in its own scope.

func Hash

func Hash(file string) string

Hash is a connection file's content hash, which a trust decision records ("" when it can't be read).

func Key

func Key(scope, category, name string) string

Key names a connection in a workspace's choices: <scope>/<category>/<name>.

func KindComment

func KindComment(kind string) string

KindComment is the line that names a file's kind.

func KindOf

func KindOf(text string) string

KindOf is the kind a connection file's text names in its comment ("": none).

func Prepare

func Prepare(cat Catalog, env Env, conn Connection) (Backend, *Kind, Connection, error)

Prepare is c as the backend writes it for a test, try or schema read: its kind and backend, and its secret settings naming their secrets when a value is typed or kept.

func Register

func Register(b Backend)

Register makes a backend available to kinds; a package registers its own in init.

func ResolvePath

func ResolvePath(p, base string) (string, bool)

ResolvePath is p, a path setting's value (a field of type path), as it opens: a relative one against base (the workspace's folder for a workspace connection, the home folder for a global one), ~/ as the home folder; a value that isn't a file's path (SQLite's :memory:, a file: URI, a ${NAME} filled later) is left as it is (false).

func SecretKey

func SecretKey(cat Category, connection, field string) string

SecretKey is the Blitz secret a connection's secret field is kept as: DB_SHOP_PASSWORD (the category's SecretPrefix, the name, the field).

func SecretRef

func SecretRef(cat Category, connection, field string) string

SecretRef is the placeholder a connection file names a secret field's value by: ${DB_SHOP_PASSWORD}.

func TryTool

func TryTool(ctx context.Context, cat Catalog, env Env, conn Connection, tool string, args map[string]any) (string, bool, error)

TryTool runs one of conn's tools as written, with args.

func ValidName

func ValidName(name string) bool

ValidName says whether name can name a connection or a query: lower case letters, digits and _, starting with a letter, at most 40.

func With

With is servers with the workspace's connector servers in place of any in-process ones there: each category's connection files that are on, served by their backends. A server of the user's own with a connector server's name wins, and Warn says so.

func WorkspaceDir

func WorkspaceDir(workspace string) string

WorkspaceDir is a workspace's connectors folder.

Types

type Backend

type Backend interface {
	// ID is the name kinds give it (toolbox).
	ID() string
	// Capabilities are what its kinds may offer.
	Capabilities() []Capability
	// Parse reads a connection file (name, its text) into the form, kinds
	// being the category's kinds it serves; one it can't describe is
	// Custom.
	Parse(cat Category, kinds []Kind, name, text string) Connection
	// Check lists what keeps c, of kind k (nil for a custom one), from
	// being written, beyond its name and required settings.
	Check(k *Kind, c Connection) []string
	// Render writes c, of kind k (nil for a custom one), as its file; its
	// secret settings name their secrets.
	Render(cat Category, k *Kind, c Connection) (string, error)
	// Serve is the MCP servers for a category's connection files; none
	// when there are none.
	Serve(s Serving) []config.MCPServerConfig
}

Backend serves kinds of connectors: it writes and reads their files and turns a category's files into MCP servers. What else it can do is the optional interfaces below (Tester, Tryer, SchemaReader), which Capabilities lists.

func BackendOf

func BackendOf(id string) (Backend, bool)

BackendOf is the backend id.

type Capability

type Capability string

Capability is something a kind of connector can do, beyond being served to the agents.

const (
	// Test connects to a connection as written and lists its tools.
	Test Capability = "test"
	// Try runs one of a connection's tools as written.
	Try Capability = "try"
	// Queries are the user's own tools, a statement with parameters each.
	Queries Capability = "queries"
	// Schema reads what a connection holds: its tables and their columns.
	Schema Capability = "schema"
	// QueryBuilder drafts a query from what the user describes, from the
	// schema, with the sql-query skill (needs Schema and Queries).
	QueryBuilder Capability = "query-builder"
)

The capabilities.

type Catalog

type Catalog struct {
	Categories []Category
	Kinds      []Kind
}

Catalog is what connectors there are: the categories and their kinds, built-in first, then the global descriptors and the workspace's. A descriptor that can't be used is listed with its problem.

func Load

func Load(prefixDir, workspace string) Catalog

Load reads the catalog for a workspace ("" for the global one alone). A global or workspace descriptor adds a category or a kind; one with the ID of one there already isn't used, so a workspace can't change what a built-in kind means. It's read again only when a descriptor file changes (catalogs).

func (Catalog) Category

func (cat Catalog) Category(id string) (Category, bool)

Category is the usable category id.

func (Catalog) Kind

func (cat Catalog) Kind(id string) (Kind, bool)

Kind is the usable kind id.

func (Catalog) KindsOf

func (cat Catalog) KindsOf(category, backend string) []Kind

KindsOf are a category's usable kinds, of backend ("" for any).

func (Catalog) Usable

func (cat Catalog) Usable() []Category

Usable are the categories that can be used.

type Category

type Category struct {
	ID   string `yaml:"id"`
	Name Text   `yaml:"name"`
	Hint Text   `yaml:"hint"`
	// Icon is a Material Design icon's name, without mdi (database).
	Icon string `yaml:"icon"`
	// Server is its MCP server's name, in /mcp and mcp(<server>:…) rules.
	Server string `yaml:"server"`
	// Prefix namespaces its tools: db__shop_list_tables.
	Prefix string `yaml:"prefix"`
	// SecretPrefix starts its connections' secrets: DB_SHOP_PASSWORD.
	SecretPrefix string `yaml:"secretPrefix"`
	// Backend serves its files that name no kind (hand-written ones).
	Backend string `yaml:"backend"`

	Origin  string `yaml:"-"`
	Path    string `yaml:"-"`
	Problem string `yaml:"-"`
}

Category is a group of kinds of connectors (Databases, Email): Studio lists connections by category, and each category's are one MCP server per backend.

type Column

type Column struct {
	Name  string
	Type  string
	About string
}

Column is a table's column.

type Connection

type Connection struct {
	// Name is the file's name; its tools are named <name>_<tool>.
	Name     string
	Category string
	// Scope is workspace (.agents/connectors/<category>) or global.
	Scope string
	Path  string
	// Kind is its kind's ID ("" for a custom file the backend can't say).
	Kind string
	// Description is what it holds, for the agents.
	Description string
	// Settings are the kind's fields; a secret one names its secret.
	Settings map[string]string
	// ReadOnly connections can't change what they're connected to.
	ReadOnly bool
	// Tools are the ready-made tools switched on, by ID.
	Tools   []string
	Queries []Query
	// Custom files are more than the form describes: edited as text.
	Custom bool
	// Text is the file.
	Text string
	// Problem is why it can't be read ("" when it can).
	Problem string
	// Trusted says the person trusted it (Serving.Trusted): a global one,
	// or a workspace one saved in Studio or with trusted project settings.
	Trusted bool
	// On says it's served in this workspace: the person can turn any
	// connection off there, a global one too (CON-17).
	On bool
}

Connection is one connection file, as Studio's form shows it.

func List

func List(cat Catalog, d Dirs) []Connection

List reads the connections, every category's, the global ones first.

func MakeGlobal

func MakeGlobal(d Dirs, category, name string) (Connection, []string, error)

MakeGlobal moves the workspace's connection name, of category, to the global folder with its secrets (CON-19), so every workspace has it: problems, with nothing moved, when its kind or category is the workspace's own descriptor, it's a custom file, or a global one has its name. ErrUnknown when it isn't there.

func Parse

func Parse(cat Catalog, c Category, name, text string) Connection

Parse reads a category's connection file text into the form, by its backend.

func Save

func Save(cat Catalog, d Dirs, conn Connection, values map[string]string, previous string) (Connection, []string, error)

Save writes conn in its scope's folder, in place of previous (its old name, "" for a new one), keeping the secret values given (by field) as Blitz secrets in that scope; problems, with nothing written, when it can't be saved.

func (Connection) Key

func (c Connection) Key() string

Key is c's key (Key).

func (Connection) Ref

func (c Connection) Ref() Ref

Ref is c's reference.

type Dirs

type Dirs struct {
	Prefix    string
	Workspace string
}

Dirs is where connections are: the settings folder (Prefix, "" for ~/.blitz) for the global ones and a workspace ("" for none) for its own.

func (Dirs) ConnectionDir

func (d Dirs) ConnectionDir(scope, category string) string

ConnectionDir is a category's connections folder in a scope ("" for the workspace's with no workspace).

func (Dirs) Dir

func (d Dirs) Dir(scope string) string

Dir is a scope's connectors folder ("" for the workspace's with no workspace).

func (Dirs) Files

func (d Dirs) Files(scope, category string) []string

Files are a category's connection files in a scope, sorted.

func (Dirs) Global

func (d Dirs) Global() Dirs

Global is d without its workspace: the global connections alone.

func (Dirs) SecretScope

func (d Dirs) SecretScope(scope string) string

SecretScope is the secrets scope of a connection scope's.

type Env

type Env struct {
	Dirs
	Category Category
	Values   map[string]string
}

Env is where a connection is tested, tried or read: its folders, its category, and the secret values typed into the form (by field) in place of the kept ones.

type Field

type Field struct {
	// Key is its name in the file (host, port, project…).
	Key   string `yaml:"key"`
	Label Text   `yaml:"label"`
	Help  Text   `yaml:"help"`
	// Type is text (the default), number (a whole number), secret (kept as
	// a Blitz secret, the file naming it), list (comma-separated) or path.
	Type     string `yaml:"type"`
	Required bool   `yaml:"required"`
	// Default is used when it's left empty.
	Default string `yaml:"default"`
}

Field is a setting a kind takes.

func (Field) Secret

func (f Field) Secret() bool

Secret says whether the field is kept as a Blitz secret.

type Kind

type Kind struct {
	ID       string `yaml:"id"`
	Category string `yaml:"category"`
	Name     Text   `yaml:"name"`
	Hint     Text   `yaml:"hint"`
	// Backend serves it (toolbox).
	Backend string `yaml:"backend"`
	// Source is the backend's own type for it (a Toolbox source type).
	Source       string       `yaml:"source"`
	Fields       []Field      `yaml:"fields"`
	ReadOnly     *ReadOnly    `yaml:"readOnly"`
	Tools        []ReadyTool  `yaml:"tools"`
	Queries      *QuerySpec   `yaml:"queries"`
	Schema       *SchemaSpec  `yaml:"schema"`
	Capabilities []Capability `yaml:"capabilities"`

	// Origin is builtin, global or workspace; Path its file ("" built in).
	Origin  string `yaml:"-"`
	Path    string `yaml:"-"`
	Problem string `yaml:"-"`
}

Kind is a kind of connector: a descriptor.

func (Kind) Can

func (k Kind) Can(c Capability) bool

Can says whether the kind has capability c.

func (Kind) Field

func (k Kind) Field(key string) (Field, bool)

Field is the kind's field key.

func (Kind) SecretFields

func (k Kind) SecretFields() []Field

SecretFields are k's fields kept as secrets (a password).

func (Kind) Tool

func (k Kind) Tool(id string) (ReadyTool, bool)

Tool is the kind's ready-made tool id.

type Param

type Param struct {
	Name string
	// Type is string, integer, float or boolean.
	Type        string
	Description string
}

Param is a query's parameter.

type Query

type Query struct {
	Name        string
	Description string
	Statement   string
	Params      []Param
}

Query is a connection's own tool: <connection>_<name>.

type QuerySpec

type QuerySpec struct {
	// Type is the backend's tool type for one (postgres-sql).
	Type string `yaml:"type"`
	// Dialect is the SQL they're in, for the query builder (PostgreSQL).
	Dialect string `yaml:"dialect"`
	// Placeholders is how a statement names its parameters: $1 (by
	// position) or @name.
	Placeholders string `yaml:"placeholders"`
	Hint         Text   `yaml:"hint"`
	Example      string `yaml:"example"`
}

QuerySpec is how a kind's own queries are written.

type ReadOnly

type ReadOnly struct {
	Key   string `yaml:"key"`
	Value any    `yaml:"value"`
	// Hint says what read-only means for it.
	Hint Text `yaml:"hint"`
}

ReadOnly is how a kind is made read-only: Key set to Value in its file. A kind without it can't be.

type ReadyTool

type ReadyTool struct {
	ID string `yaml:"id"`
	// Type is the backend's own (a Toolbox tool type).
	Type string `yaml:"type"`
	// Statement makes it a query (a backend's SQL tool).
	Statement string `yaml:"statement"`
	// About is what it does, for Studio.
	About Text `yaml:"about"`
	// Agent is what it does, for the agents; {connection} is the
	// connection's name and description.
	Agent string `yaml:"agent"`
	// Writes says it can change what it's connected to: not offered on a
	// read-only connection.
	Writes bool `yaml:"writes"`
	// On says a new connection has it.
	On bool `yaml:"on"`
}

ReadyTool is a ready-made tool a kind offers: named <connection>_<id>.

type Ref

type Ref struct {
	Scope    string
	Category string
	Name     string
}

Ref names a connection: its scope, category and name.

func (Ref) Key

func (r Ref) Key() string

Key is r's key in a workspace's choices (Key).

type SchemaReader

type SchemaReader interface {
	ReadSchema(ctx context.Context, env Env, k *Kind, c Connection) ([]Table, error)
}

SchemaReader reads a connection's tables and their columns; ErrNoTables when it can read none.

type SchemaSpec

type SchemaSpec struct {
	Statement string `yaml:"statement"`
	Reader    string `yaml:"reader"`
}

SchemaSpec is how a kind's schema is read: a statement (run as one of its queries) whose rows are the tables, or a reader the backend has.

type ServeOptions

type ServeOptions struct {
	Dirs
	Trusted func(file string) bool
	On      func(key string) bool
	Warn    func(string)
}

ServeOptions is what With serves from: the folders, which workspace files the person trusted (Serving.Trusted; nil: none), which connections are on in the workspace (by Key; nil: all), and where a server it can't serve is said (nil: nowhere).

type Serving

type Serving struct {
	Category Category
	Dirs
	Global []string
	Local  []string
	// Trusted says whether the person trusted a workspace connection file
	// (saved it in Studio, or trusted the project's settings): a trusted
	// one may use global secrets, and its read-only tools run without
	// asking. An untrusted one gets only the workspace's own secrets, and
	// every tool asks first. Nil trusts none.
	Trusted func(file string) bool
}

Serving is what a backend serves: a category's connection files of its kinds, the global ones then the workspace's.

func (Serving) Split

func (s Serving) Split() (trusted, untrusted []string)

Split is the workspace's files, trusted and not.

type Table

type Table struct {
	// Name is as a query names it (schema.table, dataset.table).
	Name string
	// About is its comment or description.
	About   string
	Columns []Column
}

Table is a table (or view) a connection holds, as its schema says.

func ReadSchema

func ReadSchema(ctx context.Context, cat Catalog, env Env, conn Connection) ([]Table, error)

ReadSchema reads conn's tables and their columns: ErrNoTables, saying why, when it can read none.

type Tester

type Tester interface {
	Test(ctx context.Context, env Env, k *Kind, c Connection) ([]ToolInfo, error)
}

Tester connects to a connection as written and lists its tools.

type Text

type Text map[string]string

Text is a descriptor's text in English and other languages: written as a string (English) or a map of language tags to text (en, es, fr-CA).

func (Text) In

func (t Text) In(lang string) string

In is the text in lang (es, fr-CA, fr), else English.

func (*Text) UnmarshalYAML

func (t *Text) UnmarshalYAML(unmarshal func(any) error) error

UnmarshalYAML reads a string or a map.

type ToolInfo

type ToolInfo struct {
	Name        string
	Description string
	// ReadOnly tools run without asking.
	ReadOnly bool
}

ToolInfo is a tool a connection offers.

func TestConnection

func TestConnection(ctx context.Context, cat Catalog, env Env, conn Connection) ([]ToolInfo, error)

TestConnection connects to conn as written and lists its tools.

type Tryer

type Tryer interface {
	Try(ctx context.Context, env Env, k *Kind, c Connection, tool string, args map[string]any) (string, bool, error)
}

Tryer runs one of a connection's tools (<connection>_<tool>, or the tool alone) as written, with args: what it answered, and whether that's the tool's error.

Directories

Path Synopsis
Package builtin holds the connector descriptors Blitz ships with (spec_connectors_047): categories/<id>.yaml and kinds/<id>.yaml, embedded in the binary.
Package builtin holds the connector descriptors Blitz ships with (spec_connectors_047): categories/<id>.yaml and kinds/<id>.yaml, embedded in the binary.

Jump to

Keyboard shortcuts

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