-
Notifications
You must be signed in to change notification settings - Fork 28
design: add design for rate limiting #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
samthanawalla
merged 1 commit into
modelcontextprotocol:main
from
samthanawalla:designRateLimting
Jun 30, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/modelcontextprotocol/go-sdk/examples/rate-limiting | ||
|
||
go 1.25 | ||
|
||
require ( | ||
github.com/modelcontextprotocol/go-sdk v0.0.0-20250625185707-09181c2c2e89 | ||
golang.org/x/time v0.12.0 | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= | ||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= | ||
github.com/modelcontextprotocol/go-sdk v0.0.0-20250625185707-09181c2c2e89 h1:kUGBYP25FTv3ZRBhLT4iQvtx4FDl7hPkWe3isYrMxyo= | ||
github.com/modelcontextprotocol/go-sdk v0.0.0-20250625185707-09181c2c2e89/go.mod h1:DcXfbr7yl7e35oMpzHfKw2nUYRjhIGS2uou/6tdsTB0= | ||
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE= | ||
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= | ||
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo= | ||
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2025 The Go MCP SDK Authors. All rights reserved. | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"github.com/modelcontextprotocol/go-sdk/mcp" | ||
"golang.org/x/time/rate" | ||
) | ||
|
||
// GlobalRateLimiterMiddleware creates a middleware that applies a global rate limit. | ||
// Every request attempting to pass through will try to acquire a token. | ||
// If a token cannot be acquired immediately, the request will be rejected. | ||
func GlobalRateLimiterMiddleware[S mcp.Session](limiter *rate.Limiter) mcp.Middleware[S] { | ||
return func(next mcp.MethodHandler[S]) mcp.MethodHandler[S] { | ||
return func(ctx context.Context, session S, method string, params mcp.Params) (mcp.Result, error) { | ||
if !limiter.Allow() { | ||
return nil, errors.New("JSON RPC overloaded") | ||
} | ||
return next(ctx, session, method, params) | ||
} | ||
} | ||
} | ||
|
||
// PerMethodRateLimiterMiddleware creates a middleware that applies rate limiting | ||
// on a per-method basis. | ||
// Methods not specified in limiters will not be rate limited by this middleware. | ||
func PerMethodRateLimiterMiddleware[S mcp.Session](limiters map[string]*rate.Limiter) mcp.Middleware[S] { | ||
return func(next mcp.MethodHandler[S]) mcp.MethodHandler[S] { | ||
return func(ctx context.Context, session S, method string, params mcp.Params) (mcp.Result, error) { | ||
if limiter, ok := limiters[method]; ok { | ||
if !limiter.Allow() { | ||
return nil, errors.New("JSON RPC overloaded") | ||
} | ||
} | ||
return next(ctx, session, method, params) | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
server := mcp.NewServer("greeter1", "v0.0.1", nil) | ||
server.AddReceivingMiddleware(GlobalRateLimiterMiddleware[*mcp.ServerSession](rate.NewLimiter(rate.Every(time.Second/5), 10))) | ||
server.AddReceivingMiddleware(PerMethodRateLimiterMiddleware[*mcp.ServerSession](map[string]*rate.Limiter{ | ||
"callTool": rate.NewLimiter(rate.Every(time.Second), 5), // once a second with a burst up to 5 | ||
"listTools": rate.NewLimiter(rate.Every(time.Minute), 20), // once a minute with a burst up to 20 | ||
})) | ||
// Run Server logic. | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.