Skip to content

Hello_World Tool (example)

hello_world

Hello_World

Bases: BaseTool

Minimal example tool that exposes greetings in several languages.

Serves as an example scaffold for real tools.

Source code in src/MCPStack/tools/hello_world/hello_world.py
class Hello_World(BaseTool):
    """Minimal example tool that exposes greetings in several languages.

    !!! note ""

        Serves as an example scaffold for real tools.
    """

    def __init__(self, allowed_languages: list[str] | None = None) -> None:
        super().__init__()
        self.required_env_vars = {
            "MCP_HELLO_PREFIX": "",
        }
        self.allowed_languages = {lang.lower() for lang in (allowed_languages or [])}
        self.prefix = ""

    def _initialize(self) -> None:
        pass

    def actions(self) -> list[Callable]:
        """actions function."""
        actions = [
            self.say_hello_world_in_french,
            self.say_hello_world_in_italian,
            self.say_hello_world_in_german,
            self.say_hello_world_in_chinese,
        ]
        if self.allowed_languages:
            actions = [
                a
                for a in actions
                if a.__name__.rsplit("_", 1)[-1].replace("_", "")
                in {"french", "italian", "german", "chinese"}
                and any(lang in a.__name__ for lang in self.allowed_languages)
            ]
        return actions

    def _with_prefix(self, s: str) -> str:
        import os

        pref = os.getenv("MCP_HELLO_PREFIX", "")
        return f"{pref} {s}" if pref else s

    def say_hello_world_in_french(self) -> str:
        """Return 'Hello world' in french.

        Returns:
          str: The translated greeting.
        """
        return self._with_prefix("Bonjour le monde")

    def say_hello_world_in_italian(self) -> str:
        """Return 'Hello world' in italian.

        Returns:
          str: The translated greeting.
        """
        return self._with_prefix("Ciao mondo")

    def say_hello_world_in_german(self) -> str:
        """Return 'Hello world' in german.

        Returns:
          str: The translated greeting.
        """
        return self._with_prefix("Hallo Welt")

    def say_hello_world_in_chinese(self) -> str:
        """Return 'Hello world' in chinese.

        Returns:
          str: The translated greeting.
        """
        return self._with_prefix("你好,世界")

    def to_dict(self) -> dict[str, object]:
        """to_dict function."""
        return {"allowed_languages": list(self.allowed_languages)}

    @classmethod
    def from_dict(cls, params: dict[str, object]):
        """from_dict function."""
        langs = params.get("allowed_languages") or []
        if isinstance(langs, list):
            langs = [str(x) for x in langs]
        else:
            langs = [str(langs)]
        return cls(allowed_languages=langs)  # type: ignore[arg-type]
actions()

actions function.

Source code in src/MCPStack/tools/hello_world/hello_world.py
def actions(self) -> list[Callable]:
    """actions function."""
    actions = [
        self.say_hello_world_in_french,
        self.say_hello_world_in_italian,
        self.say_hello_world_in_german,
        self.say_hello_world_in_chinese,
    ]
    if self.allowed_languages:
        actions = [
            a
            for a in actions
            if a.__name__.rsplit("_", 1)[-1].replace("_", "")
            in {"french", "italian", "german", "chinese"}
            and any(lang in a.__name__ for lang in self.allowed_languages)
        ]
    return actions
from_dict(params) classmethod

from_dict function.

Source code in src/MCPStack/tools/hello_world/hello_world.py
@classmethod
def from_dict(cls, params: dict[str, object]):
    """from_dict function."""
    langs = params.get("allowed_languages") or []
    if isinstance(langs, list):
        langs = [str(x) for x in langs]
    else:
        langs = [str(langs)]
    return cls(allowed_languages=langs)  # type: ignore[arg-type]
say_hello_world_in_chinese()

Return 'Hello world' in chinese.

Returns:

Name Type Description
str str

The translated greeting.

Source code in src/MCPStack/tools/hello_world/hello_world.py
def say_hello_world_in_chinese(self) -> str:
    """Return 'Hello world' in chinese.

    Returns:
      str: The translated greeting.
    """
    return self._with_prefix("你好,世界")
say_hello_world_in_french()

Return 'Hello world' in french.

Returns:

Name Type Description
str str

The translated greeting.

Source code in src/MCPStack/tools/hello_world/hello_world.py
def say_hello_world_in_french(self) -> str:
    """Return 'Hello world' in french.

    Returns:
      str: The translated greeting.
    """
    return self._with_prefix("Bonjour le monde")
say_hello_world_in_german()

Return 'Hello world' in german.

Returns:

Name Type Description
str str

The translated greeting.

Source code in src/MCPStack/tools/hello_world/hello_world.py
def say_hello_world_in_german(self) -> str:
    """Return 'Hello world' in german.

    Returns:
      str: The translated greeting.
    """
    return self._with_prefix("Hallo Welt")
say_hello_world_in_italian()

Return 'Hello world' in italian.

Returns:

Name Type Description
str str

The translated greeting.

Source code in src/MCPStack/tools/hello_world/hello_world.py
def say_hello_world_in_italian(self) -> str:
    """Return 'Hello world' in italian.

    Returns:
      str: The translated greeting.
    """
    return self._with_prefix("Ciao mondo")
to_dict()

to_dict function.

Source code in src/MCPStack/tools/hello_world/hello_world.py
def to_dict(self) -> dict[str, object]:
    """to_dict function."""
    return {"allowed_languages": list(self.allowed_languages)}

cli

Hello_WorldCLI

Bases: BaseToolCLI

Hello_WorldCLI class.

Source code in src/MCPStack/tools/hello_world/cli.py
@beartype
class Hello_WorldCLI(BaseToolCLI):
    """Hello_WorldCLI class."""

    @classmethod
    def get_app(cls) -> typer.Typer:
        """get_app function."""
        app = typer.Typer(
            help="hello_world tool commands.",
            add_completion=False,
            pretty_exceptions_show_locals=False,
            rich_markup_mode="markdown",
        )
        app.command(help="Quick init (sets a default prefix).")(cls.init)
        app.command(help="Configure hello_world (env + params).")(cls.configure)
        app.command(help="Show current hello_world status.")(cls.status)
        return app

    @classmethod
    def init(
        cls,
        prefix: Annotated[
            str | None,
            typer.Option("--prefix", "-p", help="Greeting prefix emoji/text."),
        ] = "👋",
    ) -> None:
        """init function."""
        console.print(f"[green]✅ Set default prefix to '{prefix}'[/green]")
        console.print("Export and run with:")
        console.print(f"\n    export MCP_HELLO_PREFIX='{prefix}'\n")

    @classmethod
    def configure(
        cls,
        prefix: Annotated[
            str | None,
            typer.Option("--prefix", "-p", help="Greeting prefix emoji/text."),
        ] = None,
        languages: Annotated[
            str | None,
            typer.Option(
                "--languages", "-l", help="Comma list: french,italian,german,chinese"
            ),
        ] = None,
        output: Annotated[
            str | None,
            typer.Option("--output", "-o", help="Where to save config JSON."),
        ] = None,
        verbose: Annotated[
            bool, typer.Option("--verbose", "-v", help="Print config.")
        ] = False,
    ) -> ToolConfig:
        """configure function."""
        env_vars = {}
        tool_params = {}
        if prefix is None:
            prefix = typer.prompt("Prefix (emoji/text)", default="")
        env_vars["MCP_HELLO_PREFIX"] = prefix
        if languages:
            lang_list = [s.strip().lower() for s in languages.split(",") if s.strip()]
        else:
            lang_list = []
        if lang_list:
            tool_params["allowed_languages"] = lang_list
        cfg: ToolConfig = {"env_vars": env_vars, "tool_params": tool_params}
        path = output or "hello_world_config.json"
        with open(path, "w") as f:
            json.dump(cfg, f, indent=2)
        console.print(f"[green]✅ Saved hello_world config to {path}[/green]")
        if verbose:
            console.print(
                Panel.fit(
                    json.dumps(cfg, indent=2),
                    title="[bold green]Configuration[/bold green]",
                )
            )
        return cfg

    @classmethod
    def status(cls, verbose: bool = False) -> None:
        """status function."""
        import os

        prefix = os.getenv("MCP_HELLO_PREFIX", "")
        msg = f"Prefix: '{prefix or '[none]'}'"
        console.print(
            Panel.fit(msg, title="[bold green]hello_world status[/bold green]")
        )
configure(prefix=None, languages=None, output=None, verbose=False) classmethod

configure function.

Source code in src/MCPStack/tools/hello_world/cli.py
@classmethod
def configure(
    cls,
    prefix: Annotated[
        str | None,
        typer.Option("--prefix", "-p", help="Greeting prefix emoji/text."),
    ] = None,
    languages: Annotated[
        str | None,
        typer.Option(
            "--languages", "-l", help="Comma list: french,italian,german,chinese"
        ),
    ] = None,
    output: Annotated[
        str | None,
        typer.Option("--output", "-o", help="Where to save config JSON."),
    ] = None,
    verbose: Annotated[
        bool, typer.Option("--verbose", "-v", help="Print config.")
    ] = False,
) -> ToolConfig:
    """configure function."""
    env_vars = {}
    tool_params = {}
    if prefix is None:
        prefix = typer.prompt("Prefix (emoji/text)", default="")
    env_vars["MCP_HELLO_PREFIX"] = prefix
    if languages:
        lang_list = [s.strip().lower() for s in languages.split(",") if s.strip()]
    else:
        lang_list = []
    if lang_list:
        tool_params["allowed_languages"] = lang_list
    cfg: ToolConfig = {"env_vars": env_vars, "tool_params": tool_params}
    path = output or "hello_world_config.json"
    with open(path, "w") as f:
        json.dump(cfg, f, indent=2)
    console.print(f"[green]✅ Saved hello_world config to {path}[/green]")
    if verbose:
        console.print(
            Panel.fit(
                json.dumps(cfg, indent=2),
                title="[bold green]Configuration[/bold green]",
            )
        )
    return cfg
get_app() classmethod

get_app function.

Source code in src/MCPStack/tools/hello_world/cli.py
@classmethod
def get_app(cls) -> typer.Typer:
    """get_app function."""
    app = typer.Typer(
        help="hello_world tool commands.",
        add_completion=False,
        pretty_exceptions_show_locals=False,
        rich_markup_mode="markdown",
    )
    app.command(help="Quick init (sets a default prefix).")(cls.init)
    app.command(help="Configure hello_world (env + params).")(cls.configure)
    app.command(help="Show current hello_world status.")(cls.status)
    return app
init(prefix='👋') classmethod

init function.

Source code in src/MCPStack/tools/hello_world/cli.py
@classmethod
def init(
    cls,
    prefix: Annotated[
        str | None,
        typer.Option("--prefix", "-p", help="Greeting prefix emoji/text."),
    ] = "👋",
) -> None:
    """init function."""
    console.print(f"[green]✅ Set default prefix to '{prefix}'[/green]")
    console.print("Export and run with:")
    console.print(f"\n    export MCP_HELLO_PREFIX='{prefix}'\n")
status(verbose=False) classmethod

status function.

Source code in src/MCPStack/tools/hello_world/cli.py
@classmethod
def status(cls, verbose: bool = False) -> None:
    """status function."""
    import os

    prefix = os.getenv("MCP_HELLO_PREFIX", "")
    msg = f"Prefix: '{prefix or '[none]'}'"
    console.print(
        Panel.fit(msg, title="[bold green]hello_world status[/bold green]")
    )