Add InheritEnvironmentVariables to StdioClientTransportOptions (#1563)

This commit is contained in:
Stephen Halter 2026-05-08 10:38:48 -07:00 • committed by GitHub
commit 3686a4af75
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -31,6 +31,8 @@ else
Name = "Demo Server",
Command = command,
Arguments = arguments,
InheritEnvironmentVariables = false,
EnvironmentVariables = GetMinimalDotNetEnvironment(),
});
}
await using var mcpClient = await McpClient.CreateAsync(clientTransport!);
@ -122,3 +124,21 @@ static string GetCurrentSourceDirectory([CallerFilePath] string? currentFile = n
Debug.Assert(!string.IsNullOrWhiteSpace(currentFile));
return Path.GetDirectoryName(currentFile) ?? throw new InvalidOperationException("Unable to determine source directory.");
}
// Returns the safe default environment variables plus extras needed by 'dotnet run'.
// Omitting variables the server doesn't need prevents unintentional leakage of
// credentials or other sensitive values present in the parent process.
static Dictionary<string, string?> GetMinimalDotNetEnvironment()
{
var env = StdioClientTransportOptions.GetDefaultEnvironmentVariables();
// 'dotnet run' also needs DOTNET_ROOT and NUGET_PACKAGES to find the .NET runtime and package cache.
foreach (var key in (string[])["DOTNET_ROOT", "NUGET_PACKAGES"])
{
var value = Environment.GetEnvironmentVariable(key);
if (value is not null)
{
env[key] = value;
}
}
return env;
}