Documentation
¶
Overview ¶
Package mutating implements a mutating webhook middleware for ToolHive. It calls external HTTP services to transform MCP requests using JSONPatch (RFC 6902).
Index ¶
- Constants
- func ApplyPatch(original []byte, patch []JSONPatchOp) ([]byte, error)
- func CreateMiddleware(config *types.MiddlewareConfig, runner types.MiddlewareRunner) error
- func IsPatchScopedToMCPRequest(patch []JSONPatchOp) bool
- func ValidatePatch(patch []JSONPatchOp) error
- type FactoryMiddlewareParams
- type JSONPatchOp
- type Middleware
- type MiddlewareParams
Constants ¶
const MiddlewareType = "mutating-webhook"
MiddlewareType is the type constant for the mutating webhook middleware.
Variables ¶
This section is empty.
Functions ¶
func ApplyPatch ¶
func ApplyPatch(original []byte, patch []JSONPatchOp) ([]byte, error)
ApplyPatch applies a set of RFC 6902 JSON Patch operations to the original JSON document. Returns the patched JSON document. The patch operations are applied in order.
func CreateMiddleware ¶
func CreateMiddleware(config *types.MiddlewareConfig, runner types.MiddlewareRunner) error
CreateMiddleware is the factory function for mutating webhook middleware.
func IsPatchScopedToMCPRequest ¶
func IsPatchScopedToMCPRequest(patch []JSONPatchOp) bool
IsPatchScopedToMCPRequest returns true if all patch operations target paths within the mcp_request container. This prevents webhooks from accidentally or maliciously modifying principal, context, or other immutable envelope fields. The root "/mcp_request" path is intentionally rejected so webhooks must make granular changes beneath the MCP request instead of replacing it wholesale.
func ValidatePatch ¶
func ValidatePatch(patch []JSONPatchOp) error
ValidatePatch checks that all operations in the patch are well-formed. It validates that all operations are supported RFC 6902 types and paths are non-empty.
Types ¶
type FactoryMiddlewareParams ¶
type FactoryMiddlewareParams struct {
MiddlewareParams
// ServerName is the name of the ToolHive instance.
ServerName string `json:"server_name"`
// Transport is the transport type (e.g., sse, stdio).
Transport string `json:"transport"`
}
FactoryMiddlewareParams extends MiddlewareParams with context for the factory.
type JSONPatchOp ¶
type JSONPatchOp struct {
// Op is the patch operation type (add, remove, replace, copy, move, test).
Op string `json:"op"`
// Path is the JSON Pointer (RFC 6901) path to apply the operation to.
Path string `json:"path"`
// Value is the value to use for add, replace, and test operations.
Value json.RawMessage `json:"value,omitempty"`
// From is the source path for copy and move operations.
From string `json:"from,omitempty"`
}
JSONPatchOp represents a single RFC 6902 JSON Patch operation.
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware wraps mutating webhook functionality for the factory pattern.
func (*Middleware) Close ¶
func (*Middleware) Close() error
Close cleans up any resources used by the middleware.
func (*Middleware) Handler ¶
func (m *Middleware) Handler() types.MiddlewareFunction
Handler returns the middleware function used by the proxy.
type MiddlewareParams ¶
type MiddlewareParams struct {
// Webhooks is the list of mutating webhook configurations to call.
// Webhooks are called in configuration order; each webhook receives the output
// of the previous mutation. All patches are applied sequentially.
Webhooks []webhook.Config `json:"webhooks"`
}
MiddlewareParams holds the configuration parameters for the mutating webhook middleware.
func (*MiddlewareParams) Validate ¶
func (p *MiddlewareParams) Validate() error
Validate checks that the MiddlewareParams are valid.