Skip to main content
Glama

add_document

Add text documents to a knowledge index with automatic embedding using the index's embedding model.

Instructions

Add a text document to a knowledge index. The text is embedded automatically using the index's embedding model.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
index_nameYes
textYes
metadataNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • Core handler that adds a document to an index. Retrieves the index by name, computes an embedding via Ollama, creates a Document dataclass with UUID, text, metadata, and embedding, appends it to the index, persists to disk, and returns status.
    async def add_document(
        index_name: str,
        text: str,
        metadata: dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        idx = _indexes.get(index_name)
        if idx is None:
            raise ValueError(f"Index '{index_name}' not found. Create it first.")
        embedding = await oc.embeddings(idx.embed_model, text)
        doc = Document(
            id=str(uuid.uuid4()),
            text=text,
            metadata=metadata or {},
            embedding=embedding,
        )
        idx.documents.append(doc)
        _save()
        return {"status": "added", "id": doc.id, "index": index_name}
  • Document dataclass schema defining the structure of a stored document (id, text, metadata, embedding).
    @dataclass
    class Document:
        id: str
        text: str
        metadata: dict[str, Any]
        embedding: list[float]
  • MCP tool registration using @mcp.tool decorator with name='add_document'. Defines parameters (index_name, text, metadata) and delegates to kn.add_document().
    @mcp.tool(
        name="add_document",
        description=(
            "Add a text document to a knowledge index. "
            "The text is embedded automatically using the index's embedding model."
        ),
    )
    async def add_document(
        index_name: str,
        text: str,
        metadata: dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        """
        Args:
            index_name: The target index name (must be created first).
            text: Document text to store and embed.
            metadata: Optional key-value metadata to attach to the document.
        """
        return await kn.add_document(
            index_name=index_name,
            text=text,
            metadata=metadata,
        )
  • Persistence helpers: _save() serializes all indexes (including documents with embeddings) to JSON, _load() deserializes on startup.
    def _save() -> None:
        data = {}
        for idx_name, idx in _indexes.items():
            data[idx_name] = {
                "embed_model": idx.embed_model,
                "documents": [
                    {
                        "id": d.id,
                        "text": d.text,
                        "metadata": d.metadata,
                        "embedding": d.embedding,
                    }
                    for d in idx.documents
                ],
            }
        STORE_PATH.write_text(json.dumps(data))
    
    
    def _load() -> None:
        if not STORE_PATH.exists():
            return
        data = json.loads(STORE_PATH.read_text())
        for idx_name, idx_data in data.items():
            docs = [
                Document(
                    id=d["id"],
                    text=d["text"],
                    metadata=d["metadata"],
                    embedding=d["embedding"],
                )
                for d in idx_data["documents"]
            ]
            _indexes[idx_name] = Index(
                name=idx_name,
                embed_model=idx_data["embed_model"],
                documents=docs,
            )
Behavior2/5

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

The description mentions automatic embedding, but does not disclose whether the index must already exist, idempotency, return value, or error conditions. No annotations are provided to compensate.

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

Conciseness3/5

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

The description is very short and front-loaded with the key action, but it omits important details that would make it more useful.

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

Completeness2/5

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

With 3 parameters, no annotations, and an output schema not described, the description is incomplete. It lacks prerequisites, return format, and error handling.

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

Parameters1/5

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

The description provides no additional information about the parameters beyond the input schema, which has 0% coverage.

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

Purpose4/5

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

The description clearly states the verb 'Add' and the resource 'text document to a knowledge index', and mentions automatic embedding. It implicitly distinguishes from sibling tools like query_knowledge and create_index.

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

Usage Guidelines2/5

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

No explicit guidance on when to use this tool versus alternatives, such as requiring the index to exist or when to use query_knowledge instead.

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/deadSwank001/Nexus-MCP'

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