ratelimit

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package ratelimit provides rate limiting functionality for the MCP server

Index

Constants

View Source
const (
	DefaultTool = "default"
)

TODO: make these constants configurable

Variables

View Source
var DefaultConfig = map[string]int{

	types.ListResourcesToolName: readLimit,
	types.GetResourceToolName:   readLimit,

	types.ApplyResourceToolName:  writeLimit,
	types.DeleteResourceToolName: writeLimit,

	DefaultTool: defaultLimit,
}

DefaultConfig defines the default rate limits for different tools

Functions

func SetSessionIDToContext

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

SetSessionIDToContext adds the session ID to a context

Types

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 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