-
Notifications
You must be signed in to change notification settings - Fork 550
DRAFT - HttpRequest.Builder customizer #388
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
Draft
Kehrlann
wants to merge
1
commit into
modelcontextprotocol:main
Choose a base branch
from
Kehrlann:dgarnier/http-request-builder-customzier
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.
+411
−268
Draft
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
44 changes: 44 additions & 0 deletions
44
mcp/src/main/java/io/modelcontextprotocol/client/transport/AsyncHttpRequestCustomizer.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,44 @@ | ||
package io.modelcontextprotocol.client.transport; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpRequest; | ||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Mono; | ||
import reactor.util.annotation.Nullable; | ||
|
||
/** | ||
* Customize {@link HttpRequest.Builder} before sending out SSE or Streamable HTTP | ||
* transport. | ||
* <p> | ||
* When used in a non-blocking context, implementations MUST be non-blocking. | ||
*/ | ||
public interface AsyncHttpRequestCustomizer { | ||
|
||
Publisher<HttpRequest.Builder> customize(HttpRequest.Builder builder, String method, URI endpoint, | ||
@Nullable String body); | ||
|
||
AsyncHttpRequestCustomizer NOOP = new Noop(); | ||
|
||
/** | ||
* Wrap a sync implementation in an async wrapper. | ||
* <p> | ||
* Do NOT use in a non-blocking context. | ||
*/ | ||
static AsyncHttpRequestCustomizer fromSync(SyncHttpRequestCustomizer customizer) { | ||
return (builder, method, uri, body) -> Mono.defer(() -> { | ||
customizer.customize(builder, method, uri, body); | ||
return Mono.just(builder); | ||
}); | ||
} | ||
|
||
class Noop implements AsyncHttpRequestCustomizer { | ||
|
||
@Override | ||
public Publisher<HttpRequest.Builder> customize(HttpRequest.Builder builder, String method, URI endpoint, | ||
String body) { | ||
return Mono.just(builder); | ||
} | ||
|
||
} | ||
|
||
} |
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.
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.
This feels awkward. Perhaps we should leave only the async method and let the user convert the sync customizer outside
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.
I see your point.
I’m trying to ensure sync-based users don’t have to think about async versions of this API at all. My goal is for the user to make a “sync customizer” and only have to worry about that. Making the transformation outside is one more layer they’d have to think about (and that we would have to explain in the docs).
Also, we’ve had feedback from people absolutely NOT wanting to deal with async.
let me know what you think; happy to make the change to “only async” if you think it’s more confusing than helpful.