py-ast-mcp

v0.2.1
py-ast-mcp Checked 2 days ago Published 4 months ago

MCP server for deep structural analysis of Python source via the stdlib ast module

These tools were reported by the server itself during our smoke test (tools/list), not copied from the README.

analyze_file

High-level summary of every symbol in a Python file. Lists classes (with dataclass/enum/Protocol/TypedDict classification), functions, async functions, methods and module-level assignments with line numbers. Start here when exploring an unfamiliar module. Args: path: Path to a .py file.

ParameterTypeDescription
path *string

list_functions

List all functions and methods with full signatures and line ranges. Includes parameter annotations and defaults, return annotation, decorators, async flag, and nested function relationships. Args: path: Path to a .py file.

ParameterTypeDescription
path *string

get_function_body

Extract the full source of a function or method, with line numbers. Args: path: Path to a .py file. name: Function name, `Class.method`, or a dotted nested path.

ParameterTypeDescription
name *string
path *string

list_methods

List every method of a class: declared, inherited, properties, class and static methods, plus class-level attributes. Inherited members are only resolved for base classes defined in the same file; bases from other modules are reported as unresolved. Args: path: Path to a .py file. type: Class name.

ParameterTypeDescription
path *string
type *string

get_type_definition

Extract a class, TypeAlias, Enum, Protocol, TypedDict or NamedTuple definition with its members and source. Args: path: Path to a .py file. name: Type name.

ParameterTypeDescription
name *string
path *string

list_declarations

List module-level assignments with annotated or inferred types. Args: path: Path to a .py file.

ParameterTypeDescription
path *string

list_exports

List the public symbols of a module. Respects `__all__` when present, otherwise reports non-underscore module-level names, and flags re-exported imports. Args: path: Path to a .py file.

ParameterTypeDescription
path *string

list_imports

List all imports: bound name, module path, relative level and aliases, grouped into stdlib / third-party / relative. Args: path: Path to a .py file.

ParameterTypeDescription
path *string

find_usages

Find every occurrence of an identifier with surrounding source lines. Covers reads, assignments, parameters, attribute access, imports and global/nonlocal declarations. Always reports a project-wide references section: either the cross-file hits, or that there are none, or that `jedi` is not installed and cross-file references were therefore not checked. An empty result and an unchecked one are not the same answer. Args: path: Path to a .py file. identifier: Name to search for. context: Lines of context to show around each hit (default 1).

ParameterTypeDescription
identifier *string
path *string
contextinteger

call_graph

Emit a Mermaid flowchart of the call relationships in a file or package. Args: path: Path to a .py file. function: Optional root; only calls reachable from it are drawn. direction: Mermaid layout direction: TD, TB, LR, RL or BT. include_external: Include calls to names not defined in scope. scope: "file" (default) or "package" to walk every .py file in the containing directory.

ParameterTypeDescription
path *string
directionstring
function
include_externalboolean
scopestring

get_callers

Reverse call graph: who calls this function, directly and transitively. Args: path: Path to a .py file. function: Function name or `Class.method`. scope: "file" (default) or "package".

ParameterTypeDescription
function *string
path *string
scopestring

code_complexity

Cyclomatic complexity per function, with rank and decision-point breakdown. Counts if/elif, for, while, except, comprehension fors and ifs, boolean operators, ternaries, and match cases other than the irrefutable one. Scores match `radon cc --no-assert`. `with` and `assert` are deliberately not counted: neither branches. `with` still adds nesting depth. Args: path: Path to a .py file. function: Optional single function to analyse in detail.

ParameterTypeDescription
path *string
function

code_smells

Detect long functions, deep nesting, god classes, too many parameters, mutable default arguments, mutable `@dataclass` field defaults, high complexity, bare excepts and shadowed builtins. Args: path: Path to a .py file. function: Optional single function to analyse.

ParameterTypeDescription
path *string
function

find_errors

Find Python-specific hazards: bare/broad except, `except: pass`, handlers made unreachable by an earlier one, mutable default args, mutable `@dataclass` field defaults (an import-time ValueError), unawaited coroutines (best effort), `assert` used for runtime validation, late-binding closures over a loop or comprehension variable, `==` against None/True/False, `is` against a literal, and methods that never use `self`. Args: path: Path to a .py file. function: Optional single function to analyse.

ParameterTypeDescription
path *string
function

dead_code

Find unreferenced private and module-level symbols across a directory. If `path` is a file, its containing directory is scanned so that cross-file references are seen. Args: path: A .py file or a directory. include_tests: Also scan test files (default False).

ParameterTypeDescription
path *string
include_testsboolean

find_implementations

Find classes implementing a Protocol or ABC, both explicitly (as a base class, including indirect subclasses) and structurally (method-set match). Args: path: A .py file or directory; the containing directory is scanned. interface: Name of the Protocol/ABC/base class. Named `interface` rather than `protocol` to match the identical tool in the TypeScript server, so one vocabulary works across both.

ParameterTypeDescription
interface *string
path *string

get_doc

Extract a docstring and parse it into summary/params/returns/raises when it follows Google or NumPy style. Args: path: Path to a .py file. name: Symbol name, `Class.method`, or "module" for the module docstring.

ParameterTypeDescription
name *string
path *string

analyze_package

Directory-level summary of every .py file: sizes, class/function counts, docstrings and per-file symbol lists. Args: path: Directory to analyse. include_tests: Include test files (default False).

ParameterTypeDescription
path *string
include_testsboolean

diff_ast

Structural diff between two Python files: added, removed and modified functions, classes, methods, module variables, imports and `__all__`. Signature-level, not text-level. Args: old_path: Path to the original file. new_path: Path to the updated file.

ParameterTypeDescription
new_path *string
old_path *string

find_node_at_position

Identify the AST node at a cursor position with its enclosing scope chain. Args: path: Path to a .py file. line: 1-based line number. column: 0-based column offset.

ParameterTypeDescription
column *integer
line *integer
path *string