rename_symbol
Rename a symbol across all project files by specifying its exact location (file, line, column) and new name, updating all references consistently.
Instructions
Rename the symbol at the given position across all files in the project.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file | Yes | ||
| line | Yes | ||
| column | Yes | ||
| new_name | Yes |
Implementation Reference
- fastapi_architect/server.py:42-67 (handler)The main handler function for the 'rename_symbol' tool. Uses Jedi to find all references to a symbol, then renames them in-place across all project files.
def rename_symbol(file: str, line: int, column: int, new_name: str) -> dict: """Rename the symbol at the given position across all files in the project.""" script = jedi.Script(path=file, project=_project(file)) refs = [r for r in script.get_references(line=line, column=column) if r.module_path is not None] changed_files: dict[str, list[str]] = {} for r in sorted(refs, key=lambda r: (str(r.module_path), r.line), reverse=True): path = str(r.module_path) if path not in changed_files: with open(path) as f: changed_files[path] = f.readlines() lines = changed_files[path] col = r.column idx = r.line - 1 lines[idx] = lines[idx][:col] + new_name + lines[idx][col + len(r.name):] for path, lines in changed_files.items(): with open(path, "w") as f: f.writelines(lines) return { "renamed_to": new_name, "files_changed": len(changed_files), "references_updated": len(refs), } - fastapi_architect/server.py:41-41 (registration)The tool is registered via the @mcp.tool() decorator from FastMCP, which automatically registers 'rename_symbol' as an MCP tool.
@mcp.tool() - fastapi_architect/server.py:9-15 (helper)Helper function '_project' used by rename_symbol to find the project root directory for Jedi's project context.
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))