Skip to content

Use inspect.getdoc(fn) instead of fn.__doc__ in tool automatic description retrieval in FastMCP #1134

@HectorBst

Description

@HectorBst

Description

Actually, when adding a Tool using FastMCP.add_tool() without providing the description, if the function is a method overriding an inherited one, only the docstring of the concrete method is used, without serching in the hierarchy.

If all the documentation is in the abstract class/method, providing no tool description with the concrete method will result in an empty description. Expected or ideal behavior would be to consider docstring in the parent methods too.

Using inspect.getdoc(fn) instead of fn.__doc__ in Tool.from_function would have 2 benefits :

  • also consider eventual parent methods to retrieve a docstring
  • clean the docstring (basically cleaning docstring indentation used to align with bloc codes)

For example :

class MyUseCase(ABC):
    @abstractmethod
    def do_something(self) -> str:
        """
        Do something usefull.

        Returns:
            The result of the action.
        """
        pass

class MyApp(MyUseCase):
    def do_something(self) -> str:
        ...


app = MyApp()
mcp_server = FastMCP()
mcp_server.add_tool(fn=app.do_something)

Actually, do_something tool will have an empty description.
After this fix, do_something tool description would be :

Do something usefull.

Returns:
    The result of the action.

References

No response

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