inspect_model
Inspect a Pydantic model to view its fields, types, defaults, and validators. Understand model structure without reading source files manually.
Instructions
Inspect a Pydantic model: fields with types/defaults, and validators.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file | Yes | ||
| model | Yes |
Implementation Reference
- fastapi_architect/server.py:173-173 (registration)The 'inspect_model' function is registered as an MCP tool via the @mcp.tool() decorator.
@mcp.tool() - fastapi_architect/server.py:174-211 (handler)The 'inspect_model' handler function that parses a Python file's AST to find a Pydantic model class, extracting its fields (with types and defaults) and validators (decorated with @validator/@field_validator). Returns a dict with model name, field list, and validator list.
def inspect_model(file: str, model: str) -> dict: """Inspect a Pydantic model: fields with types/defaults, and validators.""" tree = _parse(file) for node in ast.walk(tree): if not (isinstance(node, ast.ClassDef) and node.name == model): continue bases = [b.id for b in node.bases if isinstance(b, ast.Name)] if "BaseModel" not in bases: return {"error": f"'{model}' does not inherit from BaseModel"} fields = [] validators = [] for item in node.body: if isinstance(item, ast.AnnAssign) and isinstance(item.target, ast.Name): fields.append({ "name": item.target.id, "type": ast.unparse(item.annotation), "default": ast.unparse(item.value) if item.value else None, }) if isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef)): for dec in item.decorator_list: if ( isinstance(dec, ast.Call) and isinstance(dec.func, ast.Name) and dec.func.id in ("validator", "field_validator") and dec.args ): validators.append({ "function": item.name, "field": dec.args[0].value if isinstance(dec.args[0], ast.Constant) else ast.unparse(dec.args[0]), }) return {"model": model, "fields": fields, "validators": validators} return {"error": f"Model '{model}' not found in {file}"} - fastapi_architect/server.py:18-20 (helper)The '_parse' helper utility used by 'inspect_model' to read and AST-parse a Python file.
def _parse(file: str) -> ast.Module: with open(file) as f: return ast.parse(f.read())