Skip to main content
Glama

Code AST MCP Server (code-ast-mcp)

Model Context Protocol Python 3.10+ Glama MCP License: MIT

code-ast-mcp 是一个基于 Python 原生抽象语法树(ast)解析器构建的架构级模型上下文协议(MCP)服务器。它允许 AI 模型(在 Claude Desktop、Cursor、Antigravity 或自定义 MCP 客户端中)分析 Python 代码库结构、搜索符号定义、构建依赖关系图、审计文档字符串覆盖率,以及重构代码,而无需将整个原始源文件加载到 LLM 上下文窗口中。

🔗 实时仓库:https://github.com/m-sameerkhan/code-ast-mcp
🔗 Glama Registry:https://glama.ai/mcp/servers/m-sameerkhan/code-ast-mcp


⚡ 功能与特性

🛠️ 工具

  1. analyze_file_ast(file_path: str)

    • 将 .py 文件解析为清晰的 AST 大纲。

    • 提取模块文档字符串、行数、导入、顶层函数、类、方法和变量。

  2. find_class_methods(file_path: str, class_name: str)

    • 定位特定类,并返回方法签名、类型注解、行范围和文档字符串。

  3. find_symbol(target_dir: str, symbol_name: str)

    • 递归搜索目录,查找与 symbol_name 匹配的类、函数、方法或变量赋值。

  4. get_imports_graph(target_dir: str)

    • 扫描 Python 文件以构建依赖导入映射,并输出 Mermaid 图表字符串。

  5. find_missing_docstrings(target_dir: str, include_private: bool = False)

    • 审计代码库的文档字符串,并计算整体文档字符串覆盖率百分比。

📝 提示词

  • refactor_code_summary(file_path: str)

    • 生成一个结构化提示词,指示 LLM 审查文件的 AST 大纲,并提出重构、设计模式改进和文档修复建议。

📊 资源

  • codeast://stats

    • 提供工作区统计信息的实时 JSON 资源(扫描的文件总数、文档字符串覆盖率百分比、缺失项计数)。


Related MCP server: codeweave-mcp

🚀 部署与使用模式

模式 1:通过 Glama MCP Registry 部署

将 code-ast-mcp 部署到 Glama MCP Registry — Glama 会自动克隆你的 GitHub 仓库,使用附带的 Dockerfile 进行构建,并通过内置的 OAuth 2.1、监控和访问控制进行托管。

步骤:

  1. 前往 glama.ai/mcp/servers。

  2. 点击 "Add Server"。

  3. 使用 GitHub OAuth 进行身份验证(你必须对该仓库具有写权限)。

  4. 提交仓库 URL:

    https://github.com/m-sameerkhan/code-ast-mcp
  5. Glama 将使用 Dockerfile 自动构建并验证 MCP 合规性。

  6. 构建成功后,你的服务器将在 Glama 注册表上上线。

注册表 URL:

https://glama.ai/mcp/servers/m-sameerkhan/code-ast-mcp

注意:无需手动托管 — Glama 会自动处理构建、部署和验证。


模式 2:本地 Stdio MCP 服务器(推荐用于本地代码库)

最适合在 Claude Desktop、Cursor 或 Antigravity 中直接检查本地 Python 项目。

安装:

git clone https://github.com/m-sameerkhan/code-ast-mcp.git
cd code-ast-mcp

# Virtual environment setup
python -m venv .venv
# Windows:
.venv\Scripts\activate
# Linux/macOS:
source .venv/bin/activate

pip install -r requirements.txt
pip install -e .

客户端配置(claude_desktop_config.json / mcp_config.json):

{
  "mcpServers": {
    "code-ast-mcp": {
      "command": "python",
      "args": [
        "-m",
        "code_ast_mcp.server"
      ],
      "cwd": "/path/to/code-ast-mcp"
    }
  }
}

🧪 本地测试

运行单元测试

pytest

使用 MCP Inspector 测试

npx @modelcontextprotocol/inspector python -m code_ast_mcp.server

📦 项目结构

code-ast-mcp/
├── code_ast_mcp/          # Core MCP package
│   ├── __init__.py        # Package exports
│   ├── analyzer.py        # Python AST parsing & static analysis engine
│   └── server.py          # FastMCP server definition & tool handlers
├── tests/                 # Test suite
│   └── test_analyzer.py   # Unit tests with pytest
├── Dockerfile             # Container image definition (used by Glama)
├── pyproject.toml         # Packaging & metadata
└── requirements.txt       # Dependencies

📄 许可证

MIT 许可证。由 m-sameerkhan 创建。

Available Tools

5 tools
analyze_file_astB

Parse a Python file using Python's AST parser and return a detailed outline of its docstrings, imports, top-level classes, functions, variables, and line counts.

:param file_path: Path to the Python (.py) file to analyze.

Inputs: file_path (required). Returns: result

find_class_methodsA

Locate a specific class within a Python file and retrieve all its methods, argument signatures, type annotations, line numbers, and docstrings.

:param file_path: Path to the Python file containing the class. :param class_name: Name of the target class to inspect.

Inputs: file_path (required), class_name (required). Returns: result

find_missing_docstringsB

Audit Python files in a directory to identify modules, classes, and functions that lack docstrings, calculating overall docstring coverage percentage.

:param target_dir: Directory path to audit. :param include_private: Whether to include private functions/methods (starting with '_').

Inputs: target_dir (required), include_private. Returns: result

find_symbolA

Recursively search a directory or file for classes, functions, methods, or variables matching the target symbol name.

:param target_dir: Directory path or file path to search. :param symbol_name: Name or partial name of the symbol to search for.

Inputs: target_dir (required), symbol_name (required). Returns: result

get_imports_graphA

Scan all Python files in a directory to build an import dependency graph and output both a structured map and a Mermaid diagram definition.

:param target_dir: Path to the directory containing Python files.

Inputs: target_dir (required). Returns: result

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A3.7/5.0

    Scored across 5 tools

    Disambiguation4/5

    Each tool has a distinct role: file outline, class methods, symbol search, import graph, and docstring audit. There is minor overlap because analyze_file_ast, find_class_methods, and find_symbol all inspect Python definitions, but their scopes differ enough to guide selection.

    Naming Consistency4/5

    All tool names use snake_case and mostly follow a verb_target pattern. Three use find_, while analyze_file_ast and get_imports_graph deviate slightly, but the naming remains predictable and readable.

    Tool Count5/5

    Five tools is a well-scoped size for an AST inspection server. Each tool serves a distinct static analysis workflow without redundancy or bloat.

    Completeness4/5

    The set covers file outlines, class methods, symbol lookup, import graphs, and docstring coverage, which handles most Python code-inspection needs. Minor gaps exist, such as a dedicated detail view for standalone functions or arbitrary AST node inspection, but they are not critical.

    Maintenance

    ActivityInactive
    ResponsivenessOutdated data

    Related MCP Connectors

    Related MCP Servers

    • F
      license
      B
      quality
      D
      maintenance
      An MCP server that enables LLMs to understand and analyze code structure through function call graphs, allowing AI assistants to explore relationships between functions and analyze dependencies in Python repositories.
      6
      18
      -
    • A
      license
      Not graded
      quality
      D
      maintenance
      An MCP server that gives AI agents structured code understanding and precise code intelligence via local indexing of AST, call graphs, and semantic search.
      110 npm
      4
      Apache 2.0
    • -
      license
      Not graded
      quality
      Not graded
      maintenance
      An advanced MCP server that provides deep code understanding and analysis using GraphRAG, AST parsing, and semantic memory, enabling AI agents to query and interact with complex codebases.
      -
    • A
      license
      A
      quality
      D
      maintenance
      Universal MCP server that analyzes any codebase and provides structured context to AI assistants. Dynamic, accurate, and token-efficient.
      18
      MIT