Skip to main content
Glama

upsert

Create or update database records in SurrealDB. Insert new records when they don't exist, or merge data with existing records when they do.

Instructions

Upsert a record: create if it doesn't exist, merge/update if it does.

This tool is perfect when you want to ensure a record exists with specific data, regardless of whether it already exists. It will:

  • Create a new record with the specified ID if it doesn't exist

  • Merge the provided data into the existing record if it does exist

  • Always succeed (unless there's a database error)

Args: thing: The full record ID in format "table:id" (e.g., "user:john", "settings:global") data: The data for the record. If record exists, this will be merged with existing data namespace: Optional SurrealDB namespace override. If not provided, uses SURREAL_NAMESPACE env var. database: Optional SurrealDB database override. If not provided, uses SURREAL_DATABASE env var.

Returns: A dictionary containing: - success: Boolean indicating if upsert was successful - data: The record after upserting - created: Boolean indicating if a new record was created (vs updated) - error: Error message if upsert failed (only present on failure)

Examples: >>> await upsert("user:john", {"name": "John Doe", "email": "john@example.com"}) {"success": true, "data": {"id": "user:john", "name": "John Doe", ...}, "created": true}

>>> await upsert("user:john", {"email": "newemail@example.com"})  # Update existing
{"success": true, "data": {"id": "user:john", "name": "John Doe", "email": "newemail@example.com", ...}, "created": false}

>>> await upsert("settings:global", {"theme": "dark", "language": "en"})
{"success": true, "data": {"id": "settings:global", "theme": "dark", "language": "en"}, "created": true}

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
thingYes
dataYes
namespaceNo
databaseNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The primary handler for the 'upsert' tool. It validates input, resolves database context, checks record existence, invokes repo_upsert helper, and returns structured response including whether created or updated.
    @mcp.tool()
    async def upsert(
        thing: str,
        data: Dict[str, Any],
        namespace: Optional[str] = None,
        database: Optional[str] = None,
    ) -> Dict[str, Any]:
        """
        Upsert a record: create if it doesn't exist, merge/update if it does.
    
        This tool is perfect when you want to ensure a record exists with specific data, regardless
        of whether it already exists. It will:
        - Create a new record with the specified ID if it doesn't exist
        - Merge the provided data into the existing record if it does exist
        - Always succeed (unless there's a database error)
    
        Args:
            thing: The full record ID in format "table:id" (e.g., "user:john", "settings:global")
            data: The data for the record. If record exists, this will be merged with existing data
            namespace: Optional SurrealDB namespace override. If not provided, uses SURREAL_NAMESPACE env var.
            database: Optional SurrealDB database override. If not provided, uses SURREAL_DATABASE env var.
    
        Returns:
            A dictionary containing:
            - success: Boolean indicating if upsert was successful
            - data: The record after upserting
            - created: Boolean indicating if a new record was created (vs updated)
            - error: Error message if upsert failed (only present on failure)
    
        Examples:
            >>> await upsert("user:john", {"name": "John Doe", "email": "john@example.com"})
            {"success": true, "data": {"id": "user:john", "name": "John Doe", ...}, "created": true}
    
            >>> await upsert("user:john", {"email": "newemail@example.com"})  # Update existing
            {"success": true, "data": {"id": "user:john", "name": "John Doe", "email": "newemail@example.com", ...}, "created": false}
    
            >>> await upsert("settings:global", {"theme": "dark", "language": "en"})
            {"success": true, "data": {"id": "settings:global", "theme": "dark", "language": "en"}, "created": true}
        """
        try:
            ns, db = resolve_namespace_database(namespace, database)
    
            # Validate thing format
            if ":" not in thing:
                raise ValueError(f"Invalid record ID format: {thing}. Must be 'table:id'")
    
            logger.info(f"Upserting record {thing}")
    
            # Check if record exists
            try:
                existing = await repo_query(f"SELECT * FROM {thing}", namespace=ns, database=db)
                created = not existing or len(existing) == 0
            except Exception:
                created = True
    
            # Extract table name for repo_upsert
            table = thing.split(":", 1)[0]
    
            # Perform upsert - pass full record ID
            result = await repo_upsert(
                table=table, id=thing, data=data, add_timestamp=True, namespace=ns, database=db
            )
    
            # Get the first result
            upserted_record = result[0] if result else {}
    
            return {
                "success": True,
                "data": upserted_record,
                "created": created
            }
        except Exception as e:
            logger.error(f"Upsert failed for {thing}: {str(e)}")
            raise Exception(f"Failed to upsert {thing}: {str(e)}")
  • Core database helper function that constructs and executes the SurrealQL UPSERT MERGE query, handling timestamps and parameters.
    async def repo_upsert(
        table: str,
        id: Optional[str],
        data: Dict[str, Any],
        add_timestamp: bool = False,
        namespace: Optional[str] = None,
        database: Optional[str] = None,
    ) -> List[Dict[str, Any]]:
        """Create or update a record in the specified table.
    
        Args:
            table: The table name
            id: Optional record ID (if provided, upserts that specific record)
            data: The record data to upsert
            add_timestamp: Whether to add/update the 'updated' timestamp
            namespace: Optional namespace override (uses env var if not provided)
            database: Optional database override (uses env var if not provided)
    
        Returns:
            The upserted record(s)
        """
        data.pop("id", None)
        if add_timestamp:
            data["updated"] = datetime.now(timezone.utc)
        query = f"UPSERT {id if id else table} MERGE $data;"
        return await repo_query(query, {"data": data}, namespace=namespace, database=database)
  • FastMCP decorator that registers the upsert function as an MCP tool, defining its schema from type hints and docstring.
    @mcp.tool()
Behavior4/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It effectively describes the tool's behavior: the conditional create/merge logic, success conditions ('always succeed unless there's a database error'), and return structure. It could improve by mentioning potential side effects or performance implications, but covers core behavior well.

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

Conciseness4/5

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

The description is well-structured with clear sections (purpose, behavior, args, returns, examples) and uses bullet points effectively. It's appropriately detailed for a complex tool but could be slightly more concise in the examples section, which includes three detailed examples.

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

Completeness5/5

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

Given the tool's complexity (conditional create/update logic), 4 parameters with 0% schema coverage, and no annotations, the description provides comprehensive coverage. It explains purpose, usage, behavior, parameters, and return values in detail. The presence of an output schema reduces the need to fully document returns, but the description still provides helpful context.

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

Parameters5/5

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

With 0% schema description coverage, the description fully compensates by providing detailed semantic information for all parameters. It explains the format and purpose of 'thing' (record ID format), the behavior of 'data' (merged if record exists), and the optional nature and defaults for 'namespace' and 'database' parameters.

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 tool's purpose with specific verbs ('create if it doesn't exist, merge/update if it does') and identifies the resource ('record'). It distinguishes this from siblings like 'create', 'update', 'merge', and 'patch' by explaining the conditional logic that combines their functionalities.

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

Usage Guidelines5/5

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

The description explicitly states when to use this tool ('perfect when you want to ensure a record exists with specific data, regardless of whether it already exists'). It distinguishes it from alternatives by explaining its unique upsert behavior, which differs from separate create or update operations available among siblings.

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

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/lfnovo/surreal-mcp'

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