Properly use instance methods, class methods, and static methods with FlashMCP decorators.
@tool()
, @resource()
, and @prompt()
).
@tool()
, @resource()
, or @prompt()
to a method, the decorator captures the function at decoration time. For instance methods and class methods, this poses a challenge because:
self
or cls
that it cannot provide values for.
self
as a required parameter, but it won’t know what to provide for it, causing errors or unexpected behavior.
Do this instead:
obj
)obj.add
), Python creates a bound method where self
is already set to that instanceself
@classmethod
decorator (Python applies decorators bottom-to-top). So it captures the function before it’s transformed into a class method, leading to incorrect behavior.
Do this instead:
@classmethod
decorator is applied properly during class definitionMyClass.from_string
, Python provides a special method object that automatically binds the class to the cls
parametercls
parameter@staticmethod
decorator is applied first (executed last), transforming the method into a regular functionself
or cls
parameter@tool()
and add_tool()
@resource()
and add_resource_fn()
@prompt()
and add_prompt()