Skip to main content
Glama

zotero_get_collections

Retrieve all collections from your Zotero library to organize research materials efficiently, with an optional limit to refine results. Integrated with Zotero MCP for seamless access and management via AI assistants.

Instructions

List all collections in your Zotero library.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
limitNo

Implementation Reference

  • Handler function for zotero_get_collections tool. Registers the tool with MCP and implements logic to fetch collections from Zotero API, build hierarchy, and format as Markdown.
    @mcp.tool( name="zotero_get_collections", description="List all collections in your Zotero library." ) def get_collections( limit: Optional[Union[int, str]] = None, *, ctx: Context ) -> str: """ List all collections in your Zotero library. Args: limit: Maximum number of collections to return ctx: MCP context Returns: Markdown-formatted list of collections """ try: ctx.info("Fetching collections") zot = get_zotero_client() if isinstance(limit, str): limit = int(limit) collections = zot.collections(limit=limit) # Always return the header, even if empty output = ["# Zotero Collections", ""] if not collections: output.append("No collections found in your Zotero library.") return "\n".join(output) # Create a mapping of collection IDs to their data collection_map = {c["key"]: c for c in collections} # Create a mapping of parent to child collections # Only add entries for collections that actually exist hierarchy = {} for coll in collections: parent_key = coll["data"].get("parentCollection") # Handle various representations of "no parent" if parent_key in ["", None] or not parent_key: parent_key = None # Normalize to None if parent_key not in hierarchy: hierarchy[parent_key] = [] hierarchy[parent_key].append(coll["key"]) # Function to recursively format collections def format_collection(key, level=0): if key not in collection_map: return [] coll = collection_map[key] name = coll["data"].get("name", "Unnamed Collection") # Create indentation for hierarchy indent = " " * level lines = [f"{indent}- **{name}** (Key: {key})"] # Add children if they exist child_keys = hierarchy.get(key, []) for child_key in sorted(child_keys): # Sort for consistent output lines.extend(format_collection(child_key, level + 1)) return lines # Start with top-level collections (those with None as parent) top_level_keys = hierarchy.get(None, []) if not top_level_keys: # If no clear hierarchy, just list all collections output.append("Collections (flat list):") for coll in sorted(collections, key=lambda x: x["data"].get("name", "")): name = coll["data"].get("name", "Unnamed Collection") key = coll["key"] output.append(f"- **{name}** (Key: {key})") else: # Display hierarchical structure for key in sorted(top_level_keys): output.extend(format_collection(key)) return "\n".join(output) except Exception as e: ctx.error(f"Error fetching collections: {str(e)}") error_msg = f"Error fetching collections: {str(e)}" return f"# Zotero Collections\n\n{error_msg}"
  • MCP tool registration decorator for zotero_get_collections.
    @mcp.tool( name="zotero_get_collections", description="List all collections in your Zotero library." )

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/54yyyu/zotero-mcp'

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