Class: ModelContextProtocol::Server::StdioConfiguration

Inherits:
Configuration
  • Object
show all
Defined in:
lib/model_context_protocol/server/stdio_configuration.rb

Overview

Settings for servers that communicate via stdin/stdout, typically used by standalone scripts launched by clients like Claude Desktop.

Created by Server.with_stdio_transport, which yields an instance to a configuration block before passing it to Router. Adds environment variable management (via set_environment_variable and require_environment_variable) on top of the base class, and enforces that server_logger cannot write to stdout (which would corrupt the protocol).

Router queries apply_environment_variables? (true for this subclass) and calls execute_with_context to temporarily modify ENV before executing handlers. This is safe because stdio servers are single-threaded; see StreamableHttpConfiguration for the multi-threaded alternative.

Instance Attribute Summary

Attributes inherited from Configuration

#client_logger, #instructions, #name, #pagination, #title, #version

Instance Method Summary collapse

Methods inherited from Configuration

#context, #context=, #initialize, #pagination_enabled?, #pagination_options, #registry, #server_logger, #supports_list_changed?, #validate!

Constructor Details

This class inherits a constructor from ModelContextProtocol::Server::Configuration

Instance Method Details

#apply_environment_variables?Boolean

Returns true (Router modifies ENV before executing handlers because stdin/stdout servers run single-threaded and ENV mutation is safe).

Returns:

  • true (Router modifies ENV before executing handlers because stdin/stdout servers run single-threaded and ENV mutation is safe)



20
# File 'lib/model_context_protocol/server/stdio_configuration.rb', line 20

def apply_environment_variables? = true

#environment_variable(key) ⇒ String?

Retrieve a variable's value by key, checking the programmatic store first then ENV. Used by validate_environment_variables! to verify that required variables are set. Keys are normalized to uppercase for consistent lookups.

Parameters:

  • the variable name (case-insensitive; normalized to uppercase)

Returns:

  • the value from environment_variables or ENV, or nil if unset



38
39
40
# File 'lib/model_context_protocol/server/stdio_configuration.rb', line 38

def environment_variable(key)
  environment_variables[key.to_s.upcase] || ENV[key.to_s.upcase]
end

#environment_variablesHash<String, String>

Access the hash of programmatically-set variables. Router merges these with ENV via execute_with_context before executing handlers, making them available to prompts, resources, and tools through ENV lookups. Variables set here take precedence over shell environment variables.

Returns:

  • lazily-initialized hash mapping uppercase keys to values



28
29
30
# File 'lib/model_context_protocol/server/stdio_configuration.rb', line 28

def environment_variables
  @environment_variables ||= {}
end

#require_environment_variable(key) ⇒ Array<String>

Declare a variable that must be present at validation time. Adds the key to an internal tracking list; validate_environment_variables! raises MissingRequiredEnvironmentVariable if environment_variable(key) returns nil. Use this for API keys or database URLs needed by handlers.

Parameters:

  • the variable name (normalized to uppercase)

Returns:

  • the updated list of required variables (for chaining)



63
64
65
# File 'lib/model_context_protocol/server/stdio_configuration.rb', line 63

def require_environment_variable(key)
  required_environment_variables << key.to_s.upcase
end

#set_environment_variable(key, value) ⇒ String

Store a variable that Router will merge into ENV before executing handlers. Useful for secrets retrieved from password managers or encrypted vaults instead of relying on shell ENV. Called "environment variables" to match MCP specification terminology even though they're configuration values.

See https://modelcontextprotocol.io/docs/tools/debugging#environment-variables

Parameters:

  • the variable name (normalized to uppercase)

  • the value to store (overwrites ENV during handler execution)

Returns:

  • the stored value



52
53
54
# File 'lib/model_context_protocol/server/stdio_configuration.rb', line 52

def set_environment_variable(key, value)
  environment_variables[key.to_s.upcase] = value
end

#transport_typeSymbol

Returns :stdio (used by Server.start to instantiate StdioTransport).

Returns:

  • :stdio (used by Server.start to instantiate StdioTransport)



16
# File 'lib/model_context_protocol/server/stdio_configuration.rb', line 16

def transport_type = :stdio