find_model_usages
Locate every occurrence of a Pydantic model used as a type annotation in your FastAPI project. Trace model dependencies across files.
Instructions
Find all places a Pydantic model is used as a type annotation across the project.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file | Yes | ||
| model | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- fastapi_architect/server.py:214-234 (handler)The find_model_usages tool handler: parses all Python files in the project, walks the AST looking for AnnAssign nodes (type annotations) and function argument annotations that match the given model name, returning a list of locations where the model is used as a type annotation.
@mcp.tool() def find_model_usages(file: str, model: str) -> list[dict]: """Find all places a Pydantic model is used as a type annotation across the project.""" project = _project(file) results = [] for py_file in Path(str(project.path)).rglob("*.py"): try: tree = ast.parse(py_file.read_text()) except SyntaxError: continue for node in ast.walk(tree): if isinstance(node, ast.AnnAssign): if ast.unparse(node.annotation) == model: results.append({"file": str(py_file), "line": node.lineno}) if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): for arg in node.args.args: if arg.annotation and ast.unparse(arg.annotation) == model: results.append({"file": str(py_file), "line": arg.col_offset, "function": node.name}) return results - fastapi_architect/server.py:214-214 (registration)Tool registration via @mcp.tool() decorator on the find_model_usages function. The FastMCP instance is created on line 6.
@mcp.tool() - fastapi_architect/server.py:9-15 (helper)The _project helper function used by find_model_usages to locate the project root from a given file path by walking upward looking for marker files (pyproject.toml, requirements.txt, setup.py).
def _project(file: str) -> jedi.Project: """Walk up from file to find the project root.""" path = Path(file).resolve() for parent in [path.parent, *path.parents]: if any((parent / f).exists() for f in ("pyproject.toml", "requirements.txt", "setup.py")): return jedi.Project(path=str(parent)) return jedi.Project(path=str(path.parent))