policy

package
v0.10.542 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package policy classifies SQL queries and decides whether the server should allow, refuse, or seek approval for a tool call based on the configured server mode. Pure functions only - no I/O, no SDK types, no logging.

Index

Constants

View Source
const (
	ModeReadOnly   = "read_only"
	ModeSafe       = "safe"
	ModeDeleteSafe = "delete_safe"
	ModeFullAccess = "full_access"
)

Server modes. These are the legal values for Config.Server.Mode.

Variables

This section is empty.

Functions

func IsLegalMode

func IsLegalMode(mode string) bool

IsLegalMode reports whether the given string is one of the four legal modes (or the empty string, which is treated as the default elsewhere).

Types

type Decision

type Decision int

Decision is what GateDecision returns: what the server wants to do with a tool call given its mode and the class of the query.

const (
	DecisionAllow Decision = iota
	DecisionRefuseImmediate
	DecisionNeedsApproval
)

func GateDecision

func GateDecision(mode string, class QueryClass) (Decision, string)

GateDecision is pure: given the mode and the class of the query, what does the server want to do? The reason is a human-readable phrase suitable for an error message ("server is in 'read_only' mode").

An empty or unknown mode is treated as the safe default.

func (Decision) String

func (d Decision) String() string

String renders a Decision for audit logging. These string forms are part of the audit event contract.

type Policy

type Policy interface {
	Class() QueryClass
	Decision() Decision
	Reason() string
}

Policy is the composite output of classifying a SQL statement and applying the gate decision for the configured server mode. Callers use NewPolicy at the boundary, then read Class / Decision / Reason in whichever combination they need. The interface is unexported in spirit (it carries inherent types only and never crosses out of the policy package as a typed value beyond what mcp_server consumes internally), but is exposed for explicit type assertions in tests.

func NewPolicy

func NewPolicy(mode, sql string, defaultClass QueryClass) Policy

NewPolicy is the factory. When sql is empty (no SQL input on a metadata tool, for example), defaultClass becomes the effective class; otherwise the classifier inspects the SQL's first token. Mode is normalised internally so an empty / unknown value behaves like ModeSafe.

type QueryClass

type QueryClass int

QueryClass identifies the kind of statement a query tool is being asked to run. The classifier is intentionally shallow: it looks at the first token only.

const (
	QueryClassUnknown QueryClass = iota
	QueryClassSelect
	QueryClassMutationCreate // INSERT, UPDATE, REPLACE, MERGE, UPSERT
	QueryClassMutationDelete // DELETE
	QueryClassLifecycle      // EXEC
)

func ClassifyQuery

func ClassifyQuery(sql string) QueryClass

ClassifyQuery returns the class of the SQL by inspecting only the first whitespace-separated token. It does not parse the statement. Empty or unrecognised inputs return QueryClassUnknown.

func (QueryClass) String

func (c QueryClass) String() string

String renders a QueryClass for audit logging and error messages.

Jump to

Keyboard shortcuts

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