policy

package
v1.1.975 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 3 Imported by: 1 module, 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 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).

func IsLongRunningIaC added in v1.1.975

func IsLongRunningIaC(sql string) bool

IsLongRunningIaC reports whether sql is exactly one statement that converges or destroys a deployment, or applies a saved change set: an INSERT or DELETE whose target is the fully qualified stackql_preview.iac.run, or an INSERT into stackql_preview.iac.plan. The MCP server may run such a call as a task. Anything else, including a payload of several statements, is not eligible.

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 every statement in the SQL. 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. SELECT effects are inspected in the parser AST. Backend-owned view definitions and provider document effects are not available here.

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. Every statement in the payload (see sqlsplit.Statements) is classified, so a mutation cannot hide behind a leading read-only statement; the most privileged statement decides the class. 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