Skip to content

Commit 676aecb

Browse files
Copilotstephentoub
andcommitted
Revert to CreateMonotonicUuid name and document why CreateVersion7 can't be used
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent ef86c4d commit 676aecb

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

src/Common/Polyfills/System/GuidPolyfills.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
namespace System;
22

33
/// <summary>
4-
/// Provides polyfills for GUID generation methods not available in older .NET versions,
5-
/// with monotonic counter-based ordering for strict intra-millisecond sequencing.
4+
/// Provides polyfills for GUID generation methods not available in older .NET versions.
65
/// </summary>
76
internal static class GuidPolyfills
87
{
@@ -11,17 +10,24 @@ internal static class GuidPolyfills
1110
private static readonly object s_lock = new();
1211

1312
/// <summary>
14-
/// Creates a UUID v7 GUID with the specified timestamp.
13+
/// Creates a monotonically increasing UUID v7 GUID with the specified timestamp.
1514
/// Uses a counter for intra-millisecond ordering to ensure strict monotonicity.
1615
/// </summary>
1716
/// <param name="timestamp">The timestamp to embed in the GUID.</param>
18-
/// <returns>A new UUID v7 GUID.</returns>
17+
/// <returns>A new monotonically increasing UUID v7 GUID.</returns>
1918
/// <remarks>
20-
/// Unlike the built-in <c>Guid.CreateVersion7(DateTimeOffset)</c> in .NET 9+,
21-
/// this implementation uses a counter to ensure strict monotonicity within the same millisecond,
22-
/// which is required for keyset pagination to work correctly.
19+
/// <para>
20+
/// This method cannot be replaced with <c>Guid.CreateVersion7(DateTimeOffset)</c> because
21+
/// the built-in .NET implementation uses random bits for intra-millisecond uniqueness,
22+
/// which does not guarantee strict monotonicity. For keyset pagination to work correctly,
23+
/// GUIDs created within the same millisecond must be strictly ordered by creation time.
24+
/// </para>
25+
/// <para>
26+
/// This implementation uses RFC 9562's optional counter mechanism to ensure that
27+
/// multiple GUIDs generated within the same millisecond are strictly monotonically increasing.
28+
/// </para>
2329
/// </remarks>
24-
public static Guid CreateVersion7(DateTimeOffset timestamp)
30+
public static Guid CreateMonotonicUuid(DateTimeOffset timestamp)
2531
{
2632
// UUID v7 format (RFC 9562):
2733
// - 48 bits: Unix timestamp in milliseconds (big-endian)
@@ -88,4 +94,3 @@ public static Guid CreateVersion7(DateTimeOffset timestamp)
8894
bytes[12], bytes[13], bytes[14], bytes[15]);
8995
}
9096
}
91-

src/ModelContextProtocol.Core/Server/InMemoryMcpTaskStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public void Dispose()
438438
}
439439

440440
private string GenerateTaskId() =>
441-
GuidPolyfills.CreateVersion7(GetUtcNow()).ToString("N");
441+
GuidPolyfills.CreateMonotonicUuid(GetUtcNow()).ToString("N");
442442

443443
private static bool IsTerminalStatus(McpTaskStatus status) =>
444444
status is McpTaskStatus.Completed or McpTaskStatus.Failed or McpTaskStatus.Cancelled;

0 commit comments

Comments
 (0)