list_routes
Inspect all FastAPI routes defined in a file, including those on APIRouter instances.
Instructions
List all FastAPI routes defined in a file, including those on APIRouter instances.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- fastapi_architect/server.py:93-119 (handler)The `list_routes` tool handler function. Decorated with @mcp.tool(), it parses a Python file with the `ast` module, walks the AST for function definitions, inspects decorators for FastAPI route decorators (get, post, put, patch, delete, head, options), and returns a list of routes with method, path, handler name, line number, and async flag.
@mcp.tool() def list_routes(file: str) -> list[dict]: """List all FastAPI routes defined in a file, including those on APIRouter instances.""" tree = _parse(file) routes = [] for node in ast.walk(tree): if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): continue for decorator in node.decorator_list: if not isinstance(decorator, ast.Call): continue func = decorator.func if not isinstance(func, ast.Attribute): continue if func.attr not in ("get", "post", "put", "patch", "delete", "head", "options"): continue path = decorator.args[0].value if decorator.args else "unknown" routes.append({ "method": func.attr.upper(), "path": path, "handler": node.name, "line": node.lineno, "is_async": isinstance(node, ast.AsyncFunctionDef), }) return routes - fastapi_architect/server.py:18-20 (helper)The `_parse` helper function used by `list_routes` to read and parse a Python file into an AST Module.
def _parse(file: str) -> ast.Module: with open(file) as f: return ast.parse(f.read()) - fastapi_architect/server.py:93-94 (registration)The @mcp.tool() decorator registers `list_routes` as a tool in the FastMCP server instance.
@mcp.tool() def list_routes(file: str) -> list[dict]: