-
Notifications
You must be signed in to change notification settings - Fork 645
Mcp.core.distributed #1367
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
xiangyan99
wants to merge
5
commits into
modelcontextprotocol:main
Choose a base branch
from
xiangyan99:mcp.core.distributed
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.
Open
Mcp.core.distributed #1367
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0c9de38
Added ModelContextProtocol.AspNetCore.Distributed library
xiangyan99 8a7a423
update readme
xiangyan99 a059579
Merge branch 'main' into mcp.core.distributed
xiangyan99 f7ca1c9
address feedback
xiangyan99 23f7bf0
Merge branch 'mcp.core.distributed' of https://github.com/xiangyan99/…
xiangyan99 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
30 changes: 30 additions & 0 deletions
30
src/ModelContextProtocol.AspNetCore.Distributed/Abstractions/IListeningEndpointResolver.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,30 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.AspNetCore.Hosting.Server; | ||
|
|
||
| namespace ModelContextProtocol.AspNetCore.Distributed.Abstractions; | ||
|
|
||
| /// <summary> | ||
| /// Resolves the listening endpoint address for the local server instance | ||
| /// that should be advertised to other instances for session affinity routing. | ||
| /// </summary> | ||
| public interface IListeningEndpointResolver | ||
| { | ||
| /// <summary> | ||
| /// Resolves the local server address that should be advertised to other instances | ||
| /// for session affinity routing. | ||
| /// </summary> | ||
| /// <param name="server">The server instance to resolve addresses from.</param> | ||
| /// <param name="options">Configuration options containing explicit address overrides.</param> | ||
| /// <returns>A normalized address string in the format "scheme://host:port".</returns> | ||
| /// <remarks> | ||
| /// The resolution strategy is: | ||
| /// <list type="number"> | ||
| /// <item><description>If <see cref="SessionAffinityOptions.LocalServerAddress"/> is set, validate and return it</description></item> | ||
| /// <item><description>Otherwise, resolve from server bindings, preferring non-localhost HTTP addresses</description></item> | ||
| /// <item><description>Fall back to http://localhost:80 if no addresses are available</description></item> | ||
| /// </list> | ||
| /// </remarks> | ||
| string ResolveListeningEndpoint(IServer server, SessionAffinityOptions options); | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/ModelContextProtocol.AspNetCore.Distributed/Abstractions/ISessionAffinityBuilder.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,17 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace ModelContextProtocol.AspNetCore.Distributed.Abstractions; | ||
|
|
||
| /// <summary> | ||
| /// A builder for configuring MCP session affinity. | ||
| /// </summary> | ||
| public interface ISessionAffinityBuilder | ||
| { | ||
| /// <summary> | ||
| /// Gets the host application builder. | ||
| /// </summary> | ||
| IServiceCollection Services { get; } | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/ModelContextProtocol.AspNetCore.Distributed/Abstractions/ISessionStore.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,31 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace ModelContextProtocol.AspNetCore.Distributed.Abstractions; | ||
|
|
||
| /// <summary> | ||
| /// Provides persistence for MCP session ownership. | ||
| /// </summary> | ||
| public interface ISessionStore | ||
| { | ||
| /// <summary> | ||
| /// Gets the current owner of a session, or claims ownership if unclaimed. | ||
| /// </summary> | ||
| /// <param name="sessionId">The session identifier.</param> | ||
| /// <param name="ownerInfoFactory">A factory function that creates the owner information if the session is unclaimed.</param> | ||
| /// <param name="cancellationToken">Cancellation token.</param> | ||
| /// <returns>The current or newly claimed owner information for the session.</returns> | ||
| Task<SessionOwnerInfo> GetOrClaimOwnershipAsync( | ||
| string sessionId, | ||
| Func<CancellationToken, Task<SessionOwnerInfo>> ownerInfoFactory, | ||
| CancellationToken cancellationToken = default | ||
| ); | ||
|
|
||
| /// <summary> | ||
| /// Removes a session from the store. | ||
| /// </summary> | ||
| /// <param name="sessionId">The session identifier to remove.</param> | ||
| /// <param name="cancellationToken">Cancellation token.</param> | ||
| /// <returns>A task representing the asynchronous operation.</returns> | ||
| Task RemoveAsync(string sessionId, CancellationToken cancellationToken = default); | ||
| } |
103 changes: 103 additions & 0 deletions
103
src/ModelContextProtocol.AspNetCore.Distributed/Abstractions/SessionAffinityOptions.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,103 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.ComponentModel.DataAnnotations; | ||
| using Yarp.ReverseProxy.Configuration; | ||
| using Yarp.ReverseProxy.Forwarder; | ||
|
|
||
| namespace ModelContextProtocol.AspNetCore.Distributed.Abstractions; | ||
|
|
||
| /// <summary> | ||
| /// Configuration options for MCP session affinity routing behavior. | ||
| /// </summary> | ||
| public sealed class SessionAffinityOptions | ||
| { | ||
| /// <summary> | ||
| /// Configuration for the YARP forwarder when routing requests to other silos. | ||
| /// If not set, a default configuration will be used. | ||
| /// </summary> | ||
| public ForwarderRequestConfig? ForwarderRequestConfig { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Configuration for the HTTP client used when forwarding requests to other silos. | ||
| /// If not set, an empty configuration will be used. | ||
| /// </summary> | ||
| public HttpClientConfig? HttpClientConfig { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The service key to use when resolving the <see cref="Microsoft.Extensions.Caching.Hybrid.HybridCache"/> service. | ||
| /// When set, the session store will use a keyed HybridCache service that can be configured | ||
| /// to use a specific distributed cache backend (e.g., Redis, SQL Server). | ||
| /// This enables scenarios where multiple cache instances are needed in a single application. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This property is used in conjunction with keyed HybridCache registration. | ||
| /// Register a keyed HybridCache instance using the standard DI keyed services APIs. | ||
| /// </remarks> | ||
| public string? HybridCacheServiceKey { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Explicitly sets the local server address that will be advertised to other instances | ||
| /// for session affinity routing. This address is stored in the distributed session store | ||
| /// and used by other servers to forward requests back to this instance. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// When set, this value takes precedence over automatic address resolution from server bindings. | ||
| /// This is useful in scenarios where: | ||
| /// <list type="bullet"> | ||
| /// <item><description>Running in containerized environments where internal addresses differ from advertised addresses</description></item> | ||
| /// <item><description>Using service meshes where specific addresses/ports must be used for routing</description></item> | ||
| /// <item><description>Multiple network interfaces are available and a specific one should be used</description></item> | ||
| /// <item><description>Running behind load balancers or proxies with address translation</description></item> | ||
| /// </list> | ||
| /// </para> | ||
| /// <para> | ||
| /// The value must be a valid absolute URI including scheme (http or https), host, and port. | ||
| /// Examples: | ||
| /// <list type="bullet"> | ||
| /// <item><description><c>http://pod-1.mcp-service.default.svc.cluster.local:8080</c></description></item> | ||
| /// <item><description><c>http://10.0.1.5:5000</c></description></item> | ||
| /// <item><description><c>https://server1.internal:443</c></description></item> | ||
| /// </list> | ||
| /// </para> | ||
| /// <para> | ||
| /// If not set, the address will be automatically resolved from the server's configured | ||
| /// bindings, preferring HTTP over HTTPS for service mesh scenarios. | ||
| /// </para> | ||
| /// </remarks> | ||
| [HttpOrHttpsUri] | ||
| public string? LocalServerAddress { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validates that a string is a valid HTTP or HTTPS URI. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)] | ||
| internal sealed class HttpOrHttpsUriAttribute : ValidationAttribute | ||
| { | ||
| protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) | ||
| { | ||
| if (value is null or string { Length: 0 }) | ||
| { | ||
| return ValidationResult.Success; | ||
| } | ||
|
|
||
| if (value is not string stringValue) | ||
| { | ||
| return new ValidationResult("Value must be a string."); | ||
| } | ||
|
|
||
| if (!Uri.TryCreate(stringValue, UriKind.Absolute, out Uri? uri)) | ||
| { | ||
| return new ValidationResult($"'{stringValue}' is not a valid absolute URI."); | ||
| } | ||
|
|
||
| if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps) | ||
| { | ||
| return new ValidationResult($"URI must use HTTP or HTTPS scheme. Found: {uri.Scheme}"); | ||
| } | ||
|
|
||
| return ValidationResult.Success; | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
...delContextProtocol.AspNetCore.Distributed/Abstractions/SessionAffinityOptionsValidator.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,16 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace ModelContextProtocol.AspNetCore.Distributed.Abstractions; | ||
|
|
||
| /// <summary> | ||
| /// Validator for <see cref="SessionAffinityOptions"/> that ensures configuration is valid. | ||
| /// Uses compile-time code generation for AOT compatibility. | ||
| /// The source generator will automatically validate data annotations on the options class. | ||
| /// </summary> | ||
| [OptionsValidator] | ||
| internal sealed partial class SessionAffinityOptionsValidator | ||
| : IValidateOptions<SessionAffinityOptions> | ||
| { } |
19 changes: 19 additions & 0 deletions
19
src/ModelContextProtocol.AspNetCore.Distributed/Abstractions/SessionOwnerInfo.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,19 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace ModelContextProtocol.AspNetCore.Distributed.Abstractions; | ||
|
|
||
| /// <summary> | ||
| /// Identifies which server currently owns a session. | ||
| /// </summary> | ||
| public sealed record SessionOwnerInfo | ||
| { | ||
| /// <summary>Unique identifier for the owner (server id, instance id, etc.).</summary> | ||
| public required string OwnerId { get; init; } | ||
|
|
||
| /// <summary>Address (host[:port]) requests should be forwarded to.</summary> | ||
| public required string Address { get; init; } | ||
|
|
||
| /// <summary>Timestamp showing when the owner claimed this session.</summary> | ||
| public DateTimeOffset? ClaimedAt { get; init; } | ||
| } |
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.
They already have moq in this repo. I wouldn't be surprised if they wanted us to keep using that instead of NSubstitute.