-
Notifications
You must be signed in to change notification settings - Fork 794
feat: Add RequestIdGenerator for custom request ID generation #732
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
rhtnr
wants to merge
1
commit into
modelcontextprotocol:main
from
rhtnr:feature/custom-request-id-generator
+271
−14
Open
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
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
82 changes: 82 additions & 0 deletions
mcp-core/src/main/java/io/modelcontextprotocol/spec/RequestIdGenerator.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,82 @@ | ||
| /* | ||
| * Copyright 2024-2025 the original author or authors. | ||
| */ | ||
|
|
||
| package io.modelcontextprotocol.spec; | ||
|
|
||
| import java.util.UUID; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| /** | ||
| * A generator for creating unique request IDs for JSON-RPC messages. | ||
| * | ||
| * <p> | ||
| * Implementations of this interface are responsible for generating unique IDs that are | ||
| * used to correlate requests with their corresponding responses in JSON-RPC | ||
| * communication. | ||
| * | ||
| * <p> | ||
| * The MCP specification requires that: | ||
| * <ul> | ||
| * <li>Request IDs MUST be a string or integer</li> | ||
| * <li>Request IDs MUST NOT be null</li> | ||
| * <li>Request IDs MUST NOT have been previously used within the same session</li> | ||
| * </ul> | ||
| * | ||
| * <p> | ||
| * Example usage with a simple numeric ID generator: | ||
| * | ||
| * <pre>{@code | ||
| * AtomicLong counter = new AtomicLong(0); | ||
| * RequestIdGenerator generator = () -> String.valueOf(counter.incrementAndGet()); | ||
| * }</pre> | ||
| * | ||
| * @author Christian Tzolov | ||
| * @see McpClientSession | ||
| */ | ||
| @FunctionalInterface | ||
| public interface RequestIdGenerator { | ||
|
|
||
| /** | ||
| * Generates a unique request ID. | ||
| * | ||
| * <p> | ||
| * The generated ID must be unique within the session and must not be null. | ||
| * Implementations should ensure thread-safety if the generator may be called from | ||
| * multiple threads. | ||
| * @return a unique request ID as a String | ||
| */ | ||
| String generate(); | ||
|
|
||
| /** | ||
| * Creates a default request ID generator that produces UUID-prefixed incrementing | ||
| * IDs. | ||
| * | ||
| * <p> | ||
| * The generated IDs follow the format: {@code <8-char-uuid>-<counter>}, for example: | ||
| * {@code "a1b2c3d4-0"}, {@code "a1b2c3d4-1"}, etc. | ||
| * @return a new default request ID generator | ||
| */ | ||
| static RequestIdGenerator ofDefault() { | ||
| String sessionPrefix = UUID.randomUUID().toString().substring(0, 8); | ||
| AtomicLong counter = new AtomicLong(0); | ||
| return () -> sessionPrefix + "-" + counter.getAndIncrement(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a request ID generator that produces simple incrementing numeric IDs. | ||
| * | ||
| * <p> | ||
| * This generator is useful for MCP servers that require strictly numeric request IDs | ||
| * (such as the Snowflake MCP server). | ||
| * | ||
| * <p> | ||
| * The generated IDs are: {@code "1"}, {@code "2"}, {@code "3"}, etc. | ||
| * @return a new numeric request ID generator | ||
| */ | ||
| static RequestIdGenerator ofIncremental() { | ||
| AtomicLong counter = new AtomicLong(0); | ||
| return () -> String.valueOf(counter.incrementAndGet()); | ||
| } | ||
|
|
||
| } |
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.