Skip to content

Commit 9712ae8

Browse files
committed
design: add design for rate limiting
I added an example for how rate limiting should be implemented using middleware. We create a go.mod to keep the root go.mod unaffected by the dependency on x/time.
1 parent 057f525 commit 9712ae8

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

design/design.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,10 @@ server.AddReceivingMiddleware(withLogging)
470470
471471
**Differences from mcp-go**: Version 0.26.0 of mcp-go defines 24 server hooks. Each hook consists of a field in the `Hooks` struct, a `Hooks.Add` method, and a type for the hook function. These are rarely used. The most common is `OnError`, which occurs fewer than ten times in open-source code.
472472
473+
#### Rate Limiting
474+
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.
476+
473477
### Errors
474478
475479
With the exception of tool handler errors, protocol errors are handled transparently as Go errors: errors in server-side feature handlers are propagated as errors from calls from the `ClientSession`, and vice-versa.

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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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](limiter *rate.Limiter) mcp.Middleware[S] {
20+
return func(next mcp.MethodHandler[S]) mcp.MethodHandler[S] {
21+
return func(ctx context.Context, session S, method string, params mcp.Params) (mcp.Result, error) {
22+
if !limiter.Allow() {
23+
return nil, errors.New("JSON RPC overloaded")
24+
}
25+
return next(ctx, session, method, params)
26+
}
27+
}
28+
}
29+
30+
// PerMethodRateLimiterMiddleware creates a middleware that applies rate limiting
31+
// on a per-method basis.
32+
// Methods not specified in limiters will not be rate limited by this middleware.
33+
func PerMethodRateLimiterMiddleware[S mcp.Session](limiters map[string]*rate.Limiter) mcp.Middleware[S] {
34+
return func(next mcp.MethodHandler[S]) mcp.MethodHandler[S] {
35+
return func(ctx context.Context, session S, method string, params mcp.Params) (mcp.Result, error) {
36+
if limiter, ok := limiters[method]; ok {
37+
if !limiter.Allow() {
38+
return nil, errors.New("JSON RPC overloaded")
39+
}
40+
}
41+
return next(ctx, session, method, params)
42+
}
43+
}
44+
}
45+
46+
func main() {
47+
server := mcp.NewServer("greeter1", "v0.0.1", nil)
48+
server.AddReceivingMiddleware(GlobalRateLimiterMiddleware[*mcp.ServerSession](rate.NewLimiter(rate.Every(time.Second/5), 10)))
49+
server.AddReceivingMiddleware(PerMethodRateLimiterMiddleware[*mcp.ServerSession](map[string]*rate.Limiter{
50+
"callTool": rate.NewLimiter(rate.Every(time.Second), 5), // once a second with a burst up to 5
51+
"listTools": rate.NewLimiter(rate.Every(time.Minute), 20), // once a minute with a burst up to 20
52+
}))
53+
// Run Server logic.
54+
}

0 commit comments

Comments
 (0)