|
| 1 | +// Copyright 2025 The Go MCP SDK Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by an MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "errors" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 13 | + "golang.org/x/time/rate" |
| 14 | +) |
| 15 | + |
| 16 | +// GlobalRateLimiterMiddleware creates a middleware that applies a global rate limit. |
| 17 | +// Every request attempting to pass through will try to acquire a token. |
| 18 | +// If a token cannot be acquired immediately, the request will be rejected. |
| 19 | +func GlobalRateLimiterMiddleware[S mcp.Session](limit rate.Limit, burst int) mcp.Middleware[S] { |
| 20 | + limiter := rate.NewLimiter(limit, burst) |
| 21 | + |
| 22 | + return func(next mcp.MethodHandler[S]) mcp.MethodHandler[S] { |
| 23 | + return func(ctx context.Context, session S, method string, params mcp.Params) (mcp.Result, error) { |
| 24 | + if !limiter.Allow() { |
| 25 | + return nil, errors.New("JSON RPC overloaded") |
| 26 | + } |
| 27 | + return next(ctx, session, method, params) |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// PerMethodRateLimiterMiddleware creates a middleware that applies rate limiting |
| 33 | +// on a per-method basis. |
| 34 | +// `methodLimits` maps method names to their specific rate limit configurations. |
| 35 | +// Methods not specified in `methodLimits` will not be rate limited by this middleware. |
| 36 | +func PerMethodRateLimiterMiddleware[S mcp.Session](methodLimits map[string]struct { |
| 37 | + Limit rate.Limit |
| 38 | + Burst int |
| 39 | +}) mcp.Middleware[S] { |
| 40 | + limiters := make(map[string]*rate.Limiter) |
| 41 | + for method, cfg := range methodLimits { |
| 42 | + limiters[method] = rate.NewLimiter(cfg.Limit, cfg.Burst) |
| 43 | + } |
| 44 | + |
| 45 | + return func(next mcp.MethodHandler[S]) mcp.MethodHandler[S] { |
| 46 | + return func(ctx context.Context, session S, method string, params mcp.Params) (mcp.Result, error) { |
| 47 | + if limiter, ok := limiters[method]; ok { |
| 48 | + if !limiter.Allow() { |
| 49 | + return nil, errors.New("JSON RPC overloaded") |
| 50 | + } |
| 51 | + } |
| 52 | + return next(ctx, session, method, params) |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func main() { |
| 58 | + server := mcp.NewServer("greeter1", "v0.0.1", nil) |
| 59 | + server.AddReceivingMiddleware(GlobalRateLimiterMiddleware[*mcp.ServerSession](rate.Every(time.Second/5), 10)) |
| 60 | + server.AddReceivingMiddleware(PerMethodRateLimiterMiddleware[*mcp.ServerSession](map[string]struct { |
| 61 | + Limit rate.Limit |
| 62 | + Burst int |
| 63 | + }{ |
| 64 | + "callTool": {Limit: rate.Every(time.Second), Burst: 5}, // 5 callTool requests/sec |
| 65 | + "listTools": {Limit: rate.Every(time.Minute), Burst: 20}, // 20 listTools requests/min |
| 66 | + })) |
| 67 | + // Run Server logic. |
| 68 | +} |
0 commit comments