-
Notifications
You must be signed in to change notification settings - Fork 627
Add ISessionMigrationHandler
#1270
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
MackinnonBuck
wants to merge
5
commits into
main
Choose a base branch
from
mbuck/session-migration-handler
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.
+585
−27
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eae6fd1
Add ISessionMigrationHandler, require session ID unless initialize re…
MackinnonBuck 682d099
Clean up comments
MackinnonBuck d4a97e5
Fix malformed comment
MackinnonBuck f668f65
PR feedback: Move `ISessionMigrationHandler` to `ModelContextProtocol…
MackinnonBuck 28ae9cd
PR feedback: Move test to `StreamableHttpServerConformanceTests`
MackinnonBuck 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
62 changes: 62 additions & 0 deletions
62
src/ModelContextProtocol.AspNetCore/ISessionMigrationHandler.cs
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,62 @@ | ||
| using Microsoft.AspNetCore.Http; | ||
| using ModelContextProtocol.Protocol; | ||
|
|
||
| namespace ModelContextProtocol.AspNetCore; | ||
|
|
||
| /// <summary> | ||
| /// Provides hooks for persisting and restoring MCP session initialization data, | ||
| /// enabling session migration across server instances. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// When an MCP server is horizontally scaled, stateful sessions are bound to a single process. | ||
| /// If that process restarts or scales down, the session is lost. By implementing this interface | ||
| /// and registering it with DI, you can persist the initialization handshake data and restore it | ||
| /// when a client reconnects to a different server instance with its existing <c>Mcp-Session-Id</c>. | ||
| /// </para> | ||
| /// <para> | ||
| /// This does <strong>not</strong> solve the session-affinity problem for in-flight server-to-client | ||
| /// requests (such as sampling or elicitation). Responses to those requests must still be routed to | ||
| /// the process that created the request. This interface only enables migration of idle sessions | ||
| /// by persisting the data established during the initialization handshake. | ||
| /// </para> | ||
| /// </remarks> | ||
| public interface ISessionMigrationHandler | ||
| { | ||
| /// <summary> | ||
| /// Called after a session has been successfully initialized via the MCP initialization handshake. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Use this to persist the <paramref name="initializeParams"/> (which includes client capabilities, | ||
| /// client info, and protocol version) to an external store so the session can be migrated to | ||
| /// another server instance later via <see cref="AllowSessionMigrationAsync"/>. | ||
| /// </remarks> | ||
| /// <param name="context">The <see cref="HttpContext"/> for the initialization request.</param> | ||
| /// <param name="sessionId">The unique identifier for the session.</param> | ||
| /// <param name="initializeParams">The initialization parameters sent by the client during the handshake.</param> | ||
| /// <param name="cancellationToken">A cancellation token.</param> | ||
| /// <returns>A <see cref="ValueTask"/> representing the asynchronous operation.</returns> | ||
| ValueTask OnSessionInitializedAsync(HttpContext context, string sessionId, InitializeRequestParams initializeParams, CancellationToken cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Called when a request arrives with an <c>Mcp-Session-Id</c> that the current server doesn't recognize. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Return the original <see cref="InitializeRequestParams"/> to allow the session to be migrated | ||
| /// to this server instance, or <see langword="null"/> to reject the request (returning a 404 to the client). | ||
| /// </para> | ||
| /// <para> | ||
| /// Implementations should validate that the request is authorized, for example by checking | ||
| /// <see cref="HttpContext.User"/>, to ensure the caller is permitted to migrate the session. | ||
| /// </para> | ||
| /// </remarks> | ||
| /// <param name="context">The <see cref="HttpContext"/> for the request with the unrecognized session ID.</param> | ||
| /// <param name="sessionId">The session ID from the request that was not found on this server.</param> | ||
| /// <param name="cancellationToken">A cancellation token.</param> | ||
| /// <returns> | ||
| /// The original <see cref="InitializeRequestParams"/> if migration is allowed, | ||
| /// or <see langword="null"/> to reject the request. | ||
| /// </returns> | ||
| ValueTask<InitializeRequestParams?> AllowSessionMigrationAsync(HttpContext context, string sessionId, CancellationToken cancellationToken); | ||
| } |
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.
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.