Upload_Document
Upload files to a specified directory on SharePoint, supporting both text and Base64 content. Streamline document management and integration with SharePoint workflows.
Instructions
Upload a new file to a SharePoint directory
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | ||
| file_name | Yes | ||
| folder_name | Yes | ||
| is_base64 | No |
Implementation Reference
- src/mcp_sharepoint/tools.py:71-83 (handler)The main handler function for the 'Upload_Document' tool. It is decorated with @mcp.tool for registration and @_handle_sp_operation for error handling. The function uploads the provided content (decoded from base64 if specified) as a new file to the specified SharePoint folder using the SharePoint context.@mcp.tool(name="Upload_Document", description="Upload a new file to a SharePoint directory") @_handle_sp_operation async def upload_document(folder_name: str, file_name: str, content: str, is_base64: bool = False): """Upload a new file to a directory""" logger.info(f"Uploading document {file_name} to folder {folder_name}") # Convert content and upload file_content = base64.b64decode(content) if is_base64 else content.encode('utf-8') folder = sp_context.web.get_folder_by_server_relative_url(_get_path(folder_name)) uploaded_file = folder.upload_file(file_name, file_content) sp_context.execute_query() return _file_success_response(uploaded_file, f"File {file_name} uploaded successfully")
- src/mcp_sharepoint/tools.py:13-22 (helper)Helper decorator applied to the upload handler for standardized error handling in SharePoint operations.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 used by the handler to format successful upload responses.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 used in the handler to construct the full SharePoint server-relative path.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
- src/mcp_sharepoint/server.py:10-10 (registration)Import of the tools module in the server entrypoint, which triggers registration of all @mcp.tool decorated functions when the server starts.from . import resources, tools