Repository navigation
Fix list_projects date-time schema failures and wildcard filter crash #2730
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization.Metadata; | ||
| using ModelContextProtocol; | ||
|
|
||
| namespace Exceptionless.Web.Mcp; | ||
|
|
||
| /// <summary> | ||
| /// Serializer options for MCP tool output. | ||
| /// </summary> | ||
| public static class McpJsonSerializerOptions | ||
| { | ||
| public static JsonSerializerOptions Default { get; } = Create(); | ||
|
|
||
| private static JsonSerializerOptions Create() | ||
| { | ||
| var options = new JsonSerializerOptions(McpJsonUtilities.DefaultOptions); | ||
| options.TypeInfoResolver = options.TypeInfoResolver!.WithAddedModifier(WriteDatesAsUtc); | ||
| options.MakeReadOnly(); | ||
| return options; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes every <see cref="DateTime"/> property as UTC so it carries a Z, as the output schema's date-time format | ||
| /// (RFC 3339) requires. Older documents were stored without a time zone and read back as | ||
| /// <see cref="DateTimeKind.Unspecified"/>; every stored date is UTC. Changing the getter instead of adding a | ||
| /// converter keeps the generated output schema's date-time format. | ||
| /// </summary> | ||
| public static void WriteDatesAsUtc(JsonTypeInfo typeInfo) | ||
| { | ||
| if (typeInfo.Kind != JsonTypeInfoKind.Object) | ||
| return; | ||
|
|
||
| foreach (var property in typeInfo.Properties) | ||
| { | ||
| if (property.Get is not { } get) | ||
| continue; | ||
|
|
||
| if (property.PropertyType == typeof(DateTime)) | ||
| property.Get = obj => ToUtc((DateTime)get(obj)!); | ||
| else if (property.PropertyType == typeof(DateTime?)) | ||
| property.Get = obj => get(obj) is DateTime value ? ToUtc(value) : null; | ||
| } | ||
| } | ||
|
|
||
| private static DateTime ToUtc(DateTime value) | ||
| { | ||
| return value.Kind switch | ||
| { | ||
| DateTimeKind.Utc => value, | ||
| DateTimeKind.Local => value.ToUniversalTime(), | ||
| _ => DateTime.SpecifyKind(value, DateTimeKind.Utc) | ||
| }; | ||
| } | ||
| } |
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
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,36 @@ | ||
| using System.Text.Json; | ||
| using System.Text.RegularExpressions; | ||
| using Xunit; | ||
|
|
||
| namespace Exceptionless.Tests.Mcp; | ||
|
|
||
| /// <summary> | ||
| /// Asserts MCP output dates satisfy JSON Schema's date-time format (RFC 3339), which requires a Z or an offset. | ||
| /// </summary> | ||
| internal static partial class McpDateAssert | ||
| { | ||
| public static void AllDatesHaveTimeZone(JsonElement element, string path = "$") | ||
| { | ||
| switch (element.ValueKind) | ||
| { | ||
| case JsonValueKind.Object: | ||
| foreach (var property in element.EnumerateObject()) | ||
| AllDatesHaveTimeZone(property.Value, $"{path}.{property.Name}"); | ||
| break; | ||
| case JsonValueKind.Array: | ||
| int index = 0; | ||
| foreach (var item in element.EnumerateArray()) | ||
| AllDatesHaveTimeZone(item, $"{path}[{index++}]"); | ||
| break; | ||
| case JsonValueKind.String when DateTimePattern().IsMatch(element.GetString()!): | ||
| Assert.True(TimeZonePattern().IsMatch(element.GetString()!), $"{path} has no time zone: {element.GetString()}"); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| [GeneratedRegex(@"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}")] | ||
| private static partial Regex DateTimePattern(); | ||
|
|
||
| [GeneratedRegex(@"(Z|[+-]\d{2}:\d{2})$")] | ||
| private static partial Regex TimeZonePattern(); | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.