Class StdioClientTransportOptions
- Namespace
- ModelContextProtocol.Client
- Assembly
- ModelContextProtocol.Core.dll
Provides options for configuring StdioClientTransport instances.
public sealed class StdioClientTransportOptions
- Inheritance
-
StdioClientTransportOptions
- Inherited Members
Properties
Arguments
Gets or sets the arguments to pass to the server process when it is started.
public IList<string>? Arguments { get; set; }
Property Value
Command
Gets or sets the command to execute to start the server process.
public required string Command { get; set; }
Property Value
Exceptions
- ArgumentException
The value is null, empty, or composed entirely of whitespace.
EnvironmentVariables
Gets or sets environment variables to set for the server process.
public IDictionary<string, string?>? EnvironmentVariables { get; set; }
Property Value
Remarks
This property allows you to specify environment variables that will be set in the server process's environment. Setting these variables is useful for passing configuration, authentication information, or runtime flags to the server without modifying its code.
When InheritEnvironmentVariables is true (the default), the server process starts with all environment variables inherited from the current process. The entries in this EnvironmentVariables dictionary are then applied on top: adding new variables, overwriting inherited ones, or removing variables whose value is set to null.
When InheritEnvironmentVariables is false, the server process starts with an empty environment. This dictionary is the sole source of environment variables for the child process.
InheritEnvironmentVariables
Gets or sets a value indicating whether the server process should inherit the current process's environment variables.
public bool InheritEnvironmentVariables { get; set; }
Property Value
- bool
true to inherit the current process's environment variables (the default); false to start the server process with an empty environment and only the variables explicitly provided via EnvironmentVariables.
Remarks
When true (the default), the server process starts with all of the current process's environment variables. Any entries in EnvironmentVariables are then applied on top, adding or overwriting inherited variables.
When false, the server process starts with a completely empty environment. The EnvironmentVariables dictionary is the sole source of environment variables for the child process. This is useful when you want to minimize the attack surface by preventing credentials, tokens, proxy settings, and other sensitive values present in the current environment from unintentionally reaching the child process.
Security consideration: Inheriting environment variables (the default) can unintentionally expose
sensitive values to the child process. Variables such as AWS_SECRET_ACCESS_KEY, GITHUB_TOKEN,
OPENAI_API_KEY, and similar credentials that are present in the parent process will automatically flow into
the server process, which may be undesirable when running third-party or untrusted MCP servers.
Compatibility consideration: Disabling inheritance can cause the child process to fail to start or
behave unexpectedly if it relies on variables provided by the operating system or the user's shell environment.
GetDefaultEnvironmentVariables() covers the most common requirements — PATH, HOME, and
standard system directories — and is a safe starting point for most servers. For servers that also need variables
outside that set (such as DOTNET_ROOT, LD_LIBRARY_PATH, JAVA_HOME, or proxy settings like
HTTP_PROXY, HTTPS_PROXY, and NO_PROXY), add them explicitly via EnvironmentVariables
after calling GetDefaultEnvironmentVariables().
Name
Gets or sets a transport identifier used for logging purposes.
public string? Name { get; set; }
Property Value
ShutdownTimeout
Gets or sets the timeout to wait for the server to shut down gracefully.
public TimeSpan ShutdownTimeout { get; set; }
Property Value
- TimeSpan
The amount of time to wait for the server to shut down gracefully. The default is 5 seconds.
Remarks
This property dictates how long the client should wait for the server process to exit cleanly during shutdown before forcibly terminating it. This balances giving the server enough time to clean up resources and not hanging indefinitely if a server process becomes unresponsive.
StandardErrorLines
Gets or sets a callback that is invoked for each line of stderr received from the server process.
public Action<string>? StandardErrorLines { get; set; }
Property Value
WorkingDirectory
Gets or sets the working directory for the server process.
public string? WorkingDirectory { get; set; }
Property Value
Methods
GetDefaultEnvironmentVariables()
Returns a curated set of environment variables from the current process that are safe to forward to a child MCP server process.
public static Dictionary<string, string?> GetDefaultEnvironmentVariables()
Returns
- Dictionary<string, string>
A new Dictionary<TKey, TValue> populated with the subset of the current process's environment variables that most child processes need to start correctly — for example
PATH,HOME, and standard system directories. Values that appear to be shell function definitions (those starting with()) are excluded for security reasons.
Remarks
The allowlist is aligned with the defaults used by the TypeScript and Python MCP SDKs. On Windows it
includes: APPDATA, HOMEDRIVE, HOMEPATH, LOCALAPPDATA, PATH,
PATHEXT, PROCESSOR_ARCHITECTURE, PROGRAMFILES, SYSTEMDRIVE,
SYSTEMROOT, TEMP, USERNAME, and USERPROFILE. On Unix/macOS it includes:
HOME, LOGNAME, PATH, SHELL, TERM, and USER.
This method is designed to be used together with InheritEnvironmentVariables set to false. Pass the returned dictionary as EnvironmentVariables, optionally adding any server-specific variables the server requires:
var env = StdioClientTransportOptions.GetDefaultEnvironmentVariables();
env["MY_SERVER_API_KEY"] = apiKey;
var transport = new StdioClientTransport(new StdioClientTransportOptions
{
Command = "my-mcp-server",
InheritEnvironmentVariables = false,
EnvironmentVariables = env,
});
If the server requires additional variables not in the default set (such as DOTNET_ROOT,
JAVA_HOME, or proxy settings), add them explicitly after calling this method.