This commit is contained in:
Xin Wang
2026-03-31 10:28:20 +08:00
commit 645dcd92c0
3 changed files with 131 additions and 0 deletions

51
stdio_server.py Normal file
View File

@@ -0,0 +1,51 @@
from pathlib import Path
from fastmcp import FastMCP
ROOT_DIR = Path(__file__).resolve().parent
UPLOAD_DIR = ROOT_DIR / "uploads"
UPLOAD_DIR.mkdir(exist_ok=True)
mcp = FastMCP("FilePersistenceSTDIO")
@mcp.tool
def health() -> dict:
"""Return basic server health information."""
return {
"status": "ok",
"server": "FilePersistenceSTDIO",
"transport": "stdio",
"uploads_dir": str(UPLOAD_DIR),
}
@mcp.tool
def list_uploaded_files() -> dict:
"""List files currently stored in the uploads directory."""
files = sorted(path.name for path in UPLOAD_DIR.iterdir() if path.is_file())
return {
"count": len(files),
"files": files,
}
@mcp.tool
def file_info(filename: str) -> dict:
"""Return metadata for one uploaded file."""
path = (UPLOAD_DIR / filename).resolve()
if path.parent != UPLOAD_DIR.resolve() or not path.exists() or not path.is_file():
raise FileNotFoundError(f"File not found: {filename}")
stat = path.stat()
return {
"filename": path.name,
"size_bytes": stat.st_size,
"absolute_path": str(path),
}
if __name__ == "__main__":
mcp.run(transport="stdio")