Documentation
¶
Overview ¶
Package contract defines the rate limiter contract that enforcement adapters use, without the Redis-backed limiter in pkg/ratelimit.
Index ¶
Constants ¶
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 ¶
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 ¶
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 ¶
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