Skip to content

Commit 57d2880

Browse files
committed
design: add design for rate limiting
I added an example for how rate limiting should be implemented using middleware.
1 parent e5fbfa7 commit 57d2880

File tree

4 files changed

+85
-26
lines changed

4 files changed

+85
-26
lines changed

design/design.md

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -472,32 +472,7 @@ server.AddReceivingMiddleware(withLogging)
472472
473473
#### Rate Limiting
474474
475-
Rate limiting can be configured using middleware.
476-
477-
As an example, this code adds a per-session rate limiter using the [golang.org/x/time/rate](https://pkg.go.dev/golang.org/x/time/rate) package.
478-
479-
```go
480-
// PerSessionRateLimiterMiddleware creates a middleware that applies rate limiting
481-
// on a per-session basis for receiving requests.
482-
func PerSessionRateLimiterMiddleware[S Session](limit rate.Limit, burst int) Middleware[S] {
483-
var sessionLimiters sync.Map // map[*jsonrpc2.Connection]*rate.Limiter
484-
485-
return func(h MethodHandler[S]) MethodHandler[S] {
486-
return func(ctx context.Context, session S, method string, params Params) (Result, error) {
487-
conn := session.getConn()
488-
489-
limiter, _ := sessionLimiters.LoadOrStore(conn, rate.NewLimiter(limit, burst))
490-
rateLimiter := limiter.(*rate.Limiter)
491-
492-
if !rateLimiter.Allow() {
493-
return nil, jsonrpc2.ErrServerOverloaded
494-
}
495-
return h(ctx, session, method, params)
496-
}
497-
}
498-
}
499-
server.AddReceivingMiddleware(PerSessionRateLimiterMiddleware[*ServerSession](rate.Every(time.Second * 2), Burst: 5))
500-
```
475+
Rate limiting can be configured using middleware. Please see [examples/rate-limiting](<https://github.com/modelcontextprotocol/go-sdk/tree/main/examples/rate-limiting>] for an example on how to implement this.
501476
502477
### Errors
503478

examples/rate-limiting/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/modelcontextprotocol/go-sdk/examples/rate-limiting
2+
3+
go 1.25
4+
5+
require (
6+
github.com/modelcontextprotocol/go-sdk v0.0.0-20250625185707-09181c2c2e89
7+
golang.org/x/time v0.12.0
8+
)

examples/rate-limiting/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
2+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
3+
github.com/modelcontextprotocol/go-sdk v0.0.0-20250625185707-09181c2c2e89 h1:kUGBYP25FTv3ZRBhLT4iQvtx4FDl7hPkWe3isYrMxyo=
4+
github.com/modelcontextprotocol/go-sdk v0.0.0-20250625185707-09181c2c2e89/go.mod h1:DcXfbr7yl7e35oMpzHfKw2nUYRjhIGS2uou/6tdsTB0=
5+
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
6+
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
7+
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=
8+
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=

examples/rate-limiting/main.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

Comments
 (0)