Build your knowledge base

Document sources and RAG

Configure the built-in files source, a plugin package or an MCP endpoint, and aggregate documents into the knowledge layer.

On this page

Who this is for: operators who bring documents, tickets and web pages into the graph.

Not everything worth retrieving lives in code. This page is the reference for contextlake kb ingest and the [[sources]] block: which source types are built in, what each one extracts, what it refuses to guess at, and every key you can set.

Source type Ships with Reads
files core, no extra install a folder of files on disk
mcp core resources, and optionally a search tool, on an MCP server
anything heavier a plugin package whatever the plugin's iter_documents() yields

Aggregating documents#

Not everything lives in code. contextlake kb ingest pulls external documents into the same knowledge layer, they become kind="document" graph nodes and, when embeddings are on, their bodies are embedded so semantic search spans code and docs together:

contextlake kb ingest --path ./docs        # zero-config: ingest a folder of files
contextlake kb ingest --path ./docs --for-repo group/app   # …and link it to that repo's code
contextlake kb ingest --path https://example.com/changelog --source-type web   # one web page

--path sets the path of a files source. For --source-type web or api it sets the url. For --source-type mcp it sets the url when the value starts with http:// or https://. It sets one option only, so it cannot drive a graphql source, which needs a query as well. Use a [[sources]] entry in kb.toml for that. When --path names a target and no request goes out for it, the source's line reads cli: FAILED, nothing was sent for '...' with the reason, and the run exits 1.

Every --path run writes the same set of documents (the @ingest:cli partition), so a run on a second folder replaces the documents of the first. The run prints a line with the count and the first three names it replaced. To keep several, add each as its own source and run kb ingest with no --path. A web or api source takes url where a files source takes path:

contextlake kb source add notes --type files --set path=./notes

--for-repo names the already-indexed repo the documents are about. Every symbol a document mentions by name gets a documented_by edge to that document, so "where is this function explained?" is a graph hop instead of a search. Without it, documents are still stored and embedded, but they link to nothing. The per-source equivalent is for_repo = "group/app" on a [[sources]] entry.

Sources follow a tiny seam, so common ones are built-in and config-only while anything heavier is a loosely-coupled plugin: bake in the common, plugin the rest:

# kb.toml, built-in "files" source (no code, no extra install)
[[sources]]
type = "files"
name = "handbook"
path = "~/notes"
include = ["*.md", "*.txt"]

A leading ~ in path means your home directory.

A text file is skipped, with a warning that names it, when it is over max_bytes or is not valid UTF-8. Neither fails the run. A file that exists but cannot be opened (a permission error) is reported as unreadable, and the run exits 1.

PDFs: the text layer, and nothing pretending to be more#

Design docs, RFCs and architecture decisions arrive as PDFs, so the files source reads them as well. *.pdf is one of its default globs, and the text comes from the PDF's text layer through pypdf, which rides in its own extra so the core stays a single dependency:

pip install "contextlake[kb-pdf]"

[kb-pdf] is deliberately not part of [kb-full]; see the extras table. If you set include yourself, list "*.pdf" in it, a custom include replaces the defaults; it does not add to them. A single string, such as --set include='*.md', is read as one glob.

What the PDF path does not do is the load-bearing half:

Images ingested as their own files are OCR'd, covered below, but a PDF's pages are never rasterised to reach that path.

So a scanned or image-only PDF has no text layer, and contextlake says so and stores nothing. The alternative would be aggregating an empty document that looks like knowledge in search results and in the wiki:

files: skipping scan.pdf -- no extractable text (12 page(s) read, all empty). contextlake reads
  a PDF's text layer only; a scanned or image-only PDF has none and is not OCR'd.

Three other outcomes are just as loud, because a skipped PDF and a directory with no PDFs must never look the same:

That last one is the source's existing 1 MB cap, the same knob text files use. It does two jobs here: it gates the file on disk, and it bounds the text pulled out of it.

Reading stops at the first page boundary past the cap. The document is kept and marked truncated, so a 900-page PDF costs the pages that fit rather than the whole file. Raise max_bytes on the source to take more.

Images: read locally, or not at all#

Screenshots, exported diagrams and photographed whiteboards carry text that is otherwise invisible to search. *.png, *.jpg, *.jpeg, *.webp and *.bmp are default globs, and the text comes from a local OCR engine in its own extra:

pip install "contextlake[kb-ocr]"

The choice of engine is the whole point of this feature.

The obvious way to read an image is to send it to a vision model. That would have made this the first ingest path to leave local-first: every image would go over the network, one file at a time, to a third party.

[kb-ocr] ships its models inside the wheel instead. A first run downloads nothing, and no image leaves the machine. The offline boundary holds for images exactly as it does for code.

It is a large extra, an estimated 390 MB once onnxruntime and opencv land, which is why it is separate from [kb] and from [kb-full], the same call [kb-fastembed] makes.

rapidocr opens each image file with Pillow, so a Pillow advisory reaches files in your repositories that this tool did not write. That is why [kb-ocr] requires Pillow 13 or newer. Pillow 13 needs Python 3.11 or newer. On Python 3.10 the extra fails to install: pip reports no matching Pillow, because the only Pillow for 3.10 is version 12, which has eight advisories.

An OCR'd document is marked. Its node carries ocr = true in attrs, because OCR misreads and a reader deciding how far to trust a line should not have to infer that from a file extension.

The quiet outcome is the common one and it is still loud: an image the engine reads no words in (a logo, an icon, a photograph) is reported and stored as nothing, instead of becoming an empty document that looks like knowledge:

files: skipping logo.png -- the OCR engine read no text in it. An image with no words in it
  (a logo, an icon, a photograph) is expected to land here.

Without the extra, images are skipped with one line per run naming the count and the install command, the same shape the PDF reader uses.

Video: two layers, because they promise different things#

A recorded design review holds two kinds of text, and contextlake reads them with two separate extras so you can take one without the other. *.mp4, *.mov, *.mkv and *.webm are default globs.

pip install "contextlake[kb-video]"        # decode + on-screen text
pip install "contextlake[kb-transcribe]"   # ...and the spoken track

[kb-video] decodes the file and runs sampled frames through the same local OCR engine images use. It adds av, which bundles its own ffmpeg, so there is no system package to install first and nothing is downloaded at runtime: as offline as image ingestion. It reads the slides, the terminal and the UI.

[kb-transcribe] adds the spoken track with a local speech model. That model is fetched once on first use and cached under ~/.contextlake/models, the same way [kb-local]'s embedder is. That is a weaker offline promise than frame OCR's, which is why it is a separate extra rather than folded into the first. CONTEXTLAKE_WHISPER_MODEL picks the size; the default is the smallest useful one, because a first run downloads it.

The work is bounded, not the file#

max_bytes gates every other type because file size predicts how much text a document contributes. For a video it predicts resolution and length instead, so a 1 MB cap would reject every real recording.

What is capped is the sampling: one frame every 5 seconds, at most 60 frames. A two-hour recording costs the same as a ten-minute one.

Repeated on-screen lines are said once. A slide holds still across many samples, and keeping every hit would drown the transcript in its own echo.

Three outcomes#

Three outcomes are stated rather than implied:

Page numbers#

Page numbers survive the ingest. A page is to a PDF what a line number is to source code, so each document carries pages (how many the file has), pages_read and page_offsets (the character offset in the document's text where each page starts) in the attrs that land on its graph node. The document's uri stays the plain file path, so it is still a citable path on disk.

Writing a plugin#

Writing a plugin is one class with iter_documents() and one entry point, no fork, no core dependency:

# in your plugin package's pyproject.toml
[project.entry-points."contextlake.sources"]
confluence = "my_pkg.sources:ConfluenceSource"
from contextlake.kb.sources import Document          # the whole contract

class ConfluenceSource:
    def __init__(self, space=None, **_): self.space = space
    def iter_documents(self):
        yield Document(id="123", title="Runbook", text="...", uri="https://...")

Built-in sources#

contextlake kb ingest then discovers type = "confluence" automatically. Five sources ship built-in: files, web, api, graphql, and mcp. web fetches URLs and ingests their readable text (stdlib-only):

[[sources]]
type = "web"
name = "changelog"
urls = ["https://example.com/changelog", "https://example.com/roadmap"]

API sources#

An api source ships built-in too: GET a JSON endpoint and map its records to documents, with the secret read from an env var (never the config file):

[[sources]]
type = "api"
name = "tickets"
url = "https://api.example.com/v1/articles"
items = "data.articles"        # dotted path to the record list
text_field = "body"            # which key holds the document text
token_env = "EXAMPLE_API_TOKEN"  # the secret comes from this env var

id_field, title_field and text_field take a plain key or a dotted path into the record, such as fields.summary. A record with no text at that path is skipped.

A record with no id, or a null or empty one, gets an id made from its title and text, such as noid-9c68432722b2. The same record keeps its id on every run. An id names the graph node, so two documents of one source must not share one. When they do, kb ingest keeps both: it adds #2, #3 and so on to the later ones, skipping ids the source already has, and prints the count. Point id_field at a field that is unique per record to avoid the suffix.

An api source with a search_url and no url sends no request at ingest time. kb enrich queries it (see Connecting and enriching). kb ingest prints <name>: nothing to ingest: an api source with only search_url is queried by kb enrich, not kb ingest for it and exits 0. The line no documents (source reachable, nothing to ingest) is kept for a source that sent a request and got nothing back. Any web, api, graphql or mcp [[sources]] entry that sent no request prints the same nothing to ingest: line with its own reason. The reasons are a missing url, query or command, and a URL that is not http or https.

Targets that cannot be read#

A target that cannot be read does not delete its document. Say some targets of a source answer and others fail (a page that returns a 500, a file that cannot be opened). kb ingest then stores the new documents and keeps what the last run stored for everything it did not read again. The source's line says so, for example site: 1 document(s), kept 1 from the last run, 1 target(s) unreadable, and the run exits 1.

Authentication#

The credential stays on one host. The Authorization header goes only to the origin (scheme, host and port) of url. A redirect, or a next-page link, that points at a different origin is still followed, but without the header. A redirect within the same origin keeps it.

Two auth schemes. auth = "bearer" is the default and sends Authorization: Bearer <secret>. auth = "basic" sends Authorization: Basic base64(user:secret), which is what a user API token needs on Atlassian Cloud, Jira and Confluence:

[[sources]]
type = "api"
name = "issues"
url = "https://your-site.atlassian.net/rest/api/3/search/jql?jql=project%3DACME+order+by+updated&fields=summary&maxResults=100"
items = "issues"
id_field = "key"
title_field = "key"
text_field = "fields.summary"
auth = "basic"
user = "[email protected]"        # the account email; not a secret, so config is fine
token_env = "ATLASSIAN_API_TOKEN"   # the token is, so it stays in the environment

Create the token under Account settings → Security → API tokens in your Atlassian profile, then export it as ATLASSIAN_API_TOKEN.

Atlassian documents Basic auth for user API tokens, so use auth = "basic" for them. A Bearer header with a user API token against Atlassian Cloud returned 401 when the project tested it. That example assumes an unscoped user token at the site URL. A scoped user token needs an api.atlassian.com URL. A service account token can use Basic or Bearer, but only at an api.atlassian.com URL.

The Jira search address:

Note

auth, user and token_env are privileged keys: a config file contextlake discovered by walking up from the working directory may not set them, because together they decide where a secret is sent and how it is spent. Name the config explicitly with --config to use them.

GraphQL sources#

A graphql source ships built-in too: POST a query (+ optional variables) and map records in the response to documents, the same way api maps a REST response:

[[sources]]
type = "graphql"
name = "issues"
url = "https://api.example.com/graphql"
query = "{ repository { issues { nodes { id title body } } } }"
items = "repository.issues.nodes"   # dotted path into the response, rooted at `data`
text_field = "body"
token_env = "EXAMPLE_API_TOKEN"     # bearer token comes from this env var

MCP sources#

An mcp source ships built-in as well: contextlake connects as an MCP client (stdio or streamable-HTTP) to another MCP server, lists its resources, and ingests each:

[[sources]]
type = "mcp"
name = "team-kb"
command = "uvx"                 # stdio transport: a server to launch...
args = ["some-mcp-server"]
# ...or an HTTP endpoint instead:
# url = "https://mcp.example.com/sse"

So contextlake both serves a knowledge graph over MCP and consumes other MCP servers' resources into it: the loop closes on the same seam.

An mcp source may also declare a search tool rather than only reading its resources, and template codebase-derived terms into the tool's arguments. This is what powers query-driven enrichment in the enrich stage (above). Declare the tool name and an argument template with substitution placeholders:

[[sources]]
type = "mcp"
name = "team-search"
command = "uvx"
args = ["some-mcp-server"]
# Optional: call a search tool on the server, templating repo/symbol terms
tool = "search"                 # the tool name on the server
arg_template = { query = "{terms}" }  # {terms} substituted with codebase-derived terms

Both transports work with tool calling: command and args for stdio, or url for streamable-HTTP. The tool is called with the templated arguments during enrichment, returning documents grounded to the codebase's query context.

Additional keys#

Additional [[sources]] keys. Beyond the per-type keys above, connector and ingest sources also accept:

See also#

Next steps