List_SharePoint_Documents
Retrieve a comprehensive list of all documents within a specified SharePoint folder using this tool, ensuring efficient document management and access.
Instructions
List all documents in a specified SharePoint folder
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| folder_name | Yes |
Implementation Reference
- src/mcp_sharepoint/tools.py:38-41 (handler)MCP handler function for the 'List_SharePoint_Documents' tool. Registers the tool and delegates execution to the list_documents helper function from resources.py.@mcp.tool(name="List_SharePoint_Documents", description="List all documents in a specified SharePoint folder") async def list_documents_tool(folder_name: str): """List all documents in a specified SharePoint folder""" return list_documents(folder_name)
- Core helper function that lists documents in the specified SharePoint folder by invoking the generic _load_sp_items with item_type='files'.def list_documents(folder_name: str) -> List[Dict[str, Any]]: """List all documents in a specified folder""" logger.info(f"Listing documents in folder: {folder_name}") return _load_sp_items(_get_sp_path(folder_name), "files")
- Generic helper that loads SharePoint folders or files (used for documents with 'files'), retrieves properties like name, url, size, timestamps.def _load_sp_items(path: str, item_type: str) -> List[Dict[str, Any]]: """Generic function to load folders or files from SharePoint""" folder = sp_context.web.get_folder_by_server_relative_url(path) items = getattr(folder, item_type) props = ["ServerRelativeUrl", "Name", "TimeCreated", "TimeLastModified"] + (["Length"] if item_type == "files" else []) sp_context.load(items, props) sp_context.execute_query() return [{ "name": item.name, "url": item.properties.get("ServerRelativeUrl"), **({"size": item.properties.get("Length")} if item_type == "files" else {}), "created": item.properties.get("TimeCreated").isoformat() if item.properties.get("TimeCreated") else None, "modified": item.properties.get("TimeLastModified").isoformat() if item.properties.get("TimeLastModified") else None } for item in items]
- Helper to construct the full SharePoint server-relative path from folder name, prepending the document library.def _get_sp_path(sub_path: Optional[str] = None) -> str: """Create a properly formatted SharePoint path""" return f"{SHP_DOC_LIBRARY}/{sub_path or ''}".rstrip('/')
- src/mcp_sharepoint/tools.py:38-38 (registration)The @mcp.tool decorator that registers 'List_SharePoint_Documents' with MCP, including name and description.@mcp.tool(name="List_SharePoint_Documents", description="List all documents in a specified SharePoint folder")