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
17 changes: 17 additions & 0 deletions src/ModelContextProtocol.Core/Protocol/NotificationParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,21 @@ private protected NotificationParams()
/// </remarks>
[JsonPropertyName("_meta")]
public JsonObject? Meta { get; set; }

/// <summary>
/// Merges additional metadata into the existing Meta object.
/// </summary>
/// <param name="additionalMeta">The additional metadata to merge.</param>
public void MergeMeta(JsonObject additionalMeta)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're needing to duplicate this method into each place that has Meta. We're also baking in the overwrite semantics of the additionalMeta always winning, as opposed to an existing value winning. If this is common enough to need a helper for, should we instead expose a static merge method?

@eiriktsarpalis, are we missing something here in STJ? What do you think we should do?

{
if (Meta == null)
{
Meta = new JsonObject();
}

foreach (var kvp in additionalMeta)
{
Meta[kvp.Key] = kvp.Value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put this logic into an internal shared method?

Also, do we want to do a deep merge? For example,

// Left
"_meta": {
  "a": {
    "b": {}
  }
}

// Right
"_meta": {
  "a": {
    "c": {}
  }
}

// Deep Merge
"_meta": {
  "a": {
    "b": {}
    "c": {}
  }
}

With the current shallow merge, the right JsonObject would completely overwrite the left one. This might be the best behavior if we don't think it makes sense for multiple components to configure the same top-level _meta keys, but we should at least clarify this is a shallow merge in the doc comments.

}
}
}
17 changes: 17 additions & 0 deletions src/ModelContextProtocol.Core/Protocol/RequestParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,21 @@ public ProgressToken? ProgressToken
}
}
}

/// <summary>
/// Merges additional metadata into the existing Meta object.
/// </summary>
/// <param name="additionalMeta">The additional metadata to merge.</param>
public void MergeMeta(JsonObject additionalMeta)
{
if (Meta == null)
{
Meta = new JsonObject();
}

foreach (var kvp in additionalMeta)
{
Meta[kvp.Key] = kvp.Value;
}
}
}
17 changes: 17 additions & 0 deletions src/ModelContextProtocol.Core/Protocol/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,21 @@ private protected Result()
/// </remarks>
[JsonPropertyName("_meta")]
public JsonObject? Meta { get; set; }

/// <summary>
/// Merges additional metadata into the existing Meta object.
/// </summary>
/// <param name="additionalMeta">The additional metadata to merge.</param>
public void MergeMeta(JsonObject additionalMeta)
{
if (Meta == null)
{
Meta = new JsonObject();
}

foreach (var kvp in additionalMeta)
{
Meta[kvp.Key] = kvp.Value;
}
}
}