refactor(scripts): Add strict type hints to utility scripts
This commit is contained in:
@@ -2,14 +2,14 @@ import os
|
||||
import re
|
||||
|
||||
with open('mcp_client.py', 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
content: str = f.read()
|
||||
|
||||
# 1. Add import os if not there
|
||||
if 'import os' not in content:
|
||||
content = content.replace('import summarize', 'import os\nimport summarize')
|
||||
content: str = content.replace('import summarize', 'import os\nimport summarize')
|
||||
|
||||
# 2. Add the functions before "# ------------------------------------------------------------------ web tools"
|
||||
functions_code = r'''
|
||||
functions_code: str = r'''
|
||||
def py_find_usages(path: str, name: str) -> str:
|
||||
"""Finds exact string matches of a symbol in a given file or directory."""
|
||||
p, err = _resolve_and_check(path)
|
||||
@@ -179,17 +179,17 @@ def get_tree(path: str, max_depth: int = 2) -> str:
|
||||
|
||||
# ------------------------------------------------------------------ web tools'''
|
||||
|
||||
content = content.replace('# ------------------------------------------------------------------ web tools', functions_code)
|
||||
content: str = content.replace('# ------------------------------------------------------------------ web tools', functions_code)
|
||||
|
||||
# 3. Update TOOL_NAMES
|
||||
old_tool_names_match = re.search(r'TOOL_NAMES\s*=\s*\{([^}]*)\}', content)
|
||||
old_tool_names_match: re.Match | None = re.search(r'TOOL_NAMES\s*=\s*\{([^}]*)\}', content)
|
||||
if old_tool_names_match:
|
||||
old_names = old_tool_names_match.group(1)
|
||||
new_names = old_names + ', "py_find_usages", "py_get_imports", "py_check_syntax", "py_get_hierarchy", "py_get_docstring", "get_tree"'
|
||||
content = content.replace(old_tool_names_match.group(0), f'TOOL_NAMES = {{{new_names}}}')
|
||||
old_names: str = old_tool_names_match.group(1)
|
||||
new_names: str = old_names + ', "py_find_usages", "py_get_imports", "py_check_syntax", "py_get_hierarchy", "py_get_docstring", "get_tree"'
|
||||
content: str = content.replace(old_tool_names_match.group(0), f'TOOL_NAMES = {{{new_names}}}')
|
||||
|
||||
# 4. Update dispatch
|
||||
dispatch_additions = r'''
|
||||
dispatch_additions: str = r'''
|
||||
if tool_name == "py_find_usages":
|
||||
return py_find_usages(tool_input.get("path", ""), tool_input.get("name", ""))
|
||||
if tool_name == "py_get_imports":
|
||||
@@ -204,10 +204,11 @@ dispatch_additions = r'''
|
||||
return get_tree(tool_input.get("path", ""), tool_input.get("max_depth", 2))
|
||||
return f"ERROR: unknown MCP tool '{tool_name}'"
|
||||
'''
|
||||
content = re.sub(r' return f"ERROR: unknown MCP tool \'{tool_name}\'"', dispatch_additions.strip(), content)
|
||||
content: str = re.sub(
|
||||
r' return f"ERROR: unknown MCP tool \'{tool_name}\'"', dispatch_additions.strip(), content)
|
||||
|
||||
# 5. Update MCP_TOOL_SPECS
|
||||
mcp_tool_specs_addition = r'''
|
||||
mcp_tool_specs_addition: str = r'''
|
||||
{
|
||||
"name": "py_find_usages",
|
||||
"description": "Finds exact string matches of a symbol in a given file or directory.",
|
||||
@@ -281,7 +282,8 @@ mcp_tool_specs_addition = r'''
|
||||
]
|
||||
'''
|
||||
|
||||
content = re.sub(r'\]\s*$', mcp_tool_specs_addition.strip(), content)
|
||||
content: str = re.sub(
|
||||
r'\]\s*$', mcp_tool_specs_addition.strip(), content)
|
||||
|
||||
with open('mcp_client.py', 'w', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
|
||||
Reference in New Issue
Block a user