Skip to main content
Glama
aindreyway

MCP Server Neurolorap

by aindreyway

code_collector

Collect code from multiple files and directories into a single markdown document for documentation and reference purposes.

Instructions

Collect code from files into a markdown document

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
input_pathNo.
titleNoCode Collection
subproject_idNo

Implementation Reference

  • The asynchronous handler function that executes the code_collector tool. It instantiates CodeCollector and calls its collect_code method to perform the actual code collection.
    async def code_collector(
        input_path: str | list[str] = ".",
        title: str = "Code Collection",
        subproject_id: str | None = None,
    ) -> str:
        """Collect code from files into a markdown document."""
        logger.debug("Tool call: code_collector")
        logger.debug(
            "Arguments: input=%s, title=%s, subproject_id=%s",
            input_path,
            title,
            subproject_id,
        )
    
        try:
            root_path = get_project_root()
            collector = CodeCollector(
                project_root=root_path, subproject_id=subproject_id
            )
    
            logger.info("Starting code collection")
            logger.debug("Input: %s", input_path)
            logger.debug("Title: %s", title)
            logger.debug("Subproject ID: %s", subproject_id)
    
            output_file = collector.collect_code(input_path, title)
            if not output_file:
                msg = "No files found to process or error occurred"
                return msg
    
            return f"Code collection complete!\nOutput file: {output_file}"
    
        except Exception as e:
            error_msg = f"Unexpected error collecting code: {e}"
            logger.error(error_msg, exc_info=True)
            return "No files found to process or error occurred"
  • Registration of the code_collector tool with the FastMCP server instance, including name and description which define the tool schema.
        name="code_collector",
        description="Collect code from files into a markdown document",
    )(code_collector)
  • The core helper method in CodeCollector class that implements the logic to collect files, generate markdown with code blocks, table of contents, and also creates an analysis prompt file. This is the main implementation delegated to by the handler.
    def collect_code(
        self,
        input_paths: Union[str, List[str]],
        title: str = "Code Collection",
    ) -> Optional[Path]:
        """Process all files and generate markdown documentation.
    
        Args:
            input_paths: Path(s) to process.
            title: Title for the collection.
    
        Returns:
            Optional[Path]: Path to generated markdown file or None if failed.
            The path is returned as a Path object if successful,
            None if failed.
        """
        try:
            all_files = self.collect_files(input_paths)
            if not all_files:
                logger.warning("No files found to process")
                return None
    
            # Create output files with timestamp and path info
            from datetime import datetime
    
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    
            # Convert input paths to string
            if isinstance(input_paths, str):
                path_str = input_paths
            else:
                path_str = "_".join(input_paths)
            # Clean up path string
            path_str = path_str.replace("/", "_").replace(".", "_")
    
            code_output_path = self.storage.get_output_path(
                f"FULL_CODE_{timestamp}_{path_str}_{title}.md"
            )
    
            # Create parent directory if it doesn't exist
            code_output_path.parent.mkdir(parents=True, exist_ok=True)
    
            # Write code collection file
            with open(code_output_path, "w", encoding="utf-8") as output_file:
                # Write header
                output_file.write(f"# {title}\n\n")
                output_file.write(
                    "This file contains code from the specified paths, "
                    "organized by file path.\n\n"
                )
    
                # Write table of contents
                output_file.write("## Table of Contents\n\n")
                for file_path in all_files:
                    try:
                        relative_path = file_path.relative_to(
                            self.project_root
                        )
                    except ValueError:
                        relative_path = file_path
                    anchor = self.make_anchor(relative_path)
                    output_file.write(f"- [{relative_path}](#{anchor})\n")
    
                # Write file contents
                output_file.write("\n## Files\n\n")
                for file_path in all_files:
                    try:
                        relative_path = file_path.relative_to(
                            self.project_root
                        )
                    except ValueError:
                        relative_path = file_path
    
                    content = self.read_file_content(file_path)
                    anchor = self.make_anchor(relative_path)
                    lang = LanguageMap.get_language(file_path)
    
                    output_file.write(f"### {relative_path} {{{anchor}}}\n")
                    output_file.write(f"```{lang}\n{content}\n```\n\n")
                    logger.debug(f"Processed: {relative_path}")
    
                # Force flush and sync
                output_file.flush()
                os.fsync(output_file.fileno())
    
            # Force sync to ensure file is visible
            os.sync()
    
            # Wait a bit for filesystem to update
            import time
    
            time.sleep(0.1)
    
            # Create analysis prompt file with timestamp
            analyze_output_path = self.storage.get_output_path(
                f"PROMPT_ANALYZE_{timestamp}_{path_str}_{title}.md"
            )
            prompt_path = (
                Path(__file__).parent / "prompts" / "analyze.prompt.md"
            )
    
            # Read code content first
            code_content = self.read_file_content(code_output_path)
            prompt_content = self.read_file_content(prompt_path)
    
            # Write analysis file
            with open(
                analyze_output_path, "w", encoding="utf-8"
            ) as analyze_file:
                analyze_file.write(prompt_content)
                analyze_file.write("\n")
                analyze_file.write(code_content)
                # Force flush and sync
                analyze_file.flush()
                os.fsync(analyze_file.fileno())
    
            # Force sync to ensure file is visible
            os.sync()
    
            # Wait a bit for filesystem to update
            time.sleep(0.1)
    
            # Touch files and all parent directories
            # to trigger VSCode file watcher
            try:
                # Touch output files
                os.utime(code_output_path, None)
                os.utime(analyze_output_path, None)
    
                # Touch all parent directories up to project root
                current = code_output_path.parent
                while (
                    current != self.project_root and current != current.parent
                ):
                    os.utime(current, None)
                    current = current.parent
    
                os.utime(self.project_root, None)
            except Exception as e:
                logger.debug(
                    f"Failed to touch project root: {str(e)}"
                )  # nosec B110
                # Intentionally catching all exceptions for
                # filesystem operations
    
            # Verify files exist and are accessible
            if not code_output_path.exists():
                raise RuntimeError(
                    f"Failed to create code file: {code_output_path}"
                )
            if not analyze_output_path.exists():
                raise RuntimeError(
                    f"Failed to create analysis file: {analyze_output_path}"
                )
    
            logger.debug(f"Analysis prompt created: {analyze_output_path}")
            logger.info("Code collection complete!")
            logger.debug(f"Output file: {code_output_path}")
            return Path(code_output_path)
    
        except FileNotFoundError as e:
            logger.error(f"File not found error: {str(e)}")
            return None
        except PermissionError as e:
            logger.error(f"Permission denied error: {str(e)}")
            return None
        except OSError as e:
            logger.error(f"OS error during code collection: {str(e)}")
            return None
        except RuntimeError as e:
            logger.error(f"Runtime error during code collection: {str(e)}")
            return None
        except Exception as e:
            logger.error(f"Unexpected error during code collection: {str(e)}")
            logger.debug("Stack trace:", exc_info=True)
            return None
  • The CodeCollector class initialization, setting up project root, storage, and ignore patterns.
    class CodeCollector:
Install Server

Other Tools

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/aindreyway/mcp-server-neurolora-p'

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