PrefectHQ/fastmcp · error · TypeError

The function '{fn_name}' has '{params[0]}' as its first para

Error message

The function '{fn_name}' has '{params[0]}' as its first parameter. Use the standalone @resource decorator and register the bound method:

    from fastmcp.resources import resource

    class MyClass:
        @resource('{uri}')
        def {fn_name}(...):
            ...

    obj = MyClass()
    mcp.add_resource(obj.{fn_name})

See https://gofastmcp.com/servers/resources#using-with-methods

What it means

The @resource decorator was applied to an unbound method whose first parameter is `self` (or `cls`). FastMCP does not bind methods automatically for resources at registration time inside a class body, so it refuses and points to the standalone-decorator + bound-method registration pattern.

Source

Thrown at fastmcp_slim/fastmcp/server/providers/local_provider/decorators/resources.py:177

        if isinstance(annotations, dict):
            annotations = Annotations(**annotations)

        if inspect.isroutine(uri):
            raise TypeError(
                "The @resource decorator was used incorrectly. "
                "It requires a URI as the first argument. "
                "Use @resource('uri') instead of @resource"
            )

        def decorator(fn: AnyFunction) -> Any:
            # Check for unbound method
            try:
                params = list(inspect.signature(fn).parameters.keys())
            except (ValueError, TypeError):
                params = []
            if params and params[0] in ("self", "cls"):
                fn_name = getattr(fn, "__name__", "function")
                raise TypeError(
                    f"The function '{fn_name}' has '{params[0]}' as its first parameter. "
                    f"Use the standalone @resource decorator and register the bound method:\n\n"
                    f"    from fastmcp.resources import resource\n\n"
                    f"    class MyClass:\n"
                    f"        @resource('{uri}')\n"
                    f"        def {fn_name}(...):\n"
                    f"            ...\n\n"
                    f"    obj = MyClass()\n"
                    f"    mcp.add_resource(obj.{fn_name})\n\n"
                    f"See https://gofastmcp.com/servers/resources#using-with-methods"
                )

            from fastmcp.resources.function_resource import ResourceMeta

            metadata = ResourceMeta(
                uri=uri,
                name=name,
                version=version,

View on GitHub (pinned to 1f02114297)

Solutions

  1. Decorate the method with the standalone @resource decorator and register the bound method: mcp.add_resource(obj.method).
  2. Move the resource to a module-level function if no instance state is needed.
  3. For classmethods, decorate after @classmethod so the first param is not raw `cls` (staticmethod also works since it has no self).

Example fix

// before
class MyClass:
    @resource('config://app')
    def get_config(self) -> str:
        ...

// after
from fastmcp.resources import resource

class MyClass:
    @resource('config://app')
    def get_config(self) -> str:
        ...

obj = MyClass()
mcp.add_resource(obj.get_config)
Defensive patterns

Strategy: validation

Validate before calling

import inspect

def validate_not_unbound_method(fn):
    params = list(inspect.signature(fn).parameters)
    if params and params[0] in ('self', 'cls'):
        raise TypeError('decorate in class body is not supported; register the bound method')
    return fn

Type guard

def is_unbound_method(fn) -> bool:
    try:
        params = list(inspect.signature(fn).parameters)
    except (ValueError, TypeError):
        return False
    return bool(params) and params[0] in ('self', 'cls')

Try / catch

try:
    register_class_resources(MyService, mcp)
except TypeError as e:
    if "has 'self' as its first parameter" in str(e):
        logging.error('register bound methods: mcp.add_resource(obj.method)')
    raise

Prevention

When it happens

Trigger: Applying @resource('uri') to a method defined inside a class (first param self/cls), e.g. decorating an instance or class method in the class body and expecting the server to register it.

Common situations: Organizing resources in a class-based service; porting module-level @resource functions into a class without changing the registration approach.

Related errors


AI-assisted analysis of PrefectHQ/fastmcp@1f02114297 (2026-08-29). Data as JSON: /api/errors/5afe201d831b3691. Report an issue: GitHub.