ratelimit

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package ratelimit provides rate limiting functionality for the MCP server

Index

Constants

View Source
const (
	// DefaultTool is the key for the default rate limit
	DefaultTool = "default"
)

Variables

This section is empty.

Functions

func SetSessionIDToContext

func SetSessionIDToContext(ctx context.Context, sessionID string) context.Context

SetSessionIDToContext adds the session ID to a context

Types

type Config added in v0.3.0

type Config struct {
	// DefaultLimit is the default rate limit for tools not explicitly configured
	DefaultLimit int
	// ReadLimit is the rate limit for read operations (list_resources, get_resource)
	ReadLimit int
	// WriteLimit is the rate limit for write operations (apply_resource, delete_resource)
	WriteLimit int
}

Config holds the rate limiting configuration

func NewDefaultConfig added in v0.3.0

func NewDefaultConfig() *Config

NewDefaultConfig returns a Config with default values, optionally overridden by environment variables:

  • MKP_RATE_LIMIT_DEFAULT: default rate limit (default: 60)
  • MKP_RATE_LIMIT_READ: read operations rate limit (default: 120)
  • MKP_RATE_LIMIT_WRITE: write operations rate limit (default: 30)

type MockToolHandler

type MockToolHandler struct {
	mock.Mock
}

MockToolHandler for testing middleware

func (*MockToolHandler) Handle

Handle implements the ToolHandler interface for mock testing

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

RateLimiter implements a rate limiting middleware for MCP server Using a fixed window rate limiting algorithm

func GetDefaultRateLimiter

func GetDefaultRateLimiter() *RateLimiter

GetDefaultRateLimiter returns a RateLimiter with default configuration

func GetRateLimiterWithConfig added in v0.3.0

func GetRateLimiterWithConfig(config *Config) *RateLimiter

GetRateLimiterWithConfig returns a RateLimiter with the given configuration. If config is nil, default configuration is used.

func NewRateLimiter

func NewRateLimiter(opts ...RateLimiterOption) *RateLimiter

NewRateLimiter creates a new rate limiter with the given options

func (*RateLimiter) Middleware

func (rl *RateLimiter) Middleware() server.ToolHandlerMiddleware

Middleware returns a middleware function for the MCP server

func (*RateLimiter) Stop

func (rl *RateLimiter) Stop()

Stop stops the cleanup ticker

type RateLimiterOption

type RateLimiterOption func(*RateLimiter)

RateLimiterOption is a function that configures a RateLimiter

func WithDefaultLimit

func WithDefaultLimit(requestsPerMinute int) RateLimiterOption

WithDefaultLimit sets the default rate limit for all tools

func WithTimeWindow

func WithTimeWindow(window time.Duration) RateLimiterOption

WithTimeWindow sets the time window for rate limiting

func WithToolLimit

func WithToolLimit(toolName string, requestsPerMinute int) RateLimiterOption

WithToolLimit sets the rate limit for a specific tool

Jump to

Keyboard shortcuts

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