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 ¶
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 ¶
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.
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.
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, except for a leading WITH, which is resolved by scanning for a data-modifying keyword (see ClassifyQuery).
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.
A leading WITH is the one case that needs more than the first token: SQL allows a common table expression to head a read-only SELECT but also an INSERT, UPDATE or DELETE, so classifying every WITH as read-only would let a mutation through the gate. Such statements are resolved by classifyWith.
func (QueryClass) String ¶
func (c QueryClass) String() string
String renders a QueryClass for audit logging and error messages.