-
Notifications
You must be signed in to change notification settings - Fork 234
Configure rate limits on VirtualMCPServer PR B 2 #5522
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
Open
Sanskarzz
wants to merge
4
commits into
stacklok:main
Choose a base branch
from
Sanskarzz:ratelimitingVMCP3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+868
−277
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package mcp | ||
|
|
||
| // RequestError is implemented by domain errors that should fail the MCP request | ||
| // instead of being converted to a successful tool result with IsError=true. | ||
| // | ||
| // The mcp-go tool-handler seam maps returned errors to JSON-RPC INTERNAL_ERROR, | ||
| // so this is a control-flow marker, not a custom JSON-RPC code hook. | ||
| type RequestError interface { | ||
| error | ||
| MCPRequestError() | ||
| } |
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,34 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package ratelimit | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| thvmcp "github.com/stacklok/toolhive/pkg/mcp" | ||
| ) | ||
|
|
||
| const ( | ||
| // CodeRateLimited is the JSON-RPC error code for rate-limited requests. | ||
| // Per RFC THV-0057: implementation-defined code in the -32000 to -32099 range. | ||
| CodeRateLimited int64 = -32029 | ||
|
|
||
| // MessageRateLimited is the error message for rate-limited requests. | ||
| MessageRateLimited = "Rate limit exceeded" | ||
| ) | ||
|
|
||
| // RateLimitedError reports that a request exceeded its configured rate limit. | ||
| type RateLimitedError struct { | ||
| RetryAfter time.Duration | ||
| } | ||
|
|
||
| var _ thvmcp.RequestError = (*RateLimitedError)(nil) | ||
|
|
||
| func (*RateLimitedError) Error() string { | ||
| return MessageRateLimited | ||
| } | ||
|
|
||
| // MCPRequestError marks rate-limit denials as request-level failures rather | ||
| // than tool execution errors. | ||
| func (*RateLimitedError) MCPRequestError() {} |
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
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
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,61 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Package ratelimit applies rate limiting at the vMCP domain boundary. | ||
| package ratelimit | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "log/slog" | ||
|
|
||
| "github.com/stacklok/toolhive/pkg/auth" | ||
| baseratelimit "github.com/stacklok/toolhive/pkg/ratelimit" | ||
| "github.com/stacklok/toolhive/pkg/vmcp" | ||
| "github.com/stacklok/toolhive/pkg/vmcp/core" | ||
| ) | ||
|
|
||
| // decorator wraps a [core.VMCP] to rate-limit tool calls. Every method except | ||
| // CallTool is promoted from the embedded inner core unchanged. The decorator sits | ||
| // below the session optimizer, so name is already the resolved backend tool name. | ||
| type decorator struct { | ||
| core.VMCP | ||
| limiter baseratelimit.Limiter | ||
| } | ||
|
|
||
| var _ core.VMCP = (*decorator)(nil) | ||
|
|
||
| // NewDecorator wraps inner with vMCP rate limiting. | ||
| // | ||
| // inner must be non-nil; a nil inner is a composition-root wiring bug and panics | ||
| // rather than deferring the failure to the first promoted method call. A nil | ||
| // limiter means rate limiting is disabled and inner is returned unchanged. | ||
| func NewDecorator(inner core.VMCP, limiter baseratelimit.Limiter) core.VMCP { | ||
| if inner == nil { | ||
| panic("ratelimit: NewDecorator requires a non-nil inner VMCP") | ||
| } | ||
| if limiter == nil { | ||
| return inner | ||
| } | ||
| return &decorator{ | ||
| VMCP: inner, | ||
| limiter: limiter, | ||
| } | ||
| } | ||
|
|
||
| // CallTool checks the rate limit for name before delegating to inner. At this | ||
| // seam name is already resolved by any outer optimizer layer, so per-tool bucket | ||
| // keys match the real backend tool instead of the optimizer call_tool meta-tool. | ||
| func (d *decorator) CallTool( | ||
| ctx context.Context, identity *auth.Identity, name string, | ||
| args map[string]any, meta map[string]any, | ||
| ) (*vmcp.ToolCallResult, error) { | ||
| if err := baseratelimit.Allow(ctx, d.limiter, identity, name); err != nil { | ||
| var limited *baseratelimit.RateLimitedError | ||
| if errors.As(err, &limited) { | ||
| return nil, err | ||
| } | ||
| slog.WarnContext(ctx, "rate limit check failed, allowing tool call", "tool", name, "error", err) | ||
| } | ||
| return d.VMCP.CallTool(ctx, identity, name, args, meta) | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick:
Allowreturns three outcomes —nil,*RateLimitedError, or the raw limiter error (e.g. Redis unreachable). This returns the raw error too, so the decorator fails closed: a Redis blip fails every vMCP tool call. The HTTP middleware path consuming the sameAllowfails open (slog.Warn("rate limit check failed, allowing request")→ proceeds). Worth matching that posture here — block only on*RateLimitedError, log-and-allow on infra errors — so the two consumers ofAllowbehave consistently and a limiter outage doesn't take down tool calls. If fail-closed is actually desired for vMCP, a one-line comment saying so would settle it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. The vMCP rate-limit decorator now matches the HTTP middleware posture: it blocks only on
*RateLimitedErrorand logs/allows raw limiter infrastructure errors like Redis failures.That keeps limiter outages from taking down all vMCP tool calls while still enforcing actual rate-limit denials.