commit 645dcd92c00aab54c0e9d44e6374bb408e93662e Author: Xin Wang Date: Tue Mar 31 10:28:20 2026 +0800 init diff --git a/http_server.py b/http_server.py new file mode 100644 index 0000000..bc5cefd --- /dev/null +++ b/http_server.py @@ -0,0 +1,80 @@ +from pathlib import Path + +from fastmcp import FastMCP +from starlette.middleware import Middleware +from starlette.middleware.cors import CORSMiddleware +from starlette.requests import Request +from starlette.responses import JSONResponse +import uvicorn + + +ROOT_DIR = Path(__file__).resolve().parent +UPLOAD_DIR = ROOT_DIR / "uploads" +UPLOAD_DIR.mkdir(exist_ok=True) + + +# Streamable HTTP MCP server. +mcp = FastMCP("FilePersistenceMCP") + + +@mcp.tool +def health() -> dict: + """Return basic server health information.""" + return { + "status": "ok", + "server": "FilePersistenceMCP", + "transport": "streamable-http", + "uploads_dir": str(UPLOAD_DIR), + } + + +@mcp.tool +def list_uploaded_files() -> dict: + """List files currently stored in the uploads directory.""" + files = sorted(p.name for p in UPLOAD_DIR.iterdir() if p.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), + } + + +@mcp.custom_route("/health", methods=["GET"]) +async def http_health(_: Request) -> JSONResponse: + return JSONResponse( + { + "status": "ok", + "server": "FilePersistenceMCP", + "transport": "streamable-http", + } + ) + + +if __name__ == "__main__": + app = mcp.http_app( + path="/mcp", + middleware=[ + Middleware( + CORSMiddleware, + allow_origins=["*"], + allow_methods=["*"], + allow_headers=["*"], + ) + ], + stateless_http=True, + ) + uvicorn.run(app, host="0.0.0.0", port=8001) diff --git a/stdio_server.py b/stdio_server.py new file mode 100644 index 0000000..a561f92 --- /dev/null +++ b/stdio_server.py @@ -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") diff --git a/uploads/01.JPG b/uploads/01.JPG new file mode 100644 index 0000000..95c2fcb Binary files /dev/null and b/uploads/01.JPG differ