Skip to main content
Glama
Sofias-ai

SharePoint MCP Server

by Sofias-ai

Update_Document

Modify existing documents in a SharePoint directory by specifying folder name, file name, and updated content. Supports Base64 encoding for complex data.

Instructions

Update an existing document in a SharePoint directory

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentYes
file_nameYes
folder_nameYes
is_base64No

Implementation Reference

  • Full handler implementation for the 'Update_Document' tool. Registers the tool with MCP, applies error handling decorator, defines input parameters (folder_name, file_name, content, is_base64), checks file existence, decodes content if base64-encoded, overwrites the file in SharePoint using upload_file, and returns success response with file info.
    @mcp.tool(name="Update_Document", description="Update an existing document in a SharePoint directory")
    @_handle_sp_operation
    async def update_document(folder_name: str, file_name: str, content: str, is_base64: bool = False):
        """Update an existing document in a SharePoint directory"""
        logger.info(f"Updating document {file_name} in folder {folder_name}")
        
        # Check if file exists
        file_path = _get_path(folder_name, file_name)
        file = sp_context.web.get_file_by_server_relative_url(file_path)
        sp_context.load(file, ["Exists", "Name", "ServerRelativeUrl"])
        sp_context.execute_query()
        
        if not file.exists:
            return {"success": False, "message": f"File {file_name} does not exist in folder {folder_name}"}
        
        # Update file using upload method
        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))
        updated_file = folder.upload_file(file_name, file_content)
        sp_context.execute_query()
        
        return _file_success_response(updated_file, f"File {file_name} updated successfully")
  • Explicit registration of the Update_Document tool using the @mcp.tool decorator, specifying the tool name.
    @mcp.tool(name="Update_Document", description="Update an existing document in a SharePoint directory")
  • Helper decorator applied to the Update_Document handler for standardized SharePoint error handling and logging.
    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
  • Utility function used to build SharePoint server-relative paths for the document location.
    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
  • Helper function to format the success response returned by the Update_Document tool.
    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}
        }
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