Skip to main content
Glama
Sofias-ai

SharePoint MCP Server

by Sofias-ai

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
NameRequiredDescriptionDefault
file_pathYes
folder_nameYes
new_file_nameNo

Implementation Reference

  • 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
  • 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
  • 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
  • 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}
        }
  • 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
Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Sofias-ai/mcp-sharepoint'

If you have feedback or need assistance with the MCP directory API, please join our Discord server