File size: 1,123 Bytes
b0f9887
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import importlib.util
import sys
from mcp_server.registry import ToolRegistry

def scan_local_tools(directory: str):
    """
    Dynamically scans a directory for Python files and imports them to trigger 
    tool registration via decorators.
    """
    if not os.path.exists(directory):
        print(f"Discovery: Directory {directory} does not exist.")
        return

    # Add directory to sys.path if not present
    if directory not in sys.path:
        sys.path.append(directory)

    for filename in os.listdir(directory):
        if filename.endswith(".py") and not filename.startswith("__"):
            module_name = filename[:-3]
            module_path = os.path.join(directory, filename)
            
            try:
                spec = importlib.util.spec_from_file_location(module_name, module_path)
                module = importlib.util.module_from_spec(spec)
                spec.loader.exec_module(module)
                print(f"Discovery: Successfully loaded tools from {filename}")
            except Exception as e:
                print(f"Discovery: Error loading {filename}: {e}")