contract

package
v0.77.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: Apache-2.0 Imports: 5 Imported by: ? 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 contract defines the rate limiter contract that enforcement adapters use, without the Redis-backed limiter in pkg/ratelimit.

Index

Constants

View Source
const (
	// CodeRateLimited is the JSON-RPC error code for rate-limited requests,
	// mirroring HTTP 429 (Too Many Requests) the same way mcp.JSONRPCCodeDenied
	// mirrors 403. It sits outside the JSON-RPC reserved range
	// (-32768..-32000) so a single value is conformant on every MCP revision:
	// 2026-07-28 reserves -32020..-32099 exclusively for spec-defined codes
	// and directs application codes outside the reserved range, while
	// 2025-11-25 and earlier inherit plain JSON-RPC 2.0, which leaves the
	// space outside that range application-defined. Replaces -32029, chosen
	// by RFC THV-0057 when -32000..-32099 was uniformly implementation-defined
	// — a premise the 2026-07-28 partition invalidated (#6101).
	CodeRateLimited int64 = 429

	// MessageRateLimited is the error message for rate-limited requests.
	MessageRateLimited = "Rate limit exceeded"
)

Variables

This section is empty.

Functions

func Allow

func Allow(ctx context.Context, limiter Limiter, identity *auth.Identity, toolName string) error

Allow checks whether identity may call toolName through limiter. When the built-in limiter returns an infrastructure error, Allow records fail-open observability before preserving the error for the HTTP or vMCP adapter to log and apply its existing fail-open behavior.

Types

type Decision

type Decision struct {
	// Allowed is true when the request may proceed.
	Allowed bool

	// RetryAfter is populated when Allowed is false.
	// It indicates the minimum wait before the next request may succeed.
	RetryAfter time.Duration
}

Decision holds the result of a rate limit check.

type FailOpenObserver

type FailOpenObserver interface {
	RecordFailOpen(context.Context)
}

FailOpenObserver records the policy decision made by production enforcement adapters after the built-in limiter returns an infrastructure error.

type Limiter

type Limiter interface {
	// Allow checks whether a request is permitted.
	// toolName is the MCP tool being called (empty for non-tool requests).
	// userID is the authenticated user (empty for unauthenticated requests).
	Allow(ctx context.Context, toolName, userID string) (*Decision, error)
}

Limiter checks rate limits for an MCP server.

type RateLimitedError

type RateLimitedError struct {
	RetryAfter time.Duration
}

RateLimitedError reports that a request exceeded its configured rate limit.

func (*RateLimitedError) Code

func (*RateLimitedError) Code() int64

Code returns the ToolHive JSON-RPC-compatible code for rate-limited requests.

func (*RateLimitedError) Data

func (e *RateLimitedError) Data() map[string]any

Data returns structured retry metadata for transport adapters that cannot emit a custom JSON-RPC error object from the tool-handler seam.

func (*RateLimitedError) Error

func (*RateLimitedError) Error() string

Jump to

Keyboard shortcuts

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