tools

package
v0.0.0-...-26bba4b Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 27 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 tools is the tool framework: Spec/Action/Registry, schema generation, help, version gating and the Call context (DESIGN.md sections 2 and 4).

Index

Constants

View Source
const (
	// BudgetTotalBytes caps the tools/list JSON of all non-admin toolsets.
	BudgetTotalBytes = 75000
	// BudgetToolBytes caps the schema JSON of one tool.
	BudgetToolBytes = 6000
	// BudgetDescriptionChars caps Spec.Description.
	BudgetDescriptionChars = 160
)

Schema-size budget (TestSchemaBudget). Frozen by WP4 after measuring the real tools/list JSON (name + description + inputSchema + annotations) of stub param structs with about 45 properties and action-prefixed descriptions (see TestMeasureStubSchemas):

pr    (11 actions) 4.1-4.4 KB    issue (18 actions) 4.5-5.0 KB
repo  (20 actions) 4.9-5.5 KB    run   (12 actions) 4.3-4.6 KB

(lower figure: 8 params per action, upper: 12). The widest tool therefore needs about 5.5 KB, so 6000 bytes per tool holds with roughly 8% headroom. Measured on the finished server (docs/VERIFIED.md): the 29 default tools are 51,804 bytes of tools/list JSON (about 13k tokens), all 30 tools are 55,529 bytes, and the widest tool (repo) is 4,259 bytes. The limits below keep headroom above those figures; rarely used params belong to forgejo_api instead.

Variables

View Source
var Toolsets = []string{
	"meta", "repos", "pulls", "issues", "actions", "runners", "settings", "users", "packages", "admin",
}

Toolsets lists every toolset name (a tool belongs to exactly one). "meta" is always on and cannot be disabled; "admin" is off by default.

Functions

func BuildInputSchema

func BuildInputSchema(s Spec, allowed []Action) (*jsonschema.Schema, error)

BuildInputSchema builds the tool input schema (section 2): infer with jsonschema.For from Spec.Params, replace properties.action with {type:string, enum:[allowed + "help"]}, require only `action`, set additionalProperties:false and prefix every property description with the `[actions]` that read it (`*` marks required).

Single tools have no `action` property and no `help`; their required list is the one of their only action and descriptions carry no prefix.

func CheckMinVersion

func CheckMinVersion(tool, action string, declared int, v forgejo.Version) string

CheckMinVersion returns the error text of section 5.4 (`<tool>.<action> needs Forgejo >= N (server is X)`) or "" when satisfied.

func HelpText

func HelpText(s Spec, allowed []Action, action string) string

HelpText renders the implicit `help` action: the action table of the tool (`name [R|W|D] params(required marked *) - summary`) or, when action is non-empty, the full parameter list of that one action. Generated from Action.Params/Required/Summary, never hand-written twice.

func HostOf

func HostOf(raw string) string

HostOf returns the host[:port] of an instance URL (the whole string when it does not parse).

func Instructions

func Instructions(in InstructionsInfo) string

Instructions renders the <= 700 character `instructions` text of section 4.1. Sentences about tools in hidden toolsets are omitted.

func MinMajorFor

func MinMajorFor(tool, action string, declared int) int

MinMajorFor returns the effective minimum major for tool.action: the larger of the table value and the value the Action itself declares.

func ResolveToolsets

func ResolveToolsets(spec string) (map[string]bool, error)

ResolveToolsets evaluates a FORGEJO_TOOLSETS value (names, "all", "-name"; unknown name is an error listing valid names; "meta" is always added).

Types

type Action

type Action struct {
	Name      string
	Mode      Mode
	Summary   string   // one line, used by `help`
	Params    []string // json names of the params this action reads
	Required  []string // json names; "repo" is added automatically when Spec.RepoScoped and NoRepo is false
	NoRepo    bool
	Endpoints []string // "GET /repos/{owner}/{repo}/pulls" (coverage test + forgejo_endpoints "covered_by")
	MinMajor  int      // 0 = any supported
	Needs     string   // permission text for 403 hints, e.g. "repo admin"
	Hint500   string   // likely causes when the server hides a 500 message
	// ModeFor optionally classifies a call by its params (forgejo_api). The
	// registry keeps such an action when read-only is on and applies ModeFor
	// at call time.
	ModeFor func(in any) Mode
	// Run executes the action; in is a *T where T is the Spec.Params type.
	Run func(c *Call, in any) (*Result, error)
}

Action is one value of a tool's `action` enum.

func NewAction

func NewAction[T any](name string, mode Mode, run func(c *Call, p *T) (*Result, error), opts ...ActionOpt) Action

NewAction builds an Action whose Run receives the decoded *T.

type ActionOpt

type ActionOpt func(*Action)

ActionOpt customises an Action built by NewAction.

func Endpoints

func Endpoints(eps ...string) ActionOpt

Endpoints sets Action.Endpoints.

func Hint500

func Hint500(s string) ActionOpt

Hint500 sets Action.Hint500.

func MinMajor

func MinMajor(n int) ActionOpt

MinMajor sets Action.MinMajor.

func ModeFor

func ModeFor(f func(in any) Mode) ActionOpt

ModeFor sets Action.ModeFor.

func Needs

func Needs(s string) ActionOpt

Needs sets Action.Needs.

func NoRepo

func NoRepo() ActionOpt

NoRepo sets Action.NoRepo.

func Params

func Params(names ...string) ActionOpt

Params sets Action.Params.

func Required

func Required(names ...string) ActionOpt

Required sets Action.Required.

func Summary

func Summary(s string) ActionOpt

Summary sets Action.Summary.

type Call

type Call struct {
	Ctx         context.Context
	Limit       int // effective output cap for this call (6.6)
	Env         *Env
	Action      string
	Owner, Repo string // resolved when RepoScoped (empty otherwise)
	// Progress sends a progress notification; no-op when the client sent no
	// progress token.
	Progress func(msg string, done, total float64)
}

Call is the per-invocation context handed to Action.Run.

func (*Call) Cap

func (c *Call) Cap(s string) string

Cap is the last-resort cap to Call.Limit, head-keeping, cut at a line boundary; trailer lines survive (see format.CapHead).

func (*Call) CapTail

func (c *Call) CapTail(s string) string

CapTail is like Cap but keeps the END and cuts the beginning.

func (*Call) Chars

func (c *Call) Chars(requested, def int) int

Chars returns the effective per-action size: requested (or def) clamped to [200, Limit-1500] (section 6.6).

func (*Call) Confirm

func (c *Call) Confirm(got, want string) *Result

Confirm checks the `confirm` argument of a destructive action against the exact target name; it returns nil when they match.

func (*Call) Fail

func (c *Call) Fail(format string, a ...any) *Result

Fail returns an error Result with the text "error: <formatted>".

type Env

type Env struct {
	Client *forgejo.Client
	Cfg    *config.Config
	Repo   *gitremote.Detector
	Clock  clock.Clock
	// Memo holds in-process facts shared between tools, for example
	// Memo.Set("automerge:o/r#42") after a 201 from pr merge.
	Memo *Memo
}

Env is the shared runtime environment of every tool.

type InstructionsInfo

type InstructionsInfo struct {
	Host        string
	Version     string // "" before the first tool call
	DefaultRepo string // "" = none
	Toolsets    map[string]bool
	WaitMax     int // seconds
}

InstructionsInfo carries the values interpolated into the server `instructions` text (section 4.1).

type Memo

type Memo struct {
	// contains filtered or unexported fields
}

Memo is a mutex-protected string set.

func (*Memo) Has

func (m *Memo) Has(key string) bool

Has reports whether key was recorded.

func (*Memo) Set

func (m *Memo) Set(key string)

Set records key.

type Mode

type Mode uint8

Mode classifies an action. Destructive implies Write and requires `confirm`.

const (
	Read Mode = iota
	Write
	Destructive
)

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds every Spec and builds the filtered tool set.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty Registry.

func (*Registry) Add

func (r *Registry) Add(s Spec)

Add registers a Spec (before Build).

func (*Registry) Build

func (r *Registry) Build(cfg *config.Config) error

Build applies toolsets, FORGEJO_DENY and read-only (section 4 steps 1-4) and computes DeniedEndpoints and SideEffectGets.

func (*Registry) CoveredBy

func (r *Registry) CoveredBy(endpoint string) string

CoveredBy returns "tool.action" for an exact "METHOD template", or "".

func (*Registry) DeniedEndpoints

func (r *Registry) DeniedEndpoints() map[string]string

DeniedEndpoints returns the "METHOD template" strings blocked by deny rules or disabled toolsets (consumed by forgejo_api policy); the value is the "tool.action" that owns the endpoint.

func (*Registry) Enabled

func (r *Registry) Enabled() []Spec

Enabled returns the filtered Specs (only actions that survived). Before Build it returns every Spec.

func (*Registry) EnabledToolsets

func (r *Registry) EnabledToolsets() map[string]bool

EnabledToolsets returns the toolsets selected by the last Build (nil before).

func (*Registry) Install

func (r *Registry) Install(srv *mcp.Server, env *Env) error

Install registers every enabled Spec on srv with the raw Server.AddTool (handler order of section 2).

func (*Registry) SideEffectGets

func (r *Registry) SideEffectGets() []string

SideEffectGets returns the "GET template" endpoints that belong to non-read actions (sorted).

func (*Registry) Specs

func (r *Registry) Specs() []Spec

Specs returns every added Spec (unfiltered).

type Result

type Result struct {
	Text    string
	IsError bool
}

Result is a tool answer. Text is already capped by Cap/CapTail.

type Spec

type Spec struct {
	Name, Toolset, Description string // Description <= 160 chars
	Params                     any    // zero value of the flat params struct (json + jsonschema tags)
	Actions                    []Action
	RepoScoped                 bool // adds `repo` resolution before Run
	// Single marks a single-purpose tool: exactly one Action named "run", no
	// `action` property in the schema, no `help`.
	Single bool
}

Spec describes one MCP tool.

Directories

Path Synopsis
Package actions implements the `run` tool: Actions runs, jobs, logs, workflows, dispatch and artifacts (DESIGN.md sections 8.6 and 10.2).
Package actions implements the `run` tool: Actions runs, jobs, logs, workflows, dispatch and artifacts (DESIGN.md sections 8.6 and 10.2).
Package admin implements the `admin` tool (DESIGN.md section 8.11).
Package admin implements the `admin` tool (DESIGN.md section 8.11).
Package all registers every tool area.
Package all registers every tool area.
Command docgen writes docs/TOOLS.md from the tool registry.
Command docgen writes docs/TOOLS.md from the tool registry.
Package packages implements the `package` tool (DESIGN.md section 8.10).
Package packages implements the `package` tool (DESIGN.md section 8.10).
Package shared holds helpers used by several tool areas (DESIGN.md sections 1, 3.1, 5.6, 5.7, 10.5).
Package shared holds helpers used by several tool areas (DESIGN.md sections 1, 3.1, 5.6, 5.7, 10.5).
ci
Package ci holds the CI plumbing shared by the pr, checks, run, ci_failure and ci_wait tools (DESIGN.md sections 10.1-10.4).
Package ci holds the CI plumbing shared by the pr, checks, run, ci_failure and ci_wait tools (DESIGN.md sections 10.1-10.4).
ci/wait
Package wait implements the server-side polling of section 10.1 (ci_wait and run dispatch wait=true).
Package wait implements the server-side polling of section 10.1 (ci_wait and run dispatch wait=true).

Jump to

Keyboard shortcuts

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