Skip to content

Commit 06af40e

Browse files
committed
Use _meta as backing field for both Meta and ProgressToken
1 parent 595b4b6 commit 06af40e

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

src/ModelContextProtocol.Core/RequestOptions.cs

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,35 @@ public JsonObject? Meta
2929
{
3030
get
3131
{
32-
if (ProgressToken == null)
32+
_meta ??= new JsonObject();
33+
return _meta;
34+
}
35+
set
36+
{
37+
// Preserve the progress token if set.
38+
var progressToken = _meta?["progressToken"];
39+
if (value is null)
3340
{
34-
return _meta;
41+
if (progressToken is not null)
42+
{
43+
_meta = new JsonObject
44+
{
45+
["progressToken"] = progressToken,
46+
};
47+
}
48+
else
49+
{
50+
_meta = null;
51+
}
3552
}
36-
37-
// Clone existing metadata or create a new one
38-
var meta = _meta?.DeepClone() as JsonObject ?? new JsonObject();
39-
40-
// Add progress token to metadata
41-
meta["progressToken"] = ProgressToken.Value.Token switch
53+
else
4254
{
43-
string s => s,
44-
long l => l,
45-
_ => null
46-
};
47-
48-
return meta;
55+
if (progressToken is not null) {
56+
value["progressToken"] = progressToken;
57+
}
58+
_meta = value;
59+
}
4960
}
50-
set => _meta = value;
5161
}
5262

5363
/// <summary>
@@ -58,5 +68,33 @@ public JsonObject? Meta
5868
/// <summary>
5969
/// The progress token for tracking long-running operations.
6070
/// </summary>
61-
public ProgressToken? ProgressToken { get; set; }
71+
public ProgressToken? ProgressToken {
72+
get
73+
{
74+
return _meta?["progressToken"] switch
75+
{
76+
JsonValue v when v.TryGetValue(out string? s) => new ProgressToken(s),
77+
JsonValue v when v.TryGetValue(out long l) => new ProgressToken(l),
78+
_ => null
79+
};
80+
}
81+
set
82+
{
83+
if (value?.Token is {} token)
84+
{
85+
_meta ??= new JsonObject();
86+
_meta["progressToken"] = token switch
87+
{
88+
null => _meta.Remove("progressToken"),
89+
string s => s,
90+
long l => l,
91+
_ => throw new InvalidOperationException("ProgressToken must be a string or long"),
92+
};
93+
}
94+
else
95+
{
96+
_meta?.Remove("progressToken");
97+
}
98+
}
99+
}
62100
}

0 commit comments

Comments
 (0)