Skip to main content
Glama
minami110

GDScript Code Analyzer

by minami110

get_gdscript_dependencies

Extract dependencies from GDScript files to identify extends, preload, and import statements for code analysis.

Instructions

Extract dependencies from a GDScript file (extends, preload, import statements).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYesPath to the GDScript file

Implementation Reference

  • Registration of the 'get_gdscript_dependencies' tool, including name, description, and input schema.
    Tool(
        name="get_gdscript_dependencies",
        description="Extract dependencies from a GDScript file (extends, preload, import statements).",
        inputSchema={
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Path to the GDScript file",
                }
            },
            "required": ["file_path"],
        },
    ),
  • The main handler function for the tool that reads the GDScript file, parses it with tree-sitter, extracts dependencies via the parser, formats the result as JSON, and returns it.
    def _get_dependencies(self, file_path: str) -> CallToolResult:
        """Get dependencies from a GDScript file.
    
        Args:
            file_path: Path to the file
    
        Returns:
            CallToolResult with dependencies
        """
        try:
            path = Path(file_path)
            if not path.exists():
                return CallToolResult(
                    content=[TextContent(type="text", text=f"File not found: {file_path}")],
                    isError=True,
                )
    
            code = path.read_text(encoding="utf-8")
            tree = self.parser.parse(code)
            dependencies = self.parser.get_dependencies(tree, code)
    
            result = {
                "file": file_path,
                "dependencies": dependencies,
            }
    
            return CallToolResult(
                content=[TextContent(type="text", text=json.dumps(result, indent=2))],
                isError=False,
            )
        except Exception as e:
            return CallToolResult(
                content=[TextContent(type="text", text=f"Error getting dependencies: {str(e)}")],
                isError=True,
            )
  • Helper method in the parser that initializes the dependencies dict and calls the recursive extractor.
    def get_dependencies(self, tree: Tree, code: str) -> dict[str, list[str]]:
        """Extract dependencies (extends, preload) from the file.
    
        Args:
            tree: The parsed syntax tree
            code: The original source code
    
        Returns:
            Dictionary with lists of dependencies
        """
        dependencies = {
            "extends": [],
            "preload": [],
            "import": [],
        }
    
        self._extract_dependencies(tree.root_node, dependencies)
        return dependencies
  • Core recursive helper that traverses the parse tree to identify and extract extends, preload, and import dependencies.
    def _extract_dependencies(self, node: Node, deps: dict[str, list[str]]) -> None:
        """Recursively extract dependencies from tree nodes.
    
        Args:
            node: Current tree node
            deps: Dependencies dictionary to populate
        """
        node_type = node.type
    
        # Check for extends statement
        if node_type in ("extends_statement", "extend_statement"):
            # For extends, the type is in the "type" child node
            for child in node.children:
                if child.type == "type":
                    path = child.text.decode("utf-8") if isinstance(child.text, bytes) else str(child.text)
                    if path:
                        deps["extends"].append(path)
                elif child.type == "identifier":
                    path = child.text.decode("utf-8") if isinstance(child.text, bytes) else str(child.text)
                    if path and path != "extends":
                        deps["extends"].append(path)
    
        # Check for preload function calls (both function_call and call node types)
        elif node_type in ("function_call", "call"):
            if self._is_preload_call(node):
                path = self._extract_string_argument(node)
                if path:
                    deps["preload"].append(path)
    
        # Check for import statements
        elif node_type == "import_statement":
            path = self._extract_string_value(node)
            if path:
                deps["import"].append(path)
    
        # Recursively process child nodes
        for child in node.children:
            self._extract_dependencies(child, deps)

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/minami110/mcp-gdscript'

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