Skip to content

Universal Config Generator

UniversalConfigGenerator

Factory for producing an MCP host configuration JSON from a stack, dedicated to universal applications.

Deterministic

Reads from environment and StackConfig; does not mutate the stack.

Source code in src/MCPStack/core/mcp_config_generator/mcp_config_generators/universal_mcp_config.py
@beartype
class UniversalConfigGenerator:
    """Factory for producing an MCP host configuration JSON from a stack, dedicated to universal applications.

    !!! note "Deterministic"

        Reads from environment and `StackConfig`; does not mutate the stack.
    """

    @classmethod
    def generate(
        cls,
        stack,
        command: Optional[str] = None,
        args: Optional[List[str]] = None,
        cwd: Optional[str] = None,
        module_name: Optional[str] = None,
        pipeline_config_path: Optional[str] = None,
        save_path: Optional[str] = None,
    ) -> dict:
        """Create the configuration mapping and optionally persist it to disk.

        !!! tip "Use with CLI"

            The `mcpstack build` command calls into this method.

        Args:
          stack: An `MCPStackCore` instance.
          command (str | None): Executable used to launch the server; defaults to the active Python.
          args (List[str] | None): Arguments for the command; defaults to `['-m', module_name]`.
          cwd (str | None): Working directory for the server process.
          module_name (str | None): Python module to run when using `-m`.
          pipeline_config_path (str | None): Path to the pipeline JSON produced by `stack.save()`.
          save_path (str | None): If set, write the config JSON here.

        Returns:
          dict: Configuration mapping suitable for MCP-compatible hosts.

        Raises:
          MCPStackValidationError: If `command` or `cwd` are invalid (FastMCP variant).
        """
        env = stack.config.env_vars.copy()
        if pipeline_config_path:
            env["MCPSTACK_CONFIG_PATH"] = pipeline_config_path
        config = stack.__dict__.copy()
        config["env_vars"] = env
        if save_path:
            with open(save_path, "w") as f:
                json.dump(config, f, indent=2)
            logger.info(f"✅ Universal config saved to {save_path}.")
        return config

generate(stack, command=None, args=None, cwd=None, module_name=None, pipeline_config_path=None, save_path=None) classmethod

Create the configuration mapping and optionally persist it to disk.

Use with CLI

The mcpstack build command calls into this method.

Parameters:

Name Type Description Default
stack

An MCPStackCore instance.

required
command str | None

Executable used to launch the server; defaults to the active Python.

None
args List[str] | None

Arguments for the command; defaults to ['-m', module_name].

None
cwd str | None

Working directory for the server process.

None
module_name str | None

Python module to run when using -m.

None
pipeline_config_path str | None

Path to the pipeline JSON produced by stack.save().

None
save_path str | None

If set, write the config JSON here.

None

Returns:

Name Type Description
dict dict

Configuration mapping suitable for MCP-compatible hosts.

Raises:

Type Description
MCPStackValidationError

If command or cwd are invalid (FastMCP variant).

Source code in src/MCPStack/core/mcp_config_generator/mcp_config_generators/universal_mcp_config.py
@classmethod
def generate(
    cls,
    stack,
    command: Optional[str] = None,
    args: Optional[List[str]] = None,
    cwd: Optional[str] = None,
    module_name: Optional[str] = None,
    pipeline_config_path: Optional[str] = None,
    save_path: Optional[str] = None,
) -> dict:
    """Create the configuration mapping and optionally persist it to disk.

    !!! tip "Use with CLI"

        The `mcpstack build` command calls into this method.

    Args:
      stack: An `MCPStackCore` instance.
      command (str | None): Executable used to launch the server; defaults to the active Python.
      args (List[str] | None): Arguments for the command; defaults to `['-m', module_name]`.
      cwd (str | None): Working directory for the server process.
      module_name (str | None): Python module to run when using `-m`.
      pipeline_config_path (str | None): Path to the pipeline JSON produced by `stack.save()`.
      save_path (str | None): If set, write the config JSON here.

    Returns:
      dict: Configuration mapping suitable for MCP-compatible hosts.

    Raises:
      MCPStackValidationError: If `command` or `cwd` are invalid (FastMCP variant).
    """
    env = stack.config.env_vars.copy()
    if pipeline_config_path:
        env["MCPSTACK_CONFIG_PATH"] = pipeline_config_path
    config = stack.__dict__.copy()
    config["env_vars"] = env
    if save_path:
        with open(save_path, "w") as f:
            json.dump(config, f, indent=2)
        logger.info(f"✅ Universal config saved to {save_path}.")
    return config