Skip to main content
Glama

list_file_symbols

Extract all symbols defined in a Python file to analyze code structure and identify functions, classes, and variables for navigation and refactoring.

Instructions

List all symbols defined in a file.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes

Implementation Reference

  • The list_file_symbols function implements the logic for listing symbols in a file, including symbol parsing and error handling.
    async def list_file_symbols(file_path: str) -> str:
        """List all symbols defined in a file."""
        client = _get_client()
    
        path = Path(file_path).resolve()
        if not path.exists():
            return _error(f"File not found: {file_path}")
    
        try:
            await client.open_document(path)
            symbols = await client.search_document_symbols(path)
    
            if not symbols:
                return _not_found(f"No symbols in {path.name}")
    
            def parse_symbol(sym: dict) -> dict:
                name = sym.get("name", "?")
                kind = SYMBOL_KINDS.get(sym.get("kind", 0), "Symbol")
    
                if "range" in sym:
                    range_info = sym.get("range", {}).get("start", {})
                else:
                    range_info = sym.get("location", {}).get("range", {}).get("start", {})
    
                line = range_info.get("line", 0) + 1
                children = [parse_symbol(c) for c in sym.get("children", [])]
    
                result = {"name": name, "kind": kind, "line": line}
                if children:
                    result["children"] = children
                return result
    
            parsed = [parse_symbol(sym) for sym in symbols]
            return _ok({"file": path.name, "path": str(path), "count": len(symbols), "symbols": parsed})
  • The list_file_symbols tool is registered using the @mcp.tool() decorator.
    @mcp.tool()

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/qinsehm1128/mcp-ty'

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