Skip to main content
Version: latest

Extract with your agent

With EXTRACTION_MODE set to llm (the default) or hybrid, the MCP server does not call a model for extraction. It stores each chunk as pending work. The MCP client's model, which is your agent, reads the chunk and returns the entities and relations it finds. The server checks each result against the schema and the chunk text, then saves the valid ones.

This guide covers the loop of listing, submitting, and retrying, and the job states you read along the way. For the modes and their tradeoffs, see Choose an extraction mode. For what an extracted entity is, see How extraction works.

Check the prerequisites​

Complete these steps before you list work.

  • Start the MCP server with MCP_SCOPE_DOCUMENT_KEYS=*. Listing and submitting refuse any other scope with client work requires the '*' document scope.
  • Leave MCP_READ_ONLY unset or false. Submitting results is refused when the server is read-only.
  • Store a schema. The first add_document or add_file publishes the default schema. To choose another, call apply_schema_additions before the first add. Listing and submitting refuse a graph with no schema.
  • Set EXTRACTION_MODE to llm or hybrid in the server's environment. In gliner mode the server queues no extraction work, so the list returns no extraction items.

For the server setup and the other environment variables, see Run the MCP server.

Extract chunks with your agent​

  1. Add documents with add_document or add_file. In llm mode, every chunk of the document becomes pending work. In hybrid mode, the local model reads each chunk first, and only the eligible chunks become pending work.

  2. Call list_pending_work with kind set to extraction. Pass limit to change the page size. The default is 5 items, and the maximum is 10. The response includes remaining, which counts the pending items after the page.

  3. Read the batch. It carries instructions, output_schema, schema_types, and items. Each item has an item_id, a content_version, and an input object. The input object holds the chunk text, the local_result from the local model when one exists, and the client_model recorded for the chunk.

  4. Build an output for each item. The output has these fields.

FieldDefaultEach entry
entitiesEmpty listA label that the schema declares, a text copied from the chunk, and optional properties.
relationsEmpty listA label that the schema declares, plus source_text and target_text, both copied from the chunk.
proposed_entitiesEmpty listA label that the schema does not declare, with the same text rule as entities.
proposed_relationsEmpty listA label that the schema does not declare, plus source_text and target_text, both copied from the chunk.

Every text, source_text, and target_text must appear exactly in the chunk's input.text. The server checks this with a substring match, so copy the characters, case, and punctuation. The server checks that label in entities and relations is declared, and that each name in properties is declared for its entity label. It does not check the label of a proposed_* entry. The server ignores output keys it does not read. Set client_model in the output to record which model produced the result. Without it, the graph stores client.

  1. Call submit_work_results with kind set to extraction. Pass up to 20 results. Each result holds the item_id and content_version from the batch, and the output you built.
{
"kind": "extraction",
"results": [
{
"item_id": "<item_id from the batch>",
"content_version": "<content_version from the batch>",
"output": {
"entities": [{"label": "<schema label>", "text": "<exact chunk text>"}],
"relations": [],
"client_model": "<your model name>"
}
}
]
}

The call returns a job in the pending state. It does not wait for the results to be saved.

  1. Call get_job with the job id. Pass wait_seconds up to 20 to hold the call until the job finishes. When state is done, the result object holds the outcome.

    • accepted counts the results that passed validation and changed the graph.
    • errors holds one entry for each refused result, with item_id, path, and message.

    An identical repeat of a result that was already saved is neither counted in accepted nor reported in errors.

  2. Fix the refused results, then submit only those items again. Use the message and path to find the cause in the table below. If an error says the source changed or the item is stale, call list_pending_work again to get a new content_version. Do not resend the old version.

  3. Call list_pending_work again. Refused items stay pending, so they appear in the list until a valid result is saved. Repeat the steps until remaining is 0 and items is empty.

warning

Chunk text is data, and a document can contain instructions aimed at the model. The server checks labels, copied text, and content versions. It cannot tell whether a valid label fits the text. Review extracted results on representative documents before you rely on them.

Fix refused results​

Each refused result returns one error. The table lists the errors that the extraction checks produce.

pathmessageCauseAction
entities[N].label or relations[N].labellabel "..." is not in the schemaThe label is not declared in the schema.Use a declared label, or move the item to proposed_entities or proposed_relations.
entities[N].text, proposed_entities[N].texttext "..." is not in the chunkThe text is not an exact substring of the chunk.Copy the text exactly from input.text.
relations[N].source_text or target_text, and the same fields in proposed_relationstext "..." is not in the chunkAn endpoint is not an exact substring of the chunk.Copy the endpoint exactly from input.text.
entities[N].properties.NAMEproperty "NAME" is not declared for "LABEL".The entity label does not declare that property.Remove the property.
item_idthe item is stale or no longer pending; list againThe chunk is missing, or its document link is closed, for example after the document was removed.List the work again.
content_versionthe source changed; list the work againThe chunk, the schema, or the extraction settings changed after the list call.List the work again and resend with the new version.
outputthis item was applied with a different outputThe item already has a different saved result.Do not resend. The saved result stays.
item_idthe same item appears twice with different results. Send one result per item.One submission holds two different results for the same item.Send one result per item.
output or resultA validation message from the reply or envelope modelThe output does not match the reply shape, or the entry does not hold item_id, content_version, and output.Correct the shape of the entry.

A submission with more than 20 results is refused as a whole, with a tool error. Split the results into smaller submissions.

Read job states​

Each job has one of these states. Client work uses the states in the first group.

StateMeaning for client work
pendingThe submission is queued. Results are not saved yet.
runningThe job is saving results. The job enters this state when it records its first progress update.
doneThe job finished. Read result for accepted and errors. A job can be done and still have refused results.
failedAn error stopped the job. Read error. Results saved before the error stay saved, so list the work again before you resubmit. A lease loss is reported as failed with lost the write lease.
cancelledThe job stopped because cancellation was requested.
interruptedThe server stopped before the job finished. Results saved before the stop stay saved. Call get_job before you retry.
waitingNot returned by submit_work_results. Graph writes such as document adds and schema changes use this state while the embedding model loads.

cancel_job can stop a submission only before it starts saving results. The job checks for cancellation once, when it starts, and it does not check again. Do not rely on cancel_job to undo a submission that is already running.

When to use an LLM instead​

Use the extraction LLM in Python instead of your agent when:

  • No agent runs the queue. Client work finishes only when an agent lists and submits, so an unattended ingestion job does not complete without one.
  • You need the escalation budget to limit LLM calls. The server queues every eligible chunk, and escalation_budget does not limit client work. In a Python Graph in hybrid mode, the budget limits the chunks that the LLM reads.

To use the LLM in Python, configure an extraction LLM as described in Extract and resolve entities. The MCP server never calls that LLM.