CoolFace
Apppublic

CintraAI/code-chunker

sourceHugging Facemitupdated 1y agoView on Hugging Face
5likes
mcp_server.py58 linesDownload Raw Back to root
1"""2MCP server exposing Code Chunker functionality.3 4This server provides a tool to chunk code into smaller, logical segments5and a resource to list supported file extensions.6"""7 8from mcp.server.fastmcp import FastMCP9from CodeParser import CodeParser10from Chunker import CodeChunker11from typing import Dict, List, Optional12 13# Create an MCP server with the name "Code Chunker Server"14mcp = FastMCP("Code Chunker Server")15 16 17@mcp.tool()18def chunk_code(code: str, file_extension: str, token_limit: int = 25) -> Dict[int, str]:19    """20    Chunks the provided code into logical segments based on token limit.21    22    Args:23        code: The source code to be chunked24        file_extension: The file extension (e.g., 'py', 'js', 'ts', 'css')25        token_limit: Target size of each chunk in tokens (default: 25)26        27    Returns:28        A dictionary with chunk numbers as keys and code segments as values29    """30    # Create a code chunker for the specified file extension31    chunker = CodeChunker(file_extension=file_extension)32    33    # Process the code through the chunker34    chunks = chunker.chunk(code, token_limit)35    36    return chunks37 38 39@mcp.resource("supported-file-types://list")40def get_supported_file_types() -> str:41    """42    Returns a list of file extensions supported by the Code Chunker.43    44    Returns:45        A string containing the list of supported file extensions46    """47    # Get the file extensions from CodeParser's language extension map48    code_parser = CodeParser()49    supported_extensions = list(code_parser.language_extension_map.keys())50    51    # Format the list for display52    extension_list = ", ".join(supported_extensions)53    return f"Supported file extensions: {extension_list}"54 55 56if __name__ == "__main__":57    # Run the server when the script is executed directly58    mcp.run()