Skip to main content
Glama
avarant

Typesense MCP Server

upsert_document

Insert or update a document in a Typesense collection using its unique ID. Ensures data accuracy and management in MCP-driven search operations.

Instructions

Upserts (creates or updates) a single document in a specific collection.

Args:
    ctx (Context): The MCP context.
    collection_name (str): The name of the collection.
    document (dict): The document data to upsert (must include an 'id' field).

Returns:
    dict | str: The upserted document dictionary or an error message string.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collection_nameYes
documentYes

Implementation Reference

  • main.py:614-647 (handler)
    The main handler function for the 'upsert_document' tool. It is decorated with @mcp.tool(), which registers it as an MCP tool. The function upserts a document into a Typesense collection using the client from the application context, with input validation and comprehensive error handling.
    @mcp.tool()
    async def upsert_document(ctx: Context, collection_name: str, document: dict) -> dict | str:
        """
        Upserts (creates or updates) a single document in a specific collection.
    
        Args:
            ctx (Context): The MCP context.
            collection_name (str): The name of the collection.
            document (dict): The document data to upsert (must include an 'id' field).
    
        Returns:
            dict | str: The upserted document dictionary or an error message string.
        """
        if not collection_name:
            return "Error: collection_name parameter is required."
        if not isinstance(document, dict) or 'id' not in document:
            return "Error: document parameter must be a dictionary and include an 'id' field."
    
        try:
            client: typesense.Client = ctx.request_context.lifespan_context.client
            # NOTE: Assuming upsert is *sync* based on observed pattern
            upserted_doc = client.collections[collection_name].documents.upsert(document)
            return upserted_doc
        except typesense.exceptions.ObjectNotFound:
            return f"Error: Collection '{collection_name}' not found."
        except typesense.exceptions.RequestMalformed as e:
             return f"Error: Malformed upsert document request for collection '{collection_name}'. Check document structure against schema. Details: {e}"
        except typesense.exceptions.TypesenseClientError as e:
            print(f"Error upserting document in '{collection_name}': {e}")
            return f"Error upserting document in '{collection_name}': {e}"
        except Exception as e:
            print(f"An unexpected error occurred while upserting document in '{collection_name}': {e}")
            return f"An unexpected error occurred: {e}"
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It states the action ('upserts') and return type, but lacks details on permissions, error conditions (beyond mentioning error messages), idempotency, or side effects. For a mutation tool with zero annotation coverage, this is insufficient behavioral disclosure.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is front-loaded with the core purpose in the first sentence, followed by structured Args and Returns sections. Every sentence earns its place by providing essential information without redundancy, making it highly efficient.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given 2 parameters with 0% schema coverage, no annotations, and no output schema, the description provides basic purpose and parameter hints but lacks details on behavior, error handling, and full parameter semantics. It's minimally adequate but has clear gaps for a mutation tool in this context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the description must compensate. It adds meaning by explaining that 'document' must include an 'id' field, which is crucial semantic info not in the schema. However, it doesn't clarify the format or constraints for 'collection_name' or other aspects of 'document', leaving gaps.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb ('upserts'), resource ('a single document'), and scope ('in a specific collection'), distinguishing it from siblings like create_document (only creates) and delete_document (deletes). The term 'upserts' is specific and indicates both creation and update functionality.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context by specifying 'creates or updates a single document', suggesting it's for when you want to ensure a document exists with given data. However, it doesn't explicitly state when to use this vs. alternatives like create_document or update_document (if present), nor does it mention prerequisites or exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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/avarant/typesense-mcp-server'

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