import os from mcp.server.fastmcp import FastMCP # Initialize a secure local file server mcp = FastMCP("local-files") SHARE_DIR = "/projects" @mcp.tool() def list_files() -> str: """List all files available in the shared folder.""" try: files = os.listdir(SHARE_DIR) return "\n".join(files) if files else "The directory is empty." except Exception as e: return f"Error listing files: {str(e)}" @mcp.tool() def read_file(filename: str) -> str: """Read the full text content of a specific file inside the shared folder.""" try: # Path validation: strips directories to block path traversal hacks (e.g., ../../../etc/passwd) safe_name = os.path.basename(filename) target_path = os.path.join(SHARE_DIR, safe_name) with open(target_path, "r", encoding="utf-8") as f: return f.read() except Exception as e: return f"Error reading file: {str(e)}"