Reduce dependencies to 8.x versions of packages (#195)
* Reduce dependencies to 8.x versions of packages When building for netstandard2.0 or net8.0, the ModelContextProtocol library now only depends on 8.x versions of nuget packages, rather than using the newer 9.x versions. The exception to this are packages that don't have 8.x versions. This also introduces a net9.0 tfm. When building for net9.0, it'll use 9.x versions of packages. The PR temporarily removes the Microsoft.Extensions.AI.nupkg reference (the Microsoft.Extensions.AI.Abstractions reference is still very much there). The latest Microsoft.Extensions.AI package on nuget has a System.Text.Json 9.x reference, but the next build to be published drops that down to 8.x as well. Once that build is published, the dependency will be added back, and temporary polyfills (in particular TemporaryAIFunctionFactory) will be deleted. * Fix StdioClientSessionTransport shutdown StreamClientSessionTransport's ReadMessageAsync method does await CleanupAsync in its finally. CleanupAsync awaits _readTask. _readTask _is_ ReadMessageAsync. So it's waiting for itself to complete, which will then always timeout. We may want to refactor how this works more in the future, but for now, make sure it doesn't wait for itself. * Fix dangling requests when server unexpectedly shuts down If the client makes a request to the server and while waiting for the response the server shuts down, the client hangs. Make sure that when the client's message loops ends due to the server's output it's reading from ends, any pending requests are notified. * Add net9.0 testing * Avoid docker port conflicts from test suites running concurrently
This commit is contained in:
parent
1c145a5d52
commit
c452dc8e26
2 changed files with 247 additions and 91 deletions
|
|
@ -0,0 +1,64 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Reflection;
|
||||
|
||||
// NOTE:
|
||||
// This is a temporary workaround for lack of System.Text.Json's JsonStringEnumConverter<T>
|
||||
// 9.x support for JsonStringEnumMemberNameAttribute. Once all builds use the System.Text.Json 9.x
|
||||
// version, this whole file can be removed.
|
||||
|
||||
namespace System.Text.Json.Serialization;
|
||||
|
||||
internal sealed class CustomizableJsonStringEnumConverter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum> :
|
||||
JsonStringEnumConverter<TEnum> where TEnum : struct, Enum
|
||||
{
|
||||
#if !NET9_0_OR_GREATER
|
||||
public CustomizableJsonStringEnumConverter() :
|
||||
base(namingPolicy: ResolveNamingPolicy())
|
||||
{
|
||||
}
|
||||
|
||||
private static JsonNamingPolicy? ResolveNamingPolicy()
|
||||
{
|
||||
var map = typeof(TEnum).GetFields(BindingFlags.Public | BindingFlags.Static)
|
||||
.Select(f => (f.Name, AttributeName: f.GetCustomAttribute<JsonStringEnumMemberNameAttribute>()?.Name))
|
||||
.Where(pair => pair.AttributeName != null)
|
||||
.ToDictionary(pair => pair.Name, pair => pair.AttributeName);
|
||||
|
||||
return map.Count > 0 ? new EnumMemberNamingPolicy(map!) : null;
|
||||
}
|
||||
|
||||
private sealed class EnumMemberNamingPolicy(Dictionary<string, string> map) : JsonNamingPolicy
|
||||
{
|
||||
public override string ConvertName(string name) =>
|
||||
map.TryGetValue(name, out string? newName) ?
|
||||
newName :
|
||||
name;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !NET9_0_OR_GREATER
|
||||
/// <summary>
|
||||
/// Determines the string value that should be used when serializing an enum member.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
|
||||
internal sealed class JsonStringEnumMemberNameAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates new attribute instance with a specified enum member name.
|
||||
/// </summary>
|
||||
/// <param name="name">The name to apply to the current enum member.</param>
|
||||
public JsonStringEnumMemberNameAttribute(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the enum member.
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
}
|
||||
#endif
|
||||
|
|
@ -78,7 +78,6 @@ public static partial class McpJsonUtilities
|
|||
|
||||
// Keep in sync with CreateDefaultOptions above.
|
||||
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web,
|
||||
UseStringEnumConverter = true,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
NumberHandling = JsonNumberHandling.AllowReadingFromString)]
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue