Upload_Document_From_Path
Transfer files from a local path to SharePoint by specifying folder name and file path, optionally renaming the file during upload using the SharePoint MCP Server.
Instructions
Upload a file directly from a file path to SharePoint
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| folder_name | Yes | ||
| new_file_name | No |
Implementation Reference
- src/mcp_sharepoint/tools.py:87-105 (handler)Core handler logic for uploading a document from a local file path to a SharePoint folder. Opens the file in binary mode, determines filename if not provided, uploads via SharePoint context, returns success response.async def upload_document_from_path(folder_name: str, file_path: str, new_file_name: Optional[str] = None): """Upload a file directly from a path without needing to convert to base64 first""" logger.info(f"Uploading document from path {file_path} to folder {folder_name}") try: with open(file_path, "rb") as file: file_content = file.read() if not new_file_name: new_file_name = os.path.basename(file_path) folder = sp_context.web.get_folder_by_server_relative_url(_get_path(folder_name)) uploaded_file = folder.upload_file(new_file_name, file_content) sp_context.execute_query() return _file_success_response(uploaded_file, f"File {new_file_name} uploaded successfully") except Exception as e: logger.error(f"Error uploading file from path: {str(e)}") raise
- src/mcp_sharepoint/tools.py:85-86 (registration)Registers the tool 'Upload_Document_From_Path' with MCP using @mcp.tool decorator including description. Applies error handling decorator. Input schema defined by function parameters: folder_name (str), file_path (str), new_file_name (Optional[str]).@mcp.tool(name="Upload_Document_From_Path", description="Upload a file directly from a file path to SharePoint") @_handle_sp_operation
- src/mcp_sharepoint/tools.py:13-22 (helper)Decorator _handle_sp_operation applied to the tool for standardized error handling, logging, and consistent error response format.def _handle_sp_operation(func): """Decorator for SharePoint operations with error handling""" @wraps(func) async def wrapper(*args, **kwargs): try: return await func(*args, **kwargs) except Exception as e: logger.error(f"Error in {func.__name__}: {str(e)}") return {"success": False, "message": f"Operation failed: {str(e)}"} return wrapper
- src/mcp_sharepoint/tools.py:24-30 (helper)Helper function _file_success_response formats the standardized success response with file name and URL.def _file_success_response(file_obj, message: str) -> Dict[str, Any]: """Standard success response for file operations""" return { "success": True, "message": message, "file": {"name": file_obj.name, "url": file_obj.serverRelativeUrl} }
- src/mcp_sharepoint/tools.py:8-11 (helper)Helper function _get_path constructs the SharePoint server-relative path for the target folder.def _get_path(folder: str = "", file: Optional[str] = None) -> str: """Construct SharePoint path from components""" path = f"{SHP_DOC_LIBRARY}/{folder}".rstrip('/') return f"{path}/{file}" if file else path