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

80
http_server.py Normal file
View File

@@ -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)

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")

BIN
uploads/01.JPG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB