Spaces:
Sleeping
Sleeping
# 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.""" | |
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)} | |
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)} | |