code-ast-mcp
Code AST MCP Server (code-ast-mcp)
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
⚡ 功能与特性
🛠️ 工具
analyze_file_ast(file_path: str)将
.py文件解析为清晰的 AST 大纲。提取模块文档字符串、行数、导入、顶层函数、类、方法和变量。
find_class_methods(file_path: str, class_name: str)定位特定类,并返回方法签名、类型注解、行范围和文档字符串。
find_symbol(target_dir: str, symbol_name: str)递归搜索目录,查找与
symbol_name匹配的类、函数、方法或变量赋值。
get_imports_graph(target_dir: str)扫描 Python 文件以构建依赖导入映射,并输出 Mermaid 图表字符串。
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、监控和访问控制进行托管。
步骤:
点击 "Add Server"。
使用 GitHub OAuth 进行身份验证(你必须对该仓库具有写权限)。
提交仓库 URL:
https://github.com/m-sameerkhan/code-ast-mcpGlama 将使用
Dockerfile自动构建并验证 MCP 合规性。构建成功后,你的服务器将在 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 toolsanalyze_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
Scored across 5 tools
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.
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.
Five tools is a well-scoped size for an AST inspection server. Each tool serves a distinct static analysis workflow without redundancy or bloat.
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
Related MCP Connectors
An MCP server that gives your AI access to the source code and docs of all public github repos
Repository knowledge graph MCP server for codebase understanding and debugging.
A comprehensive Model Context Protocol (MCP) server that enables AI assistants to interact with yo…
Official Duck Framework MCP server for AI-assisted development and project scaffolding.
Related MCP Servers
- FlicenseBqualityDmaintenanceAn 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.618-
- AlicenseNot gradedqualityDmaintenanceAn MCP server that gives AI agents structured code understanding and precise code intelligence via local indexing of AST, call graphs, and semantic search.110 npm4Apache 2.0
- -licenseNot gradedqualityNot gradedmaintenanceAn 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.-
- AlicenseAqualityDmaintenanceUniversal MCP server that analyzes any codebase and provides structured context to AI assistants. Dynamic, accurate, and token-efficient.18MIT