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
- Variables
- func EntryPath(server, version string) (string, error)
- func Publish(dir string, e Entry) (string, error)
- func Reveal(s string) string
- func StripInvisible(s string) (string, int)
- func VisibleText(s string) string
- type Agreement
- type Comparison
- type Definition
- type Drift
- type Entry
- type EntryItem
- type Finding
- type Lockfile
- func (l *Lockfile) Check(upstream string, defs []Definition, others []OtherTool) []Report
- func (l *Lockfile) Known(upstream string) bool
- func (l *Lockfile) NoteDrift(r Report, now time.Time) bool
- func (l *Lockfile) Path() string
- func (l *Lockfile) Pin(upstream string, d Definition, accepted []string, now time.Time)
- func (l *Lockfile) Pinned(upstream, tool string) *Pin
- func (l *Lockfile) Save() error
- func (l *Lockfile) Status(upstream string, d Definition) Status
- func (l *Lockfile) Trust(r Report, now time.Time)
- func (l *Lockfile) Unpin(upstream, tool string)
- type OtherTool
- type Pin
- type Report
- type Status
- type Upstream
Constants ¶
const ( KindPrompt = "prompt" KindResource = "resource" KindTemplate = "resource-template" KindInstructions = "instructions" )
Kinds of definition other than a tool.
const ( KindInvisible = "invisible" KindInstruction = "instruction" KindCredentials = "credentials" KindShadowing = "shadowing" )
Finding kinds.
Variables ¶
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
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
Publish writes an entry into a registry directory and returns the file.
func Reveal ¶
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 ¶
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
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.
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) 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.
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 ¶
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.
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 (*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) NoteDrift ¶
NoteDrift records that a tool changed, once per new definition. It reports whether this drift is news.
func (*Lockfile) Pin ¶
Pin trusts a definition: it becomes the pinned one, any drift is cleared, and the given findings are recorded as accepted.
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.
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" )