INNER CODE UNIT · Python
write_file
existence-master/Sentient · src/server/mcp_hub/file_management/main.py:95
async def write_file(ctx: Context, filename: str, content: str) -> Dict[str, Any]:
"""
Writes or overwrites a file in the temporary storage with the provided `content`.
"""
logger.info(f"Executing tool: write_file with filename='{filename}'")
try:
user_id = auth.get_user_id_from_context(ctx)
safe_user_id = "".join(c for c in user_id if c.isalnum() or c in ('-', '_'))
# Prepend user_id to filename to create the relative path
relative_path = os.path.join(safe_user_id, filename)
safe_path = _get_safe_filepath(relative_path)
# Ensure the directory exists before writing
safe_path.parent.mkdir(parents=True, exist_ok=True)
with safe_path.open("w", encoding="utf-8") as f:
f.write(content)
return {"status": "success", "result": f"File '{filename}' written successfully."}
except Exception as e: