get_dependencies
Retrieves the complete Depends() dependency injection tree for a specified FastAPI handler to understand all nested dependencies.
Instructions
Get the full Depends() injection tree for a FastAPI handler.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file | Yes | ||
| handler | Yes |
Implementation Reference
- fastapi_architect/server.py:122-156 (handler)The core handler function for the 'get_dependencies' MCP tool. Parses a Python file using AST, finds all functions with 'Depends(...)' default arguments, builds a dependency map, and returns a recursive tree of dependencies for the requested handler. Detects circular dependencies.
@mcp.tool() def get_dependencies(file: str, handler: str) -> dict: """Get the full Depends() injection tree for a FastAPI handler.""" tree = _parse(file) dep_map: dict[str, list[str]] = {} for node in ast.walk(tree): if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): continue deps = [] for default in node.args.defaults: if ( isinstance(default, ast.Call) and isinstance(default.func, ast.Name) and default.func.id == "Depends" and default.args and isinstance(default.args[0], ast.Name) ): deps.append(default.args[0].id) dep_map[node.name] = deps def build_tree(name: str, seen: set[str] | None = None) -> dict: seen = seen or set() if name in seen: return {"name": name, "dependencies": [], "circular": True} seen.add(name) return { "name": name, "dependencies": [build_tree(dep, seen.copy()) for dep in dep_map.get(name, [])], } if handler not in dep_map: return {"error": f"Handler '{handler}' not found in {file}"} return build_tree(handler) - fastapi_architect/server.py:122-122 (registration)The tool is registered as an MCP tool via the @mcp.tool() decorator on line 122, which is the FastMCP instance created on line 6.
@mcp.tool() - fastapi_architect/server.py:18-21 (helper)Helper function '_parse' used by get_dependencies to parse a Python file into an AST module.
def _parse(file: str) -> ast.Module: with open(file) as f: return ast.parse(f.read())