Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve FastMCP constructor #305

@MischaPanch

Description

@MischaPanch

The current constructor of FastMCP creates the Settings object from kwargs during init. This erases type information on what can be passed and any help from the IDE. For example, the lifetime callable can be passed (contrary to what is currently claimed in the readme, where it is advised to use low-level interfaces for that), but I had to click through the code to discover that fact.

class FastMCP:
    def __init__(
        self, name: str | None = None, instructions: str | None = None, **settings: Any
    ):
        self.settings = Settings(**settings)

Apart from generally violating the principle of dependency injection, this actually decreases usability and type-safety, and does that for no reason whatsoever IMO

Instead, the interface should have been

class FastMCP:
    def __init__(
        self, name: str | None = None, instructions: str | None = None, settings: Settings | None = None
    ):
        self.settings = settings or Settings()

Since now it is too late, and I suppose you want to keep it backwards compatible, one could change to something like

class FastMCP:
    def __init__(
        self, name: str | None = None, instructions: str | None = None, settings: Settings | None = None, **settings_kwargs: Any
    ):
        if settings and settings_kwargs:
             raise ...
        if settings_kwargs:
             log.warning("Deprecated ...")
        self.settings = settings or Settings(**settings_kwargs)

If you are interested, I could make a quick PR for that, maybe also improve the documentation in the readme while I'm at it

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions