pinning

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: GPL-3.0 Imports: 15 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 (
	KindInvisible   = "invisible"
	KindInstruction = "instruction"
	KindCredentials = "credentials"
	KindShadowing   = "shadowing"
)

Finding kinds.

Variables

This section is empty.

Functions

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.

Types

type Definition

type Definition struct {
	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"`
}

Definition is the part of a tool that is pinned: everything the model reads and everything that decides how the tool is called. _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.

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 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) 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 tool.

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