Avoid new JsonSerializerOptions instances (#34)

We try hard to avoid creating unnecessary JsonSerializerOptions, as each can have a non-trivial overhead due to how reflection is used by JsonSerializer and the JSO instance serving as a cache.  Rather than creating a new JSO for each transport / endpoint, this can just use a singleton. The only thing it loses is logging when the converter fails, but all of those paths are also throwing an exception, which can be logged higher in the call stack.
This commit is contained in:
Stephen Toub 2025-03-07 13:50:27 -05:00 • committed by GitHub
commit 9f0beec63a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -37,7 +37,7 @@ internal abstract class McpJsonRpcEndpoint
_pendingRequests = new();
_notificationHandlers = new();
_nextRequestId = 1;
_jsonOptions = new JsonSerializerOptions().ConfigureForMcp(loggerFactory);
_jsonOptions = JsonSerializerOptionsExtensions.DefaultOptions;
_logger = loggerFactory.CreateLogger<McpClient>();
}