Spaces:
Running
Running
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.
- mcp_server.py +58 -0
mcp_server.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
MCP server exposing Code Chunker functionality.
|
| 3 |
+
|
| 4 |
+
This server provides a tool to chunk code into smaller, logical segments
|
| 5 |
+
and a resource to list supported file extensions.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from mcp.server.fastmcp import FastMCP
|
| 9 |
+
from CodeParser import CodeParser
|
| 10 |
+
from Chunker import CodeChunker
|
| 11 |
+
from typing import Dict, List, Optional
|
| 12 |
+
|
| 13 |
+
# Create an MCP server with the name "Code Chunker Server"
|
| 14 |
+
mcp = FastMCP("Code Chunker Server")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@mcp.tool()
|
| 18 |
+
def 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 chunked
|
| 24 |
+
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 values
|
| 29 |
+
"""
|
| 30 |
+
# Create a code chunker for the specified file extension
|
| 31 |
+
chunker = CodeChunker(file_extension=file_extension)
|
| 32 |
+
|
| 33 |
+
# Process the code through the chunker
|
| 34 |
+
chunks = chunker.chunk(code, token_limit)
|
| 35 |
+
|
| 36 |
+
return chunks
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@mcp.resource("supported-file-types://list")
|
| 40 |
+
def 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 extensions
|
| 46 |
+
"""
|
| 47 |
+
# Get the file extensions from CodeParser's language extension map
|
| 48 |
+
code_parser = CodeParser()
|
| 49 |
+
supported_extensions = list(code_parser.language_extension_map.keys())
|
| 50 |
+
|
| 51 |
+
# Format the list for display
|
| 52 |
+
extension_list = ", ".join(supported_extensions)
|
| 53 |
+
return f"Supported file extensions: {extension_list}"
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
# Run the server when the script is executed directly
|
| 58 |
+
mcp.run()
|