-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmcp_server_code_extractor_new.py
More file actions
483 lines (372 loc) · 16.8 KB
/
mcp_server_code_extractor_new.py
File metadata and controls
483 lines (372 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "mcp[cli]",
# "tree-sitter-languages",
# "tree-sitter==0.21.3",
# ]
# ///
"""
MCP Extract - Simple code extraction using tree-sitter-languages
A single-file MCP server that extracts functions and classes from code files.
No more grep/sed/awk gymnastics - just clean, precise extraction.
🚨 **CRITICAL WORKFLOW GUIDANCE FOR CLAUDE** 🚨
**STOP USING READ/SEARCH/GREP FOR CODE INVESTIGATION!**
❌ **WRONG**: Read(file) → Search(pattern) → Edit
✅ **CORRECT**: get_symbols(file) → get_function(file, name) → Edit
**MANDATORY STEPS**:
1. ALWAYS start with get_symbols(file) to see what's in the file
2. Use get_function(file, name) to extract specific functions
3. Use get_class(file, name) for class definitions
4. NEVER use Read() to "examine" or "investigate" code files
**🚫 Using Read() on code files wastes context and misses structure**
**COMMON SCENARIOS**:
- Testing: get_symbols(test_file) → get_function(test_file, "test_method_name")
- Debugging: get_symbols(file) → get_function(file, "problematic_function")
- Refactoring: get_symbols(file) → get_class(file, "ClassName")
- Investigation: get_symbols(file) → get_lines(file, start, end)
Usage with uv:
1. Install uv: curl -LsSf https://astral.sh/uv/install.sh | sh
2. Run directly: uv run mcp-extract.py
3. Configure in Claude Desktop with: uv run /path/to/mcp-extract.py
Or traditional install:
pip install mcp[cli] tree-sitter-languages
"""
import os
import sys
from pathlib import Path
# Add current directory to path for local imports
sys.path.insert(0, os.path.dirname(__file__))
try:
from code_extractor import CodeExtractor, create_extractor
from code_extractor.models import SymbolKind
from code_extractor.languages import get_language_for_file, is_language_supported
except ImportError as e:
print(f"Error: Code extractor library not found: {e}")
print("Make sure it's installed or run from the correct directory.")
exit(1)
try:
from mcp.server import Server
from mcp.types import Tool, TextContent
import mcp.server.stdio
except ImportError:
print("Error: MCP library not found. Install with: pip install mcp[cli]")
exit(1)
# Global server instance
server = Server("code-extractor")
def get_function(file_path: str, function_name: str) -> dict:
"""
Extract a complete function definition - USE THIS INSTEAD OF Read() for specific functions!
🎯 **PRECISE EXTRACTION** - Gets exact function boundaries with line numbers using tree-sitter.
⚠️ **REPLACES Read() + manual parsing** - No need to read entire files and search manually.
Args:
file_path: Path to the source file
function_name: Exact name of the function to extract
Returns:
dict with code, start_line, end_line, lines, function, file, language
**WORKFLOW**: get_symbols() first → get_function() for specific extraction → Edit
"""
if not os.path.exists(file_path):
return {"error": f"File not found: {file_path}"}
try:
extractor = create_extractor(file_path)
with open(file_path, 'r', encoding='utf-8') as f:
source_code = f.read()
symbol = extractor.extract_function(source_code, function_name)
if symbol is None:
return {"error": f"Function '{function_name}' not found in {file_path}"}
# Extract the actual code for the function
lines = source_code.split('\n')
function_lines = lines[symbol.start_line-1:symbol.end_line]
code = '\n'.join(function_lines)
return {
"code": code,
"start_line": symbol.start_line,
"end_line": symbol.end_line,
"lines": symbol.lines,
"function": symbol.name,
"file": file_path,
"language": extractor.language,
# Enhanced details from new extractor
"parameters": [str(p) for p in symbol.parameters],
"return_type": symbol.return_type,
"is_async": symbol.is_async,
"parent": symbol.parent,
"docstring": symbol.docstring
}
except Exception as e:
return {"error": f"Failed to extract function '{function_name}' from '{file_path}': {str(e)}"}
def get_class(file_path: str, class_name: str) -> dict:
"""
Extract a complete class definition - USE THIS INSTEAD OF Read() for specific classes!
🎯 **PRECISE EXTRACTION** - Gets exact class boundaries with all methods using tree-sitter.
⚠️ **REPLACES Read() + manual parsing** - No need to read entire files and search manually.
Args:
file_path: Path to the source file
class_name: Exact name of the class to extract
Returns:
dict with code, start_line, end_line, lines, class, file, language
**WORKFLOW**: get_symbols() first → get_class() for specific extraction → Edit
"""
if not os.path.exists(file_path):
return {"error": f"File not found: {file_path}"}
try:
extractor = create_extractor(file_path)
with open(file_path, 'r', encoding='utf-8') as f:
source_code = f.read()
symbol = extractor.extract_class(source_code, class_name)
if symbol is None:
return {"error": f"Class '{class_name}' not found in {file_path}"}
# Extract the actual code for the class
lines = source_code.split('\n')
class_lines = lines[symbol.start_line-1:symbol.end_line]
code = '\n'.join(class_lines)
# Get all methods in this class
all_symbols = extractor.extract_symbols(source_code)
methods = [s for s in all_symbols if s.kind == SymbolKind.METHOD and s.parent == class_name]
return {
"code": code,
"start_line": symbol.start_line,
"end_line": symbol.end_line,
"lines": symbol.lines,
"class": symbol.name,
"file": file_path,
"language": extractor.language,
# Enhanced details from new extractor
"docstring": symbol.docstring,
"methods": [m.name for m in methods],
"method_count": len(methods)
}
except Exception as e:
return {"error": f"Failed to extract class '{class_name}' from '{file_path}': {str(e)}"}
def get_symbols(file_path: str) -> list:
"""
🚨 **ALWAYS USE THIS FIRST** for code investigation - DO NOT use Read() on code files!
List all functions, classes, and other symbols in a file with their line numbers.
This is the CORRECT way to explore code structure instead of reading entire files.
⚠️ **REPLACES Read() for code files** - More efficient and structured than reading entire files.
Args:
file_path: Path to the source file to analyze
Returns:
List of symbols with name, type, start_line, end_line, lines, and preview
**WORKFLOW**: get_symbols() → get_function()/get_class() → Edit (NOT Read → Search → Edit)
"""
if not os.path.exists(file_path):
return [{"error": f"File not found: {file_path}"}]
try:
extractor = create_extractor(file_path)
with open(file_path, 'r', encoding='utf-8') as f:
source_code = f.read()
symbols = extractor.extract_symbols(source_code)
# Convert to dict format for MCP compatibility
result = []
for symbol in symbols:
result.append(symbol.to_dict())
return result
except Exception as e:
return [{"error": f"Failed to parse '{file_path}': {str(e)}"}]
def get_lines(file_path: str, start_line: int, end_line: int) -> dict:
"""
Get specific lines from a file using precise line range control.
Use this when you know exact line numbers you need (e.g., from get_symbols output) and
want to extract specific code sections without reading the entire file.
Args:
file_path: Path to the source file
start_line: Starting line number (1-based, inclusive)
end_line: Ending line number (1-based, inclusive)
Returns:
dict with code, start_line, end_line, lines, file, total_lines
**WORKFLOW**: get_symbols() first → get_lines() for specific ranges → Edit
"""
if not os.path.exists(file_path):
return {"error": f"File not found: {file_path}"}
try:
with open(file_path, 'r', encoding='utf-8') as f:
all_lines = f.readlines()
total_lines = len(all_lines)
# Validate line numbers
if start_line < 1 or end_line < 1:
return {"error": "Line numbers must be >= 1"}
if start_line > total_lines:
return {"error": f"Start line {start_line} exceeds file length {total_lines}"}
# Adjust end_line if it exceeds file length
end_line = min(end_line, total_lines)
# Extract lines (convert to 0-based indexing)
selected_lines = all_lines[start_line-1:end_line]
code = ''.join(selected_lines)
return {
"code": code,
"start_line": start_line,
"end_line": end_line,
"lines": f"{start_line}-{end_line}",
"file": file_path,
"total_lines": total_lines
}
except Exception as e:
return {"error": f"Failed to read lines from '{file_path}': {str(e)}"}
def get_signature(file_path: str, function_name: str) -> dict:
"""
Get just the signature/declaration of a function without the full implementation.
Use this when you only need to see function interfaces, parameters, and return types
for API exploration or documentation. Lighter weight than get_function.
Args:
file_path: Path to the source file
function_name: Exact name of the function
Returns:
dict with signature, name, file, start_line, lines
**WORKFLOW**: get_symbols() first → get_signature() for interface info → Edit
"""
if not os.path.exists(file_path):
return {"error": f"File not found: {file_path}"}
try:
extractor = create_extractor(file_path)
with open(file_path, 'r', encoding='utf-8') as f:
source_code = f.read()
symbol = extractor.extract_function(source_code, function_name)
if symbol is None:
return {"error": f"Function '{function_name}' not found in {file_path}"}
return {
"signature": symbol.signature,
"name": symbol.name,
"file": file_path,
"start_line": symbol.start_line,
"lines": symbol.lines,
"parameters": [str(p) for p in symbol.parameters],
"return_type": symbol.return_type,
"is_async": symbol.is_async,
"parent": symbol.parent
}
except Exception as e:
return {"error": f"Failed to get signature for '{function_name}' from '{file_path}': {str(e)}"}
# MCP Server Tools
@server.list_tools()
async def list_tools():
"""List available tools for code extraction."""
return [
Tool(
name="get_function",
description="""Extract a complete function definition - USE THIS INSTEAD OF Read() for specific functions!
🎯 **PRECISE EXTRACTION** - Gets exact function boundaries with line numbers using tree-sitter.
⚠️ **REPLACES Read() + manual parsing** - No need to read entire files and search manually.
Args:
file_path: Path to the source file
function_name: Exact name of the function to extract
Returns:
dict with code, start_line, end_line, lines, function, file, language
**WORKFLOW**: get_symbols() first → get_function() for specific extraction → Edit""",
inputSchema={
"type": "object",
"properties": {
"file_path": {"type": "string"},
"function_name": {"type": "string"}
},
"required": ["file_path", "function_name"]
}
),
Tool(
name="get_class",
description="""Extract a complete class definition - USE THIS INSTEAD OF Read() for specific classes!
🎯 **PRECISE EXTRACTION** - Gets exact class boundaries with all methods using tree-sitter.
⚠️ **REPLACES Read() + manual parsing** - No need to read entire files and search manually.
Args:
file_path: Path to the source file
class_name: Exact name of the class to extract
Returns:
dict with code, start_line, end_line, lines, class, file, language
**WORKFLOW**: get_symbols() first → get_class() for specific extraction → Edit""",
inputSchema={
"type": "object",
"properties": {
"file_path": {"type": "string"},
"class_name": {"type": "string"}
},
"required": ["file_path", "class_name"]
}
),
Tool(
name="get_symbols",
description="""🚨 **ALWAYS USE THIS FIRST** for code investigation - DO NOT use Read() on code files!
List all functions, classes, and other symbols in a file with their line numbers.
This is the CORRECT way to explore code structure instead of reading entire files.
⚠️ **REPLACES Read() for code files** - More efficient and structured than reading entire files.
Args:
file_path: Path to the source file to analyze
Returns:
List of symbols with name, type, start_line, end_line, lines, and preview
**WORKFLOW**: get_symbols() → get_function()/get_class() → Edit (NOT Read → Search → Edit)""",
inputSchema={
"type": "object",
"properties": {
"file_path": {"type": "string"}
},
"required": ["file_path"]
}
),
Tool(
name="get_lines",
description="""Get specific lines from a file using precise line range control.
Use this when you know exact line numbers you need (e.g., from get_symbols output) and
want to extract specific code sections without reading the entire file.
Args:
file_path: Path to the source file
start_line: Starting line number (1-based, inclusive)
end_line: Ending line number (1-based, inclusive)
Returns:
dict with code, start_line, end_line, lines, file, total_lines
**WORKFLOW**: get_symbols() first → get_lines() for specific ranges → Edit""",
inputSchema={
"type": "object",
"properties": {
"file_path": {"type": "string"},
"start_line": {"type": "integer"},
"end_line": {"type": "integer"}
},
"required": ["file_path", "start_line", "end_line"]
}
),
Tool(
name="get_signature",
description="""Get just the signature/declaration of a function without the full implementation.
Use this when you only need to see function interfaces, parameters, and return types
for API exploration or documentation. Lighter weight than get_function.
Args:
file_path: Path to the source file
function_name: Exact name of the function
Returns:
dict with signature, name, file, start_line, lines
**WORKFLOW**: get_symbols() first → get_signature() for interface info → Edit""",
inputSchema={
"type": "object",
"properties": {
"file_path": {"type": "string"},
"function_name": {"type": "string"}
},
"required": ["file_path", "function_name"]
}
)
]
@server.call_tool()
async def call_tool(name: str, arguments: dict):
"""Handle tool calls."""
try:
if name == "get_function":
result = get_function(arguments["file_path"], arguments["function_name"])
elif name == "get_class":
result = get_class(arguments["file_path"], arguments["class_name"])
elif name == "get_symbols":
result = get_symbols(arguments["file_path"])
elif name == "get_lines":
result = get_lines(arguments["file_path"], arguments["start_line"], arguments["end_line"])
elif name == "get_signature":
result = get_signature(arguments["file_path"], arguments["function_name"])
else:
return [TextContent(type="text", text=f"Unknown tool: {name}")]
return [TextContent(type="text", text=str(result))]
except Exception as e:
return [TextContent(type="text", text=f"Error: {str(e)}")]
def main():
"""Run the MCP server."""
mcp.server.stdio.run_server(server)
if __name__ == "__main__":
main()