-
Notifications
You must be signed in to change notification settings - Fork 180
Add options in Transport.send method
#408
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
+186
−63
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
7aa7961
Add support for `TransportSendOptions` in `send` method across all tr…
devcrocod 257fcff
Add KDocs for `send` method in `Transport` detailing `message` and `o…
devcrocod 52a3dcd
Simplify equality checks in `TransportSendOptions` and `RequestOption…
devcrocod 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
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
|
|
@@ -93,22 +93,58 @@ public val DEFAULT_REQUEST_TIMEOUT: Duration = 60.seconds | |
|
|
||
| /** | ||
| * Options that can be given per request. | ||
| * | ||
| * @property relatedRequestId if present, | ||
| * `relatedRequestId` is used to indicate to the transport which incoming request to associate this outgoing message with. | ||
| * @property resumptionToken the resumption token used to continue long-running requests that were interrupted. | ||
| * This allows clients to reconnect and continue from where they left off, if supported by the transport. | ||
| * @property onResumptionToken a callback that is invoked when the resumption token changes, if supported by the transport. | ||
| * This allows clients to persist the latest token for potential reconnection. | ||
| * @property onProgress callback for progress notifications. | ||
| * If set, requests progress notifications from the remote end (if supported). | ||
| * When progress notifications are received, this callback will be invoked. | ||
| * @property timeout a timeout for this request. | ||
| * If exceeded, a McpException with code `RequestTimeout` will be raised from request(). | ||
| * If not specified, `DEFAULT_REQUEST_TIMEOUT` will be used as the timeout. | ||
| */ | ||
| public data class RequestOptions( | ||
| /** | ||
| * If set, requests progress notifications from the remote end (if supported). | ||
| * When progress notifications are received, this callback will be invoked. | ||
| */ | ||
| val onProgress: ProgressCallback? = null, | ||
| public class RequestOptions( | ||
| relatedRequestId: RequestId? = null, | ||
| resumptionToken: String? = null, | ||
| onResumptionToken: ((String) -> Unit)? = null, | ||
| public val onProgress: ProgressCallback? = null, | ||
| public val timeout: Duration = DEFAULT_REQUEST_TIMEOUT, | ||
| ) : TransportSendOptions(relatedRequestId, resumptionToken, onResumptionToken) { | ||
| public operator fun component4(): ProgressCallback? = onProgress | ||
| public operator fun component5(): Duration = timeout | ||
|
|
||
| public fun copy( | ||
| relatedRequestId: RequestId? = this.relatedRequestId, | ||
| resumptionToken: String? = this.resumptionToken, | ||
| onResumptionToken: ((String) -> Unit)? = this.onResumptionToken, | ||
| onProgress: ProgressCallback? = this.onProgress, | ||
| timeout: Duration = this.timeout, | ||
| ): RequestOptions = RequestOptions(relatedRequestId, resumptionToken, onResumptionToken, onProgress, timeout) | ||
|
|
||
| override fun equals(other: Any?): Boolean { | ||
| if (this === other) return true | ||
| if (other == null || this::class != other::class) return false | ||
| if (!super.equals(other)) return false | ||
|
|
||
| other as RequestOptions | ||
|
|
||
| return onProgress == other.onProgress && timeout == other.timeout | ||
| } | ||
|
|
||
| /** | ||
| * A timeout for this request. If exceeded, an McpError with code `RequestTimeout` | ||
| * will be raised from request(). | ||
| * | ||
| * If not specified, `DEFAULT_REQUEST_TIMEOUT` will be used as the timeout. | ||
| */ | ||
| val timeout: Duration = DEFAULT_REQUEST_TIMEOUT, | ||
| ) | ||
| override fun hashCode(): Int { | ||
| var result = super.hashCode() | ||
| result = 31 * result + (onProgress?.hashCode() ?: 0) | ||
| result = 31 * result + timeout.hashCode() | ||
| return result | ||
| } | ||
|
|
||
| override fun toString(): String = | ||
| "RequestOptions(relatedRequestId=$relatedRequestId, resumptionToken=$resumptionToken, onResumptionToken=$onResumptionToken, onProgress=$onProgress, timeout=$timeout)" | ||
| } | ||
|
|
||
| /** | ||
| * Extra data given to request handlers. | ||
|
|
@@ -456,11 +492,9 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
| ), | ||
| ) | ||
|
|
||
| val serialized = JSONRPCNotification( | ||
| notification.method.value, | ||
| params = McpJson.encodeToJsonElement(notification), | ||
| ) | ||
| transport.send(serialized) | ||
| val jsonRpcNotification = notification.toJSON() | ||
|
|
||
| transport.send(jsonRpcNotification, options) | ||
|
|
||
| result.completeExceptionally(reason) | ||
| } | ||
|
|
@@ -469,7 +503,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
| try { | ||
| withTimeout(timeout) { | ||
| logger.trace { "Sending request message with id: $jsonRpcRequestId" } | ||
| [email protected]?.send(jsonRpcRequest) | ||
| [email protected]?.send(jsonRpcRequest, options) | ||
| } | ||
| return result.await() | ||
| } catch (cause: TimeoutCancellationException) { | ||
|
|
@@ -489,13 +523,14 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
| /** | ||
| * Emits a notification, which is a one-way message that does not expect a response. | ||
| */ | ||
| public suspend fun notification(notification: Notification) { | ||
| public suspend fun notification(notification: Notification, relatedRequestId: RequestId? = null) { | ||
| logger.trace { "Sending notification: ${notification.method}" } | ||
| val transport = this.transport ?: error("Not connected") | ||
| assertNotificationCapability(notification.method) | ||
| val sendOptions = relatedRequestId?.let { TransportSendOptions(relatedRequestId = it) } | ||
| val jsonRpcNotification = notification.toJSON() | ||
|
|
||
| val message = notification.toJSON() | ||
| transport.send(message) | ||
| transport.send(jsonRpcNotification, sendOptions) | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.