Skip to main content
Glama
sdg5-hub

codebase-cartographer

by sdg5-hub

Codebase Cartographer

一个 MCP 服务器,可将本地 Python 仓库映射为基于 AST 的符号图,并对其执行经过验证的多文件重构

大多数编码代理通过搜索字符串并重写找到的内容来进行重构。这种方法无法区分对 compute() 的调用与恰好名为 compute 的局部变量,也不知道重命名它时会破坏哪十二个文件。该服务器为模型提供了一个真正的索引:作用域解析的引用、导入图,以及一个两阶段编辑协议,该协议拒绝写入任何无法验证的内容。

功能

映射。 遍历仓库(遵循 .gitignore),解析每个模块,并构建函数、类、方法和模块级变量的符号表,以及模块之间的导入图。

解析。 使用真实的作用域分析查找引用——LEGB 链、globalnonlocal 声明、推导式作用域、海象运算符绑定,以及类体对嵌套函数不可见的规则。遮蔽模块级符号的局部变量不是对该符号的引用,该工具能区分这一点。

重构。 在所有涉及符号的文件中重命名、移动和删除符号——重写 from x import yimport x + x.y 属性访问、as 别名和 __all__ 条目。在您查看差异并应用计划之前,不会写入任何内容。

Related MCP server: MCP Python Code Navigation Server

安全模型

重构是两阶段的,第一阶段从不触碰磁盘。

  1. plan_rename_symbol / plan_move_symbol / plan_delete_symbol 返回计划 ID、统一差异和警告列表。每个被触碰文件的哈希值都会被记录。

  2. apply_plan 重新检查这些哈希值(如果自计划以来磁盘上有任何变化则中止),为每个文件构建新内容,重新解析每个文件,如果任何文件最终无法解析则拒绝写入。原始文件会被复制到 .cartographer-backups/<plan_id>/。写入中途失败会回滚所有文件。

其他防护措施:映射根目录之外的路径会被拒绝;重命名方法需要显式设置 allow_heuristic=true,因为属性匹配在没有类型推断的情况下无法解析;删除仍有引用的符号会被拒绝,除非强制。

它无法看到的内容

静态分析有其硬性边界,该工具的设计目标是指出边界所在,而不是假装它不存在。

find_dynamic_references 报告与符号名称匹配的字符串字面量——getattr(mod, "compute")、插件注册表、点分设置字符串、入口点表——并检测下划线前缀的原生模块导入。这些警告会附加到每个重构计划上。

这一点有具体的影响。在标准库 json 包的副本中重命名 JSONDecodeError 会正确重写全部 19 个 Python 引用,但仍然会破坏该包,因为 _json C 加速器通过 C API 在运行时解析该名称。任何纯 Python 分析都无法追踪这一点。该工具会标记原生加速器导入,并提示您手动检查。

同样不可见的还有:from x import * 的再导出(作为警告报告)、运行时猴子补丁,以及来自其他语言或配置文件的引用。

工具

工具

用途

map_repository

扫描目录并构建索引。先调用此工具,应用计划后再调用一次。

repository_overview

统计信息、包、导入循环、被依赖最多的模块。

list_modules

已索引的模块,可按点分前缀过滤。

file_outline

单个文件的导入和定义。

search_symbols

按子字符串、种类或模块前缀查找定义。

get_symbol_source

单个定义的完整源码,包含文档字符串和装饰器。

find_references

符号的每个作用域解析使用。

find_dynamic_references

静态分析无法追踪的字符串字面量和原生导入。

dependency_graph

内部导入边,整个图或单个模块的邻域。

impact_of_change

传递依赖者——编辑模块的爆炸半径。

find_dead_code

没有可发现引用的模块级定义。

plan_rename_symbol

规划仓库级重命名。不写入任何内容。

plan_move_symbol

规划将定义移动到另一个模块。不写入任何内容。

plan_delete_symbol

规划删除定义及其 __all__ 条目。不写入任何内容。

preview_plan

重新渲染待处理计划的差异。

list_plans / discard_plan

检查或丢弃待处理计划。

apply_plan

提交计划,包含哈希检查、语法验证和备份。

安装

pip install -e .

使用 MCP 客户端注册:

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

CARTOGRAPHER_ROOT 只是 map_repository 的默认值;该工具也接受显式的 root 参数。

典型会话

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/

开发

pip install -e ".[dev]"
pytest

65 个测试覆盖了作用域解析(遮蔽、global、推导式、类体可见性)、所有导入风格下的引用查找、重命名/移动/删除的正确性、生成的导入风格,以及应用阶段的保证:过期文件检测、语法验证和回滚。

该测试套件最有力的检查是工具重构其自身源码——在实现和测试中重命名一个函数,并在模块之间移动一个函数——之后完整测试套件仍然能针对重写后的副本通过。

要求

Python 3.10+(使用 ast.alias 位置属性)。仅限 Python 源码。

许可证

MIT

A
license - permissive license
Not graded
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • F
    license
    Not graded
    quality
    D
    maintenance
    Enables querying and analyzing code relationships by building a lightweight graph of TypeScript and Python symbols. Supports symbol lookup, reference tracking, impact analysis from diffs, and code snippet retrieval through natural language.
  • A
    license
    Not graded
    quality
    D
    maintenance
    Provides tools for Python code navigation, analysis, and refactoring, including finding definitions, references, and symbol lists. It enables automated tasks such as renaming symbols and organizing imports to enhance AI-driven development.
    Apache 2.0
  • A
    license
    A
    quality
    D
    maintenance
    Provides Python refactoring capabilities via the Rope library, enabling AI agents to perform safe, project-wide code transformations such as renaming symbols, moving modules, and extracting methods.
    10
    1
    MIT
  • A
    license
    A
    quality
    B
    maintenance
    Enables coding agents to perform safe, project-wide Python refactoring (rename, move, extract, inline, change signature, organize imports, etc.) with a dry-run safety contract and LSP-coordinate addressing.
    15
    MIT

View all related MCP servers

Related MCP Connectors

  • Deterministic context layer for your codebase: change impact, blast radius, answers with receipts.

  • Code intelligence for coding agents: semantic, AST, graph, and full-text search. 279+ languages.

  • Give your AI agent a persistent map of your project's structure, dependencies, and bugs.

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sdg5-hub/MCP-Cartographer'

If you have feedback or need assistance with the MCP directory API, please join our Discord server