tree-sitter-analyzer 0.2.0__py3-none-any.whl → 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of tree-sitter-analyzer might be problematic. Click here for more details.
- tree_sitter_analyzer/__init__.py +133 -121
- tree_sitter_analyzer/__main__.py +11 -12
- tree_sitter_analyzer/api.py +531 -539
- tree_sitter_analyzer/cli/__init__.py +39 -39
- tree_sitter_analyzer/cli/__main__.py +12 -13
- tree_sitter_analyzer/cli/commands/__init__.py +26 -27
- tree_sitter_analyzer/cli/commands/advanced_command.py +88 -88
- tree_sitter_analyzer/cli/commands/base_command.py +160 -155
- tree_sitter_analyzer/cli/commands/default_command.py +18 -19
- tree_sitter_analyzer/cli/commands/partial_read_command.py +141 -133
- tree_sitter_analyzer/cli/commands/query_command.py +81 -82
- tree_sitter_analyzer/cli/commands/structure_command.py +138 -121
- tree_sitter_analyzer/cli/commands/summary_command.py +101 -93
- tree_sitter_analyzer/cli/commands/table_command.py +232 -233
- tree_sitter_analyzer/cli/info_commands.py +120 -121
- tree_sitter_analyzer/cli_main.py +277 -276
- tree_sitter_analyzer/core/__init__.py +15 -20
- tree_sitter_analyzer/core/analysis_engine.py +591 -574
- tree_sitter_analyzer/core/cache_service.py +320 -330
- tree_sitter_analyzer/core/engine.py +557 -560
- tree_sitter_analyzer/core/parser.py +293 -288
- tree_sitter_analyzer/core/query.py +494 -502
- tree_sitter_analyzer/encoding_utils.py +458 -460
- tree_sitter_analyzer/exceptions.py +337 -340
- tree_sitter_analyzer/file_handler.py +217 -222
- tree_sitter_analyzer/formatters/__init__.py +1 -1
- tree_sitter_analyzer/formatters/base_formatter.py +167 -168
- tree_sitter_analyzer/formatters/formatter_factory.py +78 -74
- tree_sitter_analyzer/formatters/java_formatter.py +287 -270
- tree_sitter_analyzer/formatters/python_formatter.py +255 -235
- tree_sitter_analyzer/interfaces/__init__.py +9 -10
- tree_sitter_analyzer/interfaces/cli.py +528 -557
- tree_sitter_analyzer/interfaces/cli_adapter.py +322 -319
- tree_sitter_analyzer/interfaces/mcp_adapter.py +180 -170
- tree_sitter_analyzer/interfaces/mcp_server.py +405 -416
- tree_sitter_analyzer/java_analyzer.py +218 -219
- tree_sitter_analyzer/language_detector.py +398 -400
- tree_sitter_analyzer/language_loader.py +224 -228
- tree_sitter_analyzer/languages/__init__.py +10 -11
- tree_sitter_analyzer/languages/java_plugin.py +1129 -1113
- tree_sitter_analyzer/languages/python_plugin.py +737 -712
- tree_sitter_analyzer/mcp/__init__.py +31 -32
- tree_sitter_analyzer/mcp/resources/__init__.py +44 -47
- tree_sitter_analyzer/mcp/resources/code_file_resource.py +212 -213
- tree_sitter_analyzer/mcp/resources/project_stats_resource.py +560 -550
- tree_sitter_analyzer/mcp/server.py +333 -345
- tree_sitter_analyzer/mcp/tools/__init__.py +30 -31
- tree_sitter_analyzer/mcp/tools/analyze_scale_tool.py +621 -557
- tree_sitter_analyzer/mcp/tools/analyze_scale_tool_cli_compatible.py +242 -245
- tree_sitter_analyzer/mcp/tools/base_tool.py +54 -55
- tree_sitter_analyzer/mcp/tools/read_partial_tool.py +300 -302
- tree_sitter_analyzer/mcp/tools/table_format_tool.py +362 -359
- tree_sitter_analyzer/mcp/tools/universal_analyze_tool.py +543 -476
- tree_sitter_analyzer/mcp/utils/__init__.py +105 -106
- tree_sitter_analyzer/mcp/utils/error_handler.py +549 -549
- tree_sitter_analyzer/models.py +470 -481
- tree_sitter_analyzer/output_manager.py +261 -264
- tree_sitter_analyzer/plugins/__init__.py +333 -334
- tree_sitter_analyzer/plugins/base.py +477 -446
- tree_sitter_analyzer/plugins/java_plugin.py +608 -625
- tree_sitter_analyzer/plugins/javascript_plugin.py +446 -439
- tree_sitter_analyzer/plugins/manager.py +362 -355
- tree_sitter_analyzer/plugins/plugin_loader.py +85 -83
- tree_sitter_analyzer/plugins/python_plugin.py +606 -598
- tree_sitter_analyzer/plugins/registry.py +374 -366
- tree_sitter_analyzer/queries/__init__.py +26 -27
- tree_sitter_analyzer/queries/java.py +391 -394
- tree_sitter_analyzer/queries/javascript.py +148 -149
- tree_sitter_analyzer/queries/python.py +285 -286
- tree_sitter_analyzer/queries/typescript.py +229 -230
- tree_sitter_analyzer/query_loader.py +254 -260
- tree_sitter_analyzer/table_formatter.py +468 -448
- tree_sitter_analyzer/utils.py +277 -277
- {tree_sitter_analyzer-0.2.0.dist-info → tree_sitter_analyzer-0.3.0.dist-info}/METADATA +21 -6
- tree_sitter_analyzer-0.3.0.dist-info/RECORD +77 -0
- tree_sitter_analyzer-0.2.0.dist-info/RECORD +0 -77
- {tree_sitter_analyzer-0.2.0.dist-info → tree_sitter_analyzer-0.3.0.dist-info}/WHEEL +0 -0
- {tree_sitter_analyzer-0.2.0.dist-info → tree_sitter_analyzer-0.3.0.dist-info}/entry_points.txt +0 -0
|
@@ -1,245 +1,242 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
from
|
|
14
|
-
from
|
|
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
|
-
if not
|
|
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
|
-
arguments
|
|
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
|
-
# Tool instance for easy access
|
|
245
|
-
analyze_scale_tool_cli_compatible = AnalyzeScaleToolCLICompatible()
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
CLI-Compatible Analyze Code Scale MCP Tool
|
|
4
|
+
|
|
5
|
+
This tool provides code scale analysis with output format
|
|
6
|
+
that matches the CLI --advanced --statistics output exactly.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import time
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from ...core.analysis_engine import get_analysis_engine
|
|
14
|
+
from ...language_detector import detect_language_from_file
|
|
15
|
+
from ...utils import setup_logger
|
|
16
|
+
|
|
17
|
+
# Set up logging
|
|
18
|
+
logger = setup_logger(__name__)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class AnalyzeScaleToolCLICompatible:
|
|
22
|
+
"""
|
|
23
|
+
MCP Tool for analyzing code scale with CLI-compatible output format.
|
|
24
|
+
|
|
25
|
+
This tool matches the exact output format of CLI --advanced --statistics.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(self) -> None:
|
|
29
|
+
"""Initialize the CLI-compatible analyze scale tool."""
|
|
30
|
+
self.analysis_engine = get_analysis_engine()
|
|
31
|
+
logger.info("AnalyzeScaleToolCLICompatible initialized")
|
|
32
|
+
|
|
33
|
+
def get_tool_schema(self) -> dict[str, Any]:
|
|
34
|
+
"""
|
|
35
|
+
Get the MCP tool schema for analyze_code_scale.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
Dictionary containing the tool schema
|
|
39
|
+
"""
|
|
40
|
+
return {
|
|
41
|
+
"type": "object",
|
|
42
|
+
"properties": {
|
|
43
|
+
"file_path": {
|
|
44
|
+
"type": "string",
|
|
45
|
+
"description": "Path to the code file to analyze",
|
|
46
|
+
},
|
|
47
|
+
"language": {
|
|
48
|
+
"type": "string",
|
|
49
|
+
"description": "Programming language (optional, auto-detected if not specified)",
|
|
50
|
+
},
|
|
51
|
+
"include_complexity": {
|
|
52
|
+
"type": "boolean",
|
|
53
|
+
"description": "Include complexity metrics in the analysis",
|
|
54
|
+
"default": True,
|
|
55
|
+
},
|
|
56
|
+
"include_details": {
|
|
57
|
+
"type": "boolean",
|
|
58
|
+
"description": "Include detailed element information",
|
|
59
|
+
"default": False,
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
"required": ["file_path"],
|
|
63
|
+
"additionalProperties": False,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async def execute(self, arguments: dict[str, Any]) -> dict[str, Any]:
|
|
67
|
+
"""
|
|
68
|
+
Execute the analyze_code_scale tool with CLI-compatible output.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
arguments: Tool arguments containing file_path and optional parameters
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
Dictionary containing analysis results in CLI-compatible format
|
|
75
|
+
|
|
76
|
+
Raises:
|
|
77
|
+
ValueError: If required arguments are missing or invalid
|
|
78
|
+
FileNotFoundError: If the specified file doesn't exist
|
|
79
|
+
"""
|
|
80
|
+
# Validate required arguments
|
|
81
|
+
if "file_path" not in arguments:
|
|
82
|
+
raise ValueError("file_path is required")
|
|
83
|
+
|
|
84
|
+
file_path = arguments["file_path"]
|
|
85
|
+
language = arguments.get("language")
|
|
86
|
+
# include_complexity = arguments.get("include_complexity", True) # Not used currently
|
|
87
|
+
# include_details = arguments.get("include_details", False) # Not used currently
|
|
88
|
+
|
|
89
|
+
# Validate file exists
|
|
90
|
+
if not Path(file_path).exists():
|
|
91
|
+
raise FileNotFoundError(f"File not found: {file_path}")
|
|
92
|
+
|
|
93
|
+
# Detect language if not specified
|
|
94
|
+
if not language:
|
|
95
|
+
language = detect_language_from_file(file_path)
|
|
96
|
+
if language == "unknown":
|
|
97
|
+
raise ValueError(f"Could not detect language for file: {file_path}")
|
|
98
|
+
|
|
99
|
+
logger.info(f"Analyzing code scale for {file_path} (language: {language})")
|
|
100
|
+
|
|
101
|
+
try:
|
|
102
|
+
start_time = time.time()
|
|
103
|
+
|
|
104
|
+
# Use AdvancedAnalyzer for comprehensive analysis
|
|
105
|
+
analysis_result = await self.analysis_engine.analyze_file(file_path)
|
|
106
|
+
|
|
107
|
+
if analysis_result is None:
|
|
108
|
+
# Return CLI-compatible error format
|
|
109
|
+
return {
|
|
110
|
+
"file_path": file_path,
|
|
111
|
+
"success": False,
|
|
112
|
+
"package_name": None,
|
|
113
|
+
"element_counts": {
|
|
114
|
+
"imports": 0,
|
|
115
|
+
"classes": 0,
|
|
116
|
+
"methods": 0,
|
|
117
|
+
"fields": 0,
|
|
118
|
+
"annotations": 0,
|
|
119
|
+
},
|
|
120
|
+
"analysis_time_ms": round((time.time() - start_time) * 1000, 2),
|
|
121
|
+
"error_message": f"Failed to analyze file: {file_path}",
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
# Calculate analysis time
|
|
125
|
+
analysis_time_ms = round((time.time() - start_time) * 1000, 2)
|
|
126
|
+
|
|
127
|
+
# Build CLI-compatible result structure (exact match with CLI --advanced --statistics)
|
|
128
|
+
result = {
|
|
129
|
+
"file_path": file_path,
|
|
130
|
+
"success": True,
|
|
131
|
+
"package_name": (
|
|
132
|
+
analysis_result.package.name if analysis_result.package else None
|
|
133
|
+
),
|
|
134
|
+
"element_counts": {
|
|
135
|
+
"imports": len(analysis_result.imports),
|
|
136
|
+
"classes": len(analysis_result.classes),
|
|
137
|
+
"methods": len(analysis_result.methods),
|
|
138
|
+
"fields": len(analysis_result.fields),
|
|
139
|
+
"annotations": len(getattr(analysis_result, "annotations", [])),
|
|
140
|
+
},
|
|
141
|
+
"analysis_time_ms": analysis_time_ms,
|
|
142
|
+
"error_message": None,
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
logger.info(
|
|
146
|
+
f"Successfully analyzed {file_path}: {len(analysis_result.classes)} classes, "
|
|
147
|
+
f"{len(analysis_result.methods)} methods, {analysis_time_ms}ms"
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
return result
|
|
151
|
+
|
|
152
|
+
except Exception as e:
|
|
153
|
+
logger.error(f"Error analyzing {file_path}: {e}")
|
|
154
|
+
# Return CLI-compatible error format
|
|
155
|
+
return {
|
|
156
|
+
"file_path": file_path,
|
|
157
|
+
"success": False,
|
|
158
|
+
"package_name": None,
|
|
159
|
+
"element_counts": {
|
|
160
|
+
"imports": 0,
|
|
161
|
+
"classes": 0,
|
|
162
|
+
"methods": 0,
|
|
163
|
+
"fields": 0,
|
|
164
|
+
"annotations": 0,
|
|
165
|
+
},
|
|
166
|
+
"analysis_time_ms": 0.0,
|
|
167
|
+
"error_message": str(e),
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
def validate_arguments(self, arguments: dict[str, Any]) -> bool:
|
|
171
|
+
"""
|
|
172
|
+
Validate tool arguments against the schema.
|
|
173
|
+
|
|
174
|
+
Args:
|
|
175
|
+
arguments: Arguments to validate
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
True if arguments are valid
|
|
179
|
+
|
|
180
|
+
Raises:
|
|
181
|
+
ValueError: If arguments are invalid
|
|
182
|
+
"""
|
|
183
|
+
schema = self.get_tool_schema()
|
|
184
|
+
required_fields = schema.get("required", [])
|
|
185
|
+
|
|
186
|
+
# Check required fields
|
|
187
|
+
for field in required_fields:
|
|
188
|
+
if field not in arguments:
|
|
189
|
+
raise ValueError(f"Required field '{field}' is missing")
|
|
190
|
+
|
|
191
|
+
# Validate file_path
|
|
192
|
+
if "file_path" in arguments:
|
|
193
|
+
file_path = arguments["file_path"]
|
|
194
|
+
if not isinstance(file_path, str):
|
|
195
|
+
raise ValueError("file_path must be a string")
|
|
196
|
+
if not file_path.strip():
|
|
197
|
+
raise ValueError("file_path cannot be empty")
|
|
198
|
+
|
|
199
|
+
# Validate optional fields
|
|
200
|
+
if "language" in arguments:
|
|
201
|
+
language = arguments["language"]
|
|
202
|
+
if not isinstance(language, str):
|
|
203
|
+
raise ValueError("language must be a string")
|
|
204
|
+
|
|
205
|
+
if "include_complexity" in arguments:
|
|
206
|
+
include_complexity = arguments["include_complexity"]
|
|
207
|
+
if not isinstance(include_complexity, bool):
|
|
208
|
+
raise ValueError("include_complexity must be a boolean")
|
|
209
|
+
|
|
210
|
+
if "include_details" in arguments:
|
|
211
|
+
include_details = arguments["include_details"]
|
|
212
|
+
if not isinstance(include_details, bool):
|
|
213
|
+
raise ValueError("include_details must be a boolean")
|
|
214
|
+
|
|
215
|
+
return True
|
|
216
|
+
|
|
217
|
+
def get_tool_definition(self) -> Any:
|
|
218
|
+
"""
|
|
219
|
+
Get the MCP tool definition for analyze_code_scale.
|
|
220
|
+
|
|
221
|
+
Returns:
|
|
222
|
+
Tool definition object compatible with MCP server
|
|
223
|
+
"""
|
|
224
|
+
try:
|
|
225
|
+
from mcp.types import Tool
|
|
226
|
+
|
|
227
|
+
return Tool(
|
|
228
|
+
name="analyze_code_scale",
|
|
229
|
+
description="Analyze code scale, complexity, and structure metrics with CLI-compatible output format",
|
|
230
|
+
inputSchema=self.get_tool_schema(),
|
|
231
|
+
)
|
|
232
|
+
except ImportError:
|
|
233
|
+
# Fallback for when MCP is not available
|
|
234
|
+
return {
|
|
235
|
+
"name": "analyze_code_scale",
|
|
236
|
+
"description": "Analyze code scale, complexity, and structure metrics with CLI-compatible output format",
|
|
237
|
+
"inputSchema": self.get_tool_schema(),
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
# Tool instance for easy access
|
|
242
|
+
analyze_scale_tool_cli_compatible = AnalyzeScaleToolCLICompatible()
|
|
@@ -1,55 +1,54 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
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
|
-
...
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Base Tool Protocol for MCP Tools
|
|
4
|
+
|
|
5
|
+
This module defines the protocol that all MCP tools must implement
|
|
6
|
+
to ensure type safety and consistency.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from typing import Any, Protocol
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class MCPTool(Protocol):
|
|
13
|
+
"""
|
|
14
|
+
Protocol for MCP tools.
|
|
15
|
+
|
|
16
|
+
All MCP tools must implement this protocol to ensure they have
|
|
17
|
+
the required methods for integration with the MCP server.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def get_tool_definition(self) -> Any:
|
|
21
|
+
"""
|
|
22
|
+
Get the MCP tool definition.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
Tool definition object compatible with MCP server
|
|
26
|
+
"""
|
|
27
|
+
...
|
|
28
|
+
|
|
29
|
+
async def execute(self, arguments: dict[str, Any]) -> dict[str, Any]:
|
|
30
|
+
"""
|
|
31
|
+
Execute the tool with the given arguments.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
arguments: Tool arguments
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
Dictionary containing execution results
|
|
38
|
+
"""
|
|
39
|
+
...
|
|
40
|
+
|
|
41
|
+
def validate_arguments(self, arguments: dict[str, Any]) -> bool:
|
|
42
|
+
"""
|
|
43
|
+
Validate tool arguments.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
arguments: Arguments to validate
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
True if arguments are valid
|
|
50
|
+
|
|
51
|
+
Raises:
|
|
52
|
+
ValueError: If arguments are invalid
|
|
53
|
+
"""
|
|
54
|
+
...
|