Joe Shamon commited on
Commit
6eec371
·
unverified ·
2 Parent(s): 1d1a01b dd62878

Merge pull request #19 from CintraAI/feature/mcp-python-sdk-implementation-baton-BATON-1749669400-843

Browse files
Files changed (1) hide show
  1. 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()