File size: 2,085 Bytes
2ef9bf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# File explorer tools
"""
MCP tools for exploring files and directories.
"""
import os
from mcp.server.fastmcp import FastMCP


def register_file_explorer_tools(mcp: FastMCP):
    """Register file explorer tools with the MCP server."""
    
    @mcp.tool()
    def list_directory(path: str) -> list:
        """
        List contents of a directory.
        
        Args:
            path: Directory path to list
            
        Returns:
            List of files and directories
        """
        if not os.path.exists(path):
            return {"error": f"Path {path} does not exist"}
            
        if not os.path.isdir(path):
            return {"error": f"Path {path} is not a directory"}
            
        try:
            contents = os.listdir(path)
            result = []
            
            for item in contents:
                item_path = os.path.join(path, item)
                item_type = "directory" if os.path.isdir(item_path) else "file"
                
                result.append({
                    "name": item,
                    "type": item_type,
                    "path": item_path
                })
                
            return result
        except Exception as e:
            return {"error": str(e)}
    
    @mcp.tool()
    def file_info(path: str) -> dict:
        """
        Get information about a file.
        
        Args:
            path: Path to the file
            
        Returns:
            Information about the file
        """
        if not os.path.exists(path):
            return {"error": f"Path {path} does not exist"}
            
        try:
            stat_info = os.stat(path)
            
            return {
                "name": os.path.basename(path),
                "path": os.path.abspath(path),
                "size": stat_info.st_size,
                "last_modified": stat_info.st_mtime,
                "is_directory": os.path.isdir(path),
                "is_file": os.path.isfile(path)
            }
        except Exception as e:
            return {"error": str(e)}