-
Notifications
You must be signed in to change notification settings - Fork 0
Harden MCP request limits, key lifecycle, and connection guides #7
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
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
85e8f26
Defer contributed tool permission checks until key authorization passes
ruibaby 6f21b6d
Rate limit MCP requests by canonical client address
ruibaby f38446a
Revalidate MCP access keys after password verification
ruibaby b54349a
Enforce MCP request timeout and in-flight concurrency limits
ruibaby 594eff9
Budget in-flight attachment upload bytes
ruibaby 0c5a110
Keep MCP access tokens out of client connection guides
ruibaby d4eaafd
Parse MCP client addresses without DNS resolution
ruibaby 3646e19
Reserve exact decoded bytes for attachment uploads
ruibaby 1d9030b
Forget limiter keys whose counts return to zero
ruibaby 330952a
Release in-flight permits when handler assembly fails
ruibaby 32edef5
Serialize limiter accounting within each key's map bin
ruibaby 45b681c
Bound MCP authentication by the request deadline
ruibaby fcccb3c
Simplify MCP in-flight limit accounting
ruibaby d0160c9
Fix MCP authentication filter bean creation
ruibaby 8dfd3e6
Clarify access key warning text
ruibaby 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
72 changes: 72 additions & 0 deletions
72
src/main/java/run/halo/mcpserver/KeyedInFlightLimiter.java
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,72 @@ | ||
| package run.halo.mcpserver; | ||
|
|
||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.Semaphore; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| /** Process-local global and per-key in-flight budgets. */ | ||
| public final class KeyedInFlightLimiter { | ||
|
|
||
| private final int perKeyLimit; | ||
| private final int maxTrackedKeys; | ||
| private final Semaphore globalPermits; | ||
| private final ConcurrentHashMap<String, Semaphore> keyPermits = new ConcurrentHashMap<>(); | ||
|
|
||
| public KeyedInFlightLimiter(int globalLimit, int perKeyLimit, int maxTrackedKeys) { | ||
| this.perKeyLimit = perKeyLimit; | ||
| this.maxTrackedKeys = maxTrackedKeys; | ||
| this.globalPermits = new Semaphore(globalLimit); | ||
| } | ||
|
|
||
| public Lease tryAcquire(String keyId, int permits) { | ||
| if (permits < 0 || keyPermits.size() >= maxTrackedKeys && !keyPermits.containsKey(keyId)) { | ||
| return null; | ||
| } | ||
| if (!globalPermits.tryAcquire(permits)) { | ||
| return null; | ||
| } | ||
| var acquired = new AtomicReference<Semaphore>(); | ||
| keyPermits.compute(keyId, (key, existing) -> { | ||
| var semaphore = existing == null ? new Semaphore(perKeyLimit) : existing; | ||
| if (!semaphore.tryAcquire(permits)) { | ||
| return existing; | ||
| } | ||
| acquired.set(semaphore); | ||
| return semaphore; | ||
| }); | ||
| var semaphore = acquired.get(); | ||
| if (semaphore == null) { | ||
| globalPermits.release(permits); | ||
| return null; | ||
| } | ||
| return new Lease(keyId, semaphore, permits); | ||
| } | ||
|
|
||
| public final class Lease implements AutoCloseable { | ||
|
|
||
| private final String keyId; | ||
| private final Semaphore semaphore; | ||
| private final int permits; | ||
| private final AtomicBoolean released = new AtomicBoolean(); | ||
|
|
||
| private Lease(String keyId, Semaphore semaphore, int permits) { | ||
| this.keyId = keyId; | ||
| this.semaphore = semaphore; | ||
| this.permits = permits; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| if (released.compareAndSet(false, true)) { | ||
| globalPermits.release(permits); | ||
| keyPermits.compute(keyId, (key, mapped) -> { | ||
| semaphore.release(permits); | ||
| return mapped == semaphore && semaphore.availablePermits() == perKeyLimit | ||
| ? null | ||
| : mapped; | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } |
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,23 @@ | ||
| package run.halo.mcpserver; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** Process-local bounds on concurrent authenticated MCP request work. */ | ||
| @Component | ||
| class McpInFlightLimiter { | ||
|
|
||
| static final int GLOBAL_LIMIT = 100; | ||
| static final int PER_KEY_LIMIT = 16; | ||
| static final int MAX_TRACKED_KEYS = 10_000; | ||
|
|
||
| private final KeyedInFlightLimiter limiter = | ||
| new KeyedInFlightLimiter(GLOBAL_LIMIT, PER_KEY_LIMIT, MAX_TRACKED_KEYS); | ||
|
|
||
| /** | ||
| * Reserves one global and per-key slot for the key, or returns null when either budget is | ||
| * exhausted. The caller must close the permit exactly once when the request terminates. | ||
| */ | ||
| KeyedInFlightLimiter.Lease tryAcquire(String keyId) { | ||
| return limiter.tryAcquire(keyId, 1); | ||
| } | ||
| } |
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
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.