Skip to content

Commit ad668ea

Browse files
committed
examples/rate-limiting: add example for session based rate limiting
Fixes #22
1 parent 06ae01b commit ad668ea

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

examples/rate-limiting/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/modelcontextprotocol/go-sdk/examples/rate-limiting
33
go 1.25
44

55
require (
6-
github.com/modelcontextprotocol/go-sdk v0.0.0-20250625185707-09181c2c2e89
6+
github.com/modelcontextprotocol/go-sdk v0.1.0
77
golang.org/x/time v0.12.0
88
)

examples/rate-limiting/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
22
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=
3+
github.com/modelcontextprotocol/go-sdk v0.1.0 h1:ItzbFWYNt4EHcUrScX7P8JPASn1FVYb29G773Xkl+IU=
4+
github.com/modelcontextprotocol/go-sdk v0.1.0/go.mod h1:DcXfbr7yl7e35oMpzHfKw2nUYRjhIGS2uou/6tdsTB0=
55
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
66
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
77
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=

examples/rate-limiting/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package main
77
import (
88
"context"
99
"errors"
10+
"log"
11+
"sync"
1012
"time"
1113

1214
"github.com/modelcontextprotocol/go-sdk/mcp"
@@ -43,12 +45,36 @@ func PerMethodRateLimiterMiddleware[S mcp.Session](limiters map[string]*rate.Lim
4345
}
4446
}
4547

48+
// PerSessionRateLimiterMiddleware creates a middleware that applies rate limiting
49+
// on a per-session basis for receiving requests.
50+
func PerSessionRateLimiterMiddleware[S mcp.Session](limit rate.Limit, burst int) mcp.Middleware[S] {
51+
// A map to store limiters, keyed by the session ID.
52+
// Use sync.Map for concurrent access as sessions can connect/disconnect concurrently.
53+
var sessionLimiters sync.Map // map[string]*rate.Limiter
54+
55+
return func(next mcp.MethodHandler[S]) mcp.MethodHandler[S] {
56+
return func(ctx context.Context, session S, method string, params mcp.Params) (mcp.Result, error) {
57+
// Load or CREATE a new limiter for this session if it doesn't exist
58+
actualLimiter, _ := sessionLimiters.LoadOrStore(session.ID(), rate.NewLimiter(limit, burst))
59+
rateLimiter := actualLimiter.(*rate.Limiter)
60+
61+
if !rateLimiter.Allow() {
62+
return nil, errors.New("JSON RPC overloaded")
63+
}
64+
return next(ctx, session, method, params)
65+
}
66+
}
67+
}
68+
4669
func main() {
4770
server := mcp.NewServer("greeter1", "v0.0.1", nil)
4871
server.AddReceivingMiddleware(GlobalRateLimiterMiddleware[*mcp.ServerSession](rate.NewLimiter(rate.Every(time.Second/5), 10)))
4972
server.AddReceivingMiddleware(PerMethodRateLimiterMiddleware[*mcp.ServerSession](map[string]*rate.Limiter{
5073
"callTool": rate.NewLimiter(rate.Every(time.Second), 5), // once a second with a burst up to 5
5174
"listTools": rate.NewLimiter(rate.Every(time.Minute), 20), // once a minute with a burst up to 20
5275
}))
76+
server.AddReceivingMiddleware(PerSessionRateLimiterMiddleware[*mcp.ServerSession](rate.Every(time.Second/5), 10))
5377
// Run Server logic.
78+
log.Println("MCP Server instance created with Middleware (but not running).")
79+
log.Println("This example demonstrates configuration, not live interaction.")
5480
}

0 commit comments

Comments
 (0)