Spaces:
Sleeping
Sleeping
batonai-coding-assistant[bot] commited on
Implement Code Chunker MCP Server
Browse files- Create an MCP server named 'Code Chunker Server' that provides functionality to chunk code into logical segments.
- Implement the `chunk_code` tool, which takes code, file extension, and token limit as inputs, processes the code using `CodeChunker`, and returns the resulting chunks as a dictionary.
- Add a resource `get_supported_file_types` to return a list of supported file extensions from `CodeParser`'s language extension map.
These changes enable users to efficiently chunk code based on token counts and provides a way to discover supported file types for the chunking process. The new server facilitates improved interactions with code and enhances UX for developers.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758"""MCP server exposing Code Chunker functionality.
This server provides a tool to chunk code into smaller, logical segmentsand a resource to list supported file extensions."""
from mcp.server.fastmcp import FastMCPfrom CodeParser import CodeParserfrom Chunker import CodeChunkerfrom typing import Dict, List, Optional
# Create an MCP server with the name "Code Chunker Server"mcp = FastMCP("Code Chunker Server")
@mcp.tool()def chunk_code(code: str, file_extension: str, token_limit: int = 25) -> Dict[int, str]: """ Chunks the provided code into logical segments based on token limit. Args: code: The source code to be chunked file_extension: The file extension (e.g., 'py', 'js', 'ts', 'css') token_limit: Target size of each chunk in tokens (default: 25) Returns: A dictionary with chunk numbers as keys and code segments as values """ # Create a code chunker for the specified file extension chunker = CodeChunker(file_extension=file_extension) # Process the code through the chunker chunks = chunker.chunk(code, token_limit) return chunks
@mcp.resource("supported-file-types://list")def get_supported_file_types() -> str: """ Returns a list of file extensions supported by the Code Chunker. Returns: A string containing the list of supported file extensions """ # Get the file extensions from CodeParser's language extension map code_parser = CodeParser() supported_extensions = list(code_parser.language_extension_map.keys()) # Format the list for display extension_list = ", ".join(supported_extensions) return f"Supported file extensions: {extension_list}"
if __name__ == "__main__": # Run the server when the script is executed directly mcp.run()mcp_server.pyAdded+58