pinning

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: GPL-3.0 Imports: 19 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 pinning keeps MCP servers from changing their tools behind the user's back.

A tool's definition — its description above all — is read by the model as instructions. A server that was harmless when it was installed can start sending a different description later (a "rug pull"), and one that ships a poisoned description from the start can hide instructions in it that the user never sees. This package pins every tool definition in a lockfile the first time it is seen, reports any later change as drift, and scans definitions for the tricks poisoned ones use.

Index

Constants

View Source
const (
	KindPrompt       = "prompt"
	KindResource     = "resource"
	KindTemplate     = "resource-template"
	KindInstructions = "instructions"
)

Kinds of definition other than a tool.

View Source
const (
	KindInvisible   = "invisible"
	KindInstruction = "instruction"
	KindCredentials = "credentials"
	KindShadowing   = "shadowing"
)

Finding kinds.

Variables

View Source
var ErrNotInRegistry = errors.New("not in the registry")

ErrNotInRegistry is returned when a registry has no entry for a server version.

Functions

func EntryPath added in v0.5.0

func EntryPath(server, version string) (string, error)

EntryPath is where an entry lives in a registry: <server>/<version>.json, with a scoped name like @scope/name kept as two directories and anything else that is not safe in a path replaced.

func Publish added in v0.5.0

func Publish(dir string, e Entry) (string, error)

Publish writes an entry into a registry directory and returns the file.

func Reveal

func Reveal(s string) string

Reveal renders what a text hides: tag characters are spelled out in «hidden: …», and every other invisible or reordering character is shown as its code point. What is left is exactly what the model reads.

func StripInvisible

func StripInvisible(s string) (string, int)

StripInvisible removes every character that renders as nothing or reorders what is rendered, and reports how many it removed.

func VisibleText added in v0.5.0

func VisibleText(s string) string

VisibleText is what a pattern should look at: hidden characters are taken out, so they cannot be used to split a phrase, and hidden tag text is spelled out, so what it says is checked too.

Types

type Agreement added in v0.5.0

type Agreement string

Agreement is how one definition compares with a registry.

const (
	AgreeSame      Agreement = "same"
	AgreeDiffers   Agreement = "differs"
	AgreeOnlyHere  Agreement = "only-here"
	AgreeOnlyThere Agreement = "only-in-registry"
)

The ways a definition and a registry can relate.

type Comparison added in v0.5.0

type Comparison struct {
	Ref   string      `json:"ref"`
	Agree Agreement   `json:"agree"`
	Here  *Definition `json:"here,omitempty"`
	There *Definition `json:"there,omitempty"`
}

Comparison is one definition against the registry.

func Compare added in v0.5.0

func Compare(here []Definition, there *Entry) []Comparison

Compare lines up what a server offers here with a registry entry.

type Definition

type Definition struct {
	// Kind is empty for a tool, else one of the Kind constants.
	Kind         string          `json:"kind,omitempty"`
	Name         string          `json:"name"`
	Title        string          `json:"title,omitempty"`
	Description  string          `json:"description,omitempty"`
	InputSchema  json.RawMessage `json:"inputSchema,omitempty"`
	OutputSchema json.RawMessage `json:"outputSchema,omitempty"`
	Annotations  json.RawMessage `json:"annotations,omitempty"`
	// Arguments are a prompt's arguments.
	Arguments json.RawMessage `json:"arguments,omitempty"`
	// URI is a resource's URI, or a resource template's URI template.
	URI      string `json:"uri,omitempty"`
	MIMEType string `json:"mimeType,omitempty"`
}

Definition is the part of what a server offers that is pinned: everything the model reads and everything that decides how it is used. A tool is the common case; a prompt, a resource, a resource template and the server's instructions are pinned the same way, because the model reads them too. _meta and icons are left out; they are not shown to the model.

func DefinitionFromJSON

func DefinitionFromJSON(raw []byte) (Definition, error)

DefinitionFromJSON extracts a Definition from a tool as it travels on the wire.

func (Definition) Hash

func (d Definition) Hash() string

Hash identifies a definition's content.

func (Definition) Ref added in v0.5.0

func (d Definition) Ref() string

Ref is the key a definition is pinned under: a tool's name, so lockfiles written before other kinds were pinned keep their meaning, and kind:name for the rest — prompt:review, resource:file:///notes/a.md, resource-template:file:///notes/{name}, instructions.

func (Definition) What added in v0.5.0

func (d Definition) What() string

What names the kind of definition in a sentence: "tool", "prompt", ….

type Drift

type Drift struct {
	Hash       string     `json:"hash"`
	SeenAt     time.Time  `json:"seen_at"`
	Definition Definition `json:"definition"`
}

Drift is a definition that differs from the pinned one.

type Entry added in v0.5.0

type Entry struct {
	Server      string               `json:"server"`
	Version     string               `json:"version"`
	Definitions map[string]EntryItem `json:"definitions"`
}

Entry is one server version as a registry keeps it. It names no local upstream and carries no times, so two people who trust the same server write the same bytes.

func LoadEntry added in v0.5.0

func LoadEntry(ctx context.Context, source, server, version string, client *http.Client) (*Entry, error)

LoadEntry finds the entry for a server version. source is a registry directory or base URL, or a single entry file or URL ending in .json, which is used whatever server it names.

func NewEntry added in v0.5.0

func NewEntry(server, version string, defs []Definition) Entry

NewEntry builds an entry from definitions.

func (Entry) Marshal added in v0.5.0

func (e Entry) Marshal() []byte

Marshal is the entry as a registry file: indented, keys sorted.

type EntryItem added in v0.5.0

type EntryItem struct {
	Hash       string     `json:"hash"`
	Definition Definition `json:"definition"`
}

EntryItem is one definition, keyed by its Ref in Entry.Definitions.

type Finding

type Finding struct {
	// Kind is invisible, instruction, credentials or shadowing.
	Kind string `json:"kind"`
	// Where is the part of the definition it was found in: description,
	// title, or a path into a schema ("inputSchema.properties.note.description").
	Where string `json:"where"`
	// Detail says what was found, in words.
	Detail string `json:"detail"`
	// Excerpt is the text in question. For hidden characters it is what they
	// spell out, which is usually the whole point of hiding them.
	Excerpt string `json:"excerpt,omitempty"`
}

Finding is one suspicious thing in a tool definition.

func Scan

func Scan(d Definition, others []OtherTool) []Finding

Scan looks through a definition for the marks of a poisoned tool: text the user cannot see, instructions addressed to the model rather than a description of the tool, pointers at credentials, and talk about other servers' tools.

func ScanResult

func ScanResult(text string) []Finding

ScanResult looks through what a tool returned for text aimed at the model rather than the user: hidden characters and instructions. Pointers at credentials are left out here; a result that mentions .env files is usually just a result about .env files.

func (Finding) Key

func (f Finding) Key() string

Key identifies a finding for acceptance: the same kind of thing in the same place.

type Lockfile

type Lockfile struct {
	Version   int                  `json:"version"`
	Upstreams map[string]*Upstream `json:"upstreams"`
	// contains filtered or unexported fields
}

Lockfile is the record of every tool definition a human has trusted, grouped by upstream.

func Load

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

Load reads a lockfile. A missing file is an empty lockfile.

func (*Lockfile) Check

func (l *Lockfile) Check(upstream string, defs []Definition, others []OtherTool) []Report

Check compares the tools an upstream offers with what is pinned, and scans each current definition. others are the tools of every other upstream, which a definition should have no reason to talk about.

func (*Lockfile) Known

func (l *Lockfile) Known(upstream string) bool

Known reports whether an upstream has been pinned before.

func (*Lockfile) NoteDrift

func (l *Lockfile) NoteDrift(r Report, now time.Time) bool

NoteDrift records that a tool changed, once per new definition. It reports whether this drift is news.

func (*Lockfile) Path

func (l *Lockfile) Path() string

Path is where the lockfile lives.

func (*Lockfile) Pin

func (l *Lockfile) Pin(upstream string, d Definition, accepted []string, now time.Time)

Pin trusts a definition: it becomes the pinned one, any drift is cleared, and the given findings are recorded as accepted.

func (*Lockfile) Pinned

func (l *Lockfile) Pinned(upstream, tool string) *Pin

Pinned returns the pin of one tool, if there is one.

func (*Lockfile) Save

func (l *Lockfile) Save() error

Save writes the lockfile atomically: a reader never sees half of it.

func (*Lockfile) Status added in v0.5.0

func (l *Lockfile) Status(upstream string, d Definition) Status

Status reports how one definition stands with the lockfile, without the scan; Check does both for a whole upstream.

func (*Lockfile) Trust

func (l *Lockfile) Trust(r Report, now time.Time)

Trust pins the current definition of a report and accepts its findings.

func (*Lockfile) Unpin

func (l *Lockfile) Unpin(upstream, tool string)

Unpin forgets a tool, or a whole upstream when tool is empty.

type OtherTool

type OtherTool struct {
	Upstream string
	Name     string
}

OtherTool is a tool of another upstream, for the shadowing check.

type Pin

type Pin struct {
	Hash       string     `json:"hash"`
	PinnedAt   time.Time  `json:"pinned_at"`
	Definition Definition `json:"definition"`
	// Accepted lists the scan findings a human looked at and accepted for
	// this exact definition.
	Accepted []string `json:"accepted_findings,omitempty"`
	// Drift is set when the server started offering something else under
	// this name. It stays until a human pins the tool again.
	Drift *Drift `json:"drift,omitempty"`
}

Pin is one tool as it was trusted.

type Report

type Report struct {
	Upstream string     `json:"upstream"`
	Tool     string     `json:"tool"`
	Status   Status     `json:"status"`
	Current  Definition `json:"current"`
	// Pinned is the trusted definition, for changed and removed tools.
	Pinned *Definition `json:"pinned,omitempty"`
	// Findings are what the scan found in the current definition, minus the
	// ones a human already accepted for it.
	Findings []Finding `json:"findings,omitempty"`
	// Accepted are the findings a human accepted for this definition.
	Accepted []Finding `json:"accepted,omitempty"`
}

Report is the verdict on one definition. Tool is its Ref.

type Status

type Status string

Status is how a tool on offer relates to the lockfile.

const (
	// StatusPinned means the tool is exactly what was trusted.
	StatusPinned Status = "pinned"
	// StatusNew means the upstream is pinned, but this tool is not.
	StatusNew Status = "new"
	// StatusChanged means the definition differs from the pinned one.
	StatusChanged Status = "changed"
	// StatusRemoved means the tool is pinned but no longer offered.
	StatusRemoved Status = "removed"
	// StatusUnpinned means nothing about this upstream is pinned yet.
	StatusUnpinned Status = "unpinned"
)

type Upstream

type Upstream struct {
	PinnedAt time.Time       `json:"pinned_at"`
	Tools    map[string]*Pin `json:"tools"`
}

Upstream is the pinned tools of one upstream server.

Jump to

Keyboard shortcuts

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