mcp

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package mcp is 012 mcp: a Model Context Protocol server on the workbooks in the folders open to it (see docs/agents/mcp.md). Its tools, resources and prompts are a thin layer over internal/headless, the same code 012 get, set and describe run, so agents' changes go through the workbook's one mutation path (Batch) and the same checks as a person's, and are shown by 012 diff. Each workbook stands behind a Backend: a file here, and for live mode a session.

Index

Constants

View Source
const SuggestionsURI = scheme + "live/suggestions"

SuggestionsURI is the resource listing the agent's suggestions, as the suggestions tool does; clients subscribed to it are told when the person settles one (SuggestionsChanged).

Variables

This section is empty.

Functions

func NewRoots added in v0.5.0

func NewRoots(dirs []string) ([]confine.Root, error)

NewRoots confines paths to dirs, the working directory when there are none.

Types

type Backend

type Backend interface {
	// Name is what the workbook is called, for titles: its file's name.
	Name() string
	// View runs fn on the workbook as it is, which fn must not change.
	View(ctx context.Context, fn func(*sheet.Workbook) error) error
	// Scratch runs fn on a copy of the workbook that is thrown away.
	Scratch(ctx context.Context, fn func(*sheet.Workbook) error) error
	// Change runs fn on the workbook inside one Batch, labelled label,
	// and returns what it changed. The change is kept unless fn fails or
	// dryRun is set; a failed change leaves the workbook as it was.
	Change(ctx context.Context, label string, dryRun bool, fn func(*sheet.Workbook) error) ([]diff.Change, error)
}

Backend is where the tools' workbook lives. The tools only read it, work on a copy thrown away, or change it through the workbook's one mutation path (Batch), so the file backend here and a live session's (a workbook open on the screen, changed as a participant's operations) can stand behind the same tools.

type Changed

type Changed struct {
	Saved    bool              `json:"saved"`
	Changes  []headless.Change `json:"changes"`
	Warnings []string          `json:"warnings"`
	// Suggestion and Notices are live mode's: the change waiting for
	// the person, and what became of earlier ones.
	Suggestion int      `json:"suggestion,omitempty" jsonschema:"live mode: the suggestion's number, waiting for the person to accept or reject it"`
	Notices    []string `json:"notices,omitempty" jsonschema:"live mode: what became of your earlier suggestions"`
}

Changed is what a write returns: whether it was kept, what changed as 012 diff lists it, and warnings of entries a rule marks invalid.

type FileBackend

type FileBackend struct {
	Path string
	// Prepare, when set, runs on the workbook once opened, before fn:
	// what the server's flags allow (answering JEV functions).
	Prepare func(context.Context, *headless.File) error
	// Save writes a changed workbook; nil is File.Save.
	Save func(*headless.File) error
	// contains filtered or unexported fields
}

FileBackend is a workbook file, opened afresh for every call so edits made meanwhile by others (the screen, git, 012 set) are seen, and saved atomically after each change, as 012 set saves it.

func (*FileBackend) Change

func (b *FileBackend) Change(ctx context.Context, label string, dryRun bool, fn func(*sheet.Workbook) error) ([]diff.Change, error)

func (*FileBackend) Name

func (b *FileBackend) Name() string

Name is the file's name without its folder.

func (*FileBackend) Scratch

func (b *FileBackend) Scratch(ctx context.Context, fn func(*sheet.Workbook) error) error

Scratch is View: every call opens its own copy of the file.

func (*FileBackend) View

func (b *FileBackend) View(ctx context.Context, fn func(*sheet.Workbook) error) error

type ImportBackend added in v0.5.0

type ImportBackend struct {
	Path    string
	Prepare func(context.Context, *headless.File) error
}

ImportBackend is a file in a format 012 imports (CSV, XLSX, SQLite and the rest), read afresh for every call as the app opens it. It is read only: 012 writes a workbook as a .012 file, which create_workbook makes from it.

func (*ImportBackend) Change added in v0.5.0

func (*ImportBackend) Name added in v0.5.0

func (b *ImportBackend) Name() string

Name is the file's name without its folder or extension.

func (*ImportBackend) Scratch added in v0.5.0

func (b *ImportBackend) Scratch(ctx context.Context, fn func(*sheet.Workbook) error) error

Scratch is View: every call imports its own copy.

func (*ImportBackend) View added in v0.5.0

func (b *ImportBackend) View(ctx context.Context, fn func(*sheet.Workbook) error) error

type Live added in v0.6.0

type Live interface {
	Backend
	// Make makes a change as the agent's, or suggests it.
	Make(ctx context.Context, m Making) (Made, error)
	// Focus moves the agent's pointer to ref, for the person to see
	// where it's looking, and says where that is.
	Focus(ctx context.Context, ref string) (string, error)
	// Undo takes back the agent's latest step, saying what it was.
	Undo(ctx context.Context) (string, error)
	// Ask asks the person a question, as an elicitation request asks a
	// host's user, and returns their answer.
	Ask(ctx context.Context, p *sdk.ElicitParams) (*sdk.ElicitResult, error)
	// Suggestions are the agent's suggestions and what became of them.
	Suggestions(ctx context.Context) ([]Suggestion, error)
	// RunCell has the person's session run a notebook cell, once they
	// allow it, and says what happens next.
	RunCell(ctx context.Context, notebook string, cell int) (string, error)
	// Scope is what the person let the agent change, in words.
	Scope() string
}

Live is a workbook open in a session, as the agent attached to it works on it.

type Made added in v0.6.0

type Made struct {
	Changes    []diff.Change
	Applied    bool
	Suggestion int
	Notices    []string
}

Made is what came of a change: what it changes, whether it was made on the workbook or waits as a suggestion, and news of the agent's earlier suggestions.

type Making added in v0.6.0

type Making struct {
	Label, Message string
	DryRun         bool
	Fn             func(*sheet.Workbook) error
}

Making is a change a tool asks for: its label, the agent's message shown with it, whether only to see it, and the change itself.

type Options

type Options struct {
	// Version is 012's, which the server reports.
	Version string
	// ReadOnly leaves out every tool that changes a workbook.
	ReadOnly bool
	// Force lets writes change protected ranges, as 012 set --force.
	Force bool
	// Notebooks, when set, lets run_notebook_cell run cells with nu, as
	// --notebooks does; nil leaves the tool out. MayRun says why a
	// workbook's cells may not run here (trust, the shell setting), and
	// may mark it trusted, as the command line's --trust does.
	Notebooks *headless.NotebookOptions
	MayRun    func(path string, w *sheet.Workbook) error
	// Roots are the folders workbooks may be in when the client shares
	// none of its own (NewRoots makes them).
	Roots []confine.Root
	// Default, an absolute path, is the workbook of tools called without
	// a path, open wherever it is; "" makes the path required.
	Default string
	// Prepare, when set, runs on every workbook once opened (answering
	// JEV functions); Save writes a changed one, nil being File.Save.
	Prepare func(context.Context, *headless.File) error
	Save    func(*headless.File) error
	// Live, when set, is the one workbook of a server attached to a
	// session (live mode, live.go): every tool works on it, and the
	// live tools are added. Roots, Default and Notebooks go unused.
	Live Live
}

Options say where the server's workbooks are and what it may do beyond reading and writing cells.

type RangeRead added in v0.5.0

type RangeRead = headless.Range

RangeRead is headless.Range under another name, which a struct embedding it can have beside its own field Range.

type Server

type Server struct {
	*sdk.Server
	// contains filtered or unexported fields
}

Server is the MCP server on the workbooks open to it.

func New

func New(o Options) *Server

New makes the server with its tools, resources and prompts.

func (*Server) SuggestionsChanged added in v0.6.0

func (s *Server) SuggestionsChanged(ctx context.Context)

SuggestionsChanged tells clients subscribed to SuggestionsURI that the person settled a suggestion.

type Suggestion added in v0.6.0

type Suggestion struct {
	ID       int    `json:"id"`
	Label    string `json:"label"`
	Message  string `json:"message,omitempty"`
	State    string `json:"state" jsonschema:"pending, accepted, accepted in part or rejected"`
	Cells    int    `json:"cells" jsonschema:"the cells it sets"`
	Accepted int    `json:"accepted" jsonschema:"the cells the person accepted"`
	Rejected int    `json:"rejected" jsonschema:"the cells the person rejected"`
}

Suggestion is one of the agent's suggestions as the suggestions tool lists it.

type View added in v0.5.0

type View struct {
	Title string `json:"title"`
	Name  string `json:"name,omitempty"`
	Where string `json:"where"`
	HTML  string `json:"html"`
}

View is what a view draws of a result, in its _meta under viewKey: the workbook's name, what the name box shows before a cell is pointed at (a chart's data), the range or the chart's kind, sheet and data, and the grid or chart as the HTML export draws it.

Jump to

Keyboard shortcuts

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