Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/ModelContextProtocol.Core/RequestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ internal RequestOptions Clone() =>
if (ProgressToken is not null)
{
meta = (JsonObject?)meta?.DeepClone() ?? [];
meta["progressToken"] = ProgressToken.ToString();

// Serialize through ProgressToken's own converter rather than ToString(), so that an integer
// token is written as a JSON number. Stringifying it changes the token the peer echoes back
// in progress notifications, and the caller then no longer recognizes its own token.
meta["progressToken"] = JsonSerializer.SerializeToNode(
ProgressToken.Value, McpJsonUtilities.JsonContext.Default.ProgressToken);
}

return meta;
Expand Down
15 changes: 15 additions & 0 deletions tests/ModelContextProtocol.Tests/RequestOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ public static void GetMetaForRequest_OnlyProgressTokenSetAsLong_ReturnsNewObject
Assert.NotSame(actual, options.GetMetaForRequest());
}

[Fact]
public static void GetMetaForRequest_LongProgressToken_IsWrittenAsJsonNumber()
{
RequestOptions options = new() { ProgressToken = new ProgressToken(42L) };

var actual = options.GetMetaForRequest();

Assert.NotNull(actual);
Assert.Equal(JsonValueKind.Number, actual["progressToken"]!.GetValueKind());

// The token the peer reads back must be the token the caller asked for.
RequestParams request = new CallToolRequestParams { Name = "tool", Meta = actual };
Assert.Equal(new ProgressToken(42L), request.ProgressToken);
}

[Fact]
public static void GetMetaForRequest_BothSet_ReturnsCloneWithProgressToken()
{
Expand Down