{"record":{"id":"d5ae668e1f9a1397","repo":"OpenBB-finance/OpenBB","slug":"error-the-name-instance-in-app-path-is-not","errorCode":null,"errorMessage":"Error: The {name} instance in '{app_path}' is not an instance of FastAPI","messagePattern":"Error: The (.+?) instance in '(.+?)' is not an instance of FastAPI","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"critical","filePath":"openbb_platform/extensions/mcp_server/openbb_mcp_server/utils/app_import.py","lineNumber":98,"sourceCode":"    # Here we use the same approach as uvicorn to handle factory functions.\n    # This prevents us from relying on explicit type annotations.\n    # See: https://github.com/encode/uvicorn/blob/master/uvicorn/config.py\n    try:\n        app = app_or_factory()\n        if not factory:\n            print(  # noqa: T201\n                \"\\n\\n[WARNING]   \"\n                \"App factory detected. Using it, but please consider setting the --factory flag explicitly.\\n\"\n            )\n    except TypeError:\n        if factory:\n            raise TypeError(  # pylint: disable=raise-missing-from\n                f\"Error: The {name} instance in '{app_path}' appears not to be a callable factory function\"\n            )\n        app = app_or_factory\n\n    if not isinstance(app, FastAPI):\n        raise TypeError(\n            f\"Error: The {name} instance in '{app_path}' is not an instance of FastAPI\"\n        )\n\n    return app\n\n\ncl_doc = \"\"\"OpenBB MCP Server\n\nUsage:\n    >>> python -m openbb_mcp_server [OPTIONS]\n\n    >>> openbb-mcp --app ./some_app.py --host 0.0.0.0 --port 8005\n\nDescription:\n    The OpenBB MCP Server is a component of the OpenBB Platform that provides\n    a server for the Model-Context-Protocol. REST endpoints are converted into\n    tools and made available to connected clients.\n","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/OpenBB-finance/OpenBB/blob/3e071fcc2cd9f891cac6040ae60296dba76dab46/openbb_platform/extensions/mcp_server/openbb_mcp_server/utils/app_import.py#L80-L116","documentation":"Final TypeError from the app importer: the resolved attribute (after optional factory call) is not an instance of FastAPI. The MCP server only mounts FastAPI applications, so Flask apps, Starlette apps, routers, or arbitrary objects are rejected at this guard.","triggerScenarios":"--app pointing at a module whose 'app' is a flask.Flask, an APIRouter, a Starlette() instance, or a plain dict/class; or a factory that returns something other than FastAPI. The isinstance(app, FastAPI) check runs after both the direct and factory paths.","commonSituations":"Trying to wrap an existing Flask service, passing a bare router file expecting it to be mounted, a factory that returns a Starlette app or a tuple (app, lifespan), version mismatch where the imported FastAPI class comes from a different installed copy than the check's.","solutions":["Expose an actual FastAPI instance: app = FastAPI() in the target module (or a factory returning one)","For non-FastAPI services, write a thin FastAPI wrapper/proxy instead of passing the original object","If the factory returns a tuple like (app, lifespan), change it to return just the FastAPI instance"],"exampleFix":"# before\n# main.py\nfrom flask import Flask\napp = Flask(__name__)\n\n# after\nfrom fastapi import FastAPI\napp = FastAPI()","handlingStrategy":"type-guard","validationCode":"from fastapi import FastAPI\n\napp_obj = getattr(module, name)\napp_obj = app_obj() if factory else app_obj\nif not isinstance(app_obj, FastAPI):\n    raise TypeError(\n        f\"expected FastAPI, got {type(app_obj).__name__}; \"\n        \"wrap non-FastAPI services in a FastAPI app\"\n    )","typeGuard":"from fastapi import FastAPI\n\ndef is_fastapi_app(module, name: str, factory: bool = False) -> bool:\n    obj = getattr(module, name, None)\n    if factory and callable(obj):\n        try:\n            obj = obj()\n        except TypeError:\n            return False\n    return isinstance(obj, FastAPI)","tryCatchPattern":"try:\n    app = import_app(app_path, name, factory)\nexcept TypeError as e:\n    if \"not an instance of FastAPI\" in str(e):\n        raise SystemExit(\n            f\"{app_path}:{name} is {type(getattr(module, name)).__name__}; \"\n            \"the MCP server can only mount FastAPI apps\"\n        ) from e\n    raise","preventionTips":["Expose 'app = FastAPI()' as the module-level object the server imports","Write a thin FastAPI proxy for Flask/Starlette legacy services","If using a factory, make sure it returns the FastAPI instance itself, not a tuple"],"tags":["python","cli","fastapi","type-check","factory"],"backgroundTag":null,"analyzedSha":"3e071fcc2cd9f891cac6040ae60296dba76dab46","analyzedAt":"2026-08-14T23:40:48.960Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}