File size: 943 Bytes
9507c73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)}"