codebase-cartographer

codebase-cartographer

Maps a local Python repository into an AST-backed symbol graph and performs verified multi-file refactors, enabling scope-aware renaming, moving, and deletion of symbols with safety checks.

Category
访问服务器

README

Codebase Cartographer

An MCP server that maps a local Python repository into an AST-backed symbol graph and performs verified multi-file refactors on it.

Most coding agents refactor by grepping for a string and rewriting what they find. That approach cannot tell a call to compute() apart from a local variable that happens to be named compute, and it has no idea which twelve files break when you rename it. This server gives the model a real index instead: scope-resolved references, an import graph, and a two-phase edit protocol that refuses to write anything it cannot verify.

What it does

Maps. Walks the repository (honouring .gitignore), parses every module, and builds a symbol table of functions, classes, methods, and module-level variables, plus the import graph between modules.

Resolves. Finds references using real scope analysis — the LEGB chain, global and nonlocal declarations, comprehension scopes, walrus bindings, and the rule that class bodies are invisible to nested functions. A local variable that shadows a module-level symbol is not a reference to it, and the tool knows the difference.

Refactors. Renames, moves, and deletes symbols across every file that touches them — rewriting from x import y, import x + x.y attribute access, as aliases, and __all__ entries. Nothing is written until you apply a plan you have seen the diff for.

Safety model

Refactoring is two-phase, and the first phase never touches disk.

  1. plan_rename_symbol / plan_move_symbol / plan_delete_symbol return a plan id, a unified diff, and a list of warnings. Every touched file's content hash is recorded.
  2. apply_plan re-checks those hashes (aborting if anything changed on disk since planning), builds the new content for every file, re-parses each one, and refuses to write if any file would end up unparseable. Originals are copied to .cartographer-backups/<plan_id>/. A failure mid-write rolls every file back.

Further guardrails: paths outside the mapped root are rejected; renaming a method requires an explicit allow_heuristic=true because attribute matching cannot be resolved without type inference; deleting a symbol that still has references is refused unless forced.

What it cannot see

Static analysis has a hard edge, and the tool is built to say where that edge is rather than to pretend it isn't there.

find_dynamic_references reports string literals matching a symbol name — getattr(mod, "compute"), plugin registries, dotted settings strings, entry-point tables — and detects imports of underscore-prefixed native modules. Those warnings are attached to every refactor plan.

This matters concretely. Renaming JSONDecodeError in a copy of the stdlib json package rewrites all 19 Python references correctly and still breaks the package, because the _json C accelerator resolves that name at runtime through the C API. No pure-Python analysis can follow that. The tool flags the native accelerator import and tells you to check by hand.

Also invisible: from x import * re-exports (reported as a warning), runtime monkey-patching, and references from other languages or config files.

Tools

Tool Purpose
map_repository Scan a directory and build the index. Call this first, and again after applying.
repository_overview Stats, packages, import cycles, most-depended-on modules.
list_modules Indexed modules, filterable by dotted prefix.
file_outline One file's imports and definitions.
search_symbols Find definitions by substring, kind, or module prefix.
get_symbol_source Full source of one definition, with docstring and decorators.
find_references Every scope-resolved use of a symbol.
find_dynamic_references String literals and native imports that static analysis can't follow.
dependency_graph Internal import edges, whole-graph or one module's neighbourhood.
impact_of_change Transitive dependents — the blast radius of editing a module.
find_dead_code Module-level definitions with no discoverable references.
plan_rename_symbol Plan a repository-wide rename. Writes nothing.
plan_move_symbol Plan moving a definition to another module. Writes nothing.
plan_delete_symbol Plan removing a definition and its __all__ entry. Writes nothing.
preview_plan Re-render a pending plan's diff.
list_plans / discard_plan Inspect or drop pending plans.
apply_plan Commit a plan, with hash checks, syntax validation, and backups.

Install

pip install -e .

Register it with an MCP client:

{
  "mcpServers": {
    "codebase-cartographer": {
      "command": "python",
      "args": ["-m", "cartographer"],
      "env": { "CARTOGRAPHER_ROOT": "/path/to/your/repo" }
    }
  }
}

CARTOGRAPHER_ROOT is only the default for map_repository; the tool takes an explicit root argument too.

A typical session

map_repository(root="~/work/service")
  -> 412 files, 1 import cycle, 38k LOC

impact_of_change(module="service.auth.tokens")
  -> 23 transitive dependents

find_references(qualname="service.auth.tokens.decode_jwt")
  -> 31 references across 12 files

find_dynamic_references(name="decode_jwt")
  -> 1 string literal in service/registry.py:44   <- read this before proceeding

plan_rename_symbol(qualname="service.auth.tokens.decode_jwt", new_name="decode_access_token")
  -> plan 9f2a1c: 31 edits across 12 files, with diff

apply_plan(plan_id="9f2a1c")
  -> written, backed up to .cartographer-backups/9f2a1c/

Development

pip install -e ".[dev]"
pytest

65 tests cover scope resolution (shadowing, global, comprehensions, class-body visibility), reference finding across every import style, rename/move/delete correctness, generated-import style, and the apply-phase guarantees: stale-file detection, syntax validation, and rollback.

The suite's strongest check is that the tool refactors its own source — renaming a function across the implementation and the tests, and moving one between modules — after which the full suite still passes against the rewritten copy.

Requirements

Python 3.10+ (uses ast.alias position attributes). Python source only.

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选