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 withclient work requires the '*' document scope. - Leave
MCP_READ_ONLYunset or false. Submitting results is refused when the server is read-only. - Store a schema. The first
add_documentoradd_filepublishes thedefaultschema. To choose another, callapply_schema_additionsbefore the first add. Listing and submitting refuse a graph with no schema. - Set
EXTRACTION_MODEtollmorhybridin the server's environment. Inglinermode 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
-
Add documents with
add_documentoradd_file. Inllmmode, every chunk of the document becomes pending work. Inhybridmode, the local model reads each chunk first, and only the eligible chunks become pending work. -
Call
list_pending_workwithkindset toextraction. Passlimitto change the page size. The default is 5 items, and the maximum is 10. The response includesremaining, which counts the pending items after the page. -
Read the batch. It carries
instructions,output_schema,schema_types, anditems. Each item has anitem_id, acontent_version, and aninputobject. Theinputobject holds the chunktext, thelocal_resultfrom the local model when one exists, and theclient_modelrecorded for the chunk. -
Build an
outputfor each item. The output has these fields.
| Field | Default | Each entry |
|---|---|---|
entities | Empty list | A label that the schema declares, a text copied from the chunk, and optional properties. |
relations | Empty list | A label that the schema declares, plus source_text and target_text, both copied from the chunk. |
proposed_entities | Empty list | A label that the schema does not declare, with the same text rule as entities. |
proposed_relations | Empty list | A 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.
- Call
submit_work_resultswithkindset toextraction. Pass up to 20 results. Each result holds theitem_idandcontent_versionfrom the batch, and theoutputyou 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.
-
Call
get_jobwith the jobid. Passwait_secondsup to 20 to hold the call until the job finishes. Whenstateisdone, theresultobject holds the outcome.acceptedcounts the results that passed validation and changed the graph.errorsholds one entry for each refused result, withitem_id,path, andmessage.
An identical repeat of a result that was already saved is neither counted in
acceptednor reported inerrors. -
Fix the refused results, then submit only those items again. Use the
messageandpathto find the cause in the table below. If an error says the source changed or the item is stale, calllist_pending_workagain to get a newcontent_version. Do not resend the old version. -
Call
list_pending_workagain. Refused items stay pending, so they appear in the list until a valid result is saved. Repeat the steps untilremainingis 0 anditemsis empty.
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.
path | message | Cause | Action |
|---|---|---|---|
entities[N].label or relations[N].label | label "..." is not in the schema | The 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].text | text "..." is not in the chunk | The 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_relations | text "..." is not in the chunk | An endpoint is not an exact substring of the chunk. | Copy the endpoint exactly from input.text. |
entities[N].properties.NAME | property "NAME" is not declared for "LABEL". | The entity label does not declare that property. | Remove the property. |
item_id | the item is stale or no longer pending; list again | The chunk is missing, or its document link is closed, for example after the document was removed. | List the work again. |
content_version | the source changed; list the work again | The chunk, the schema, or the extraction settings changed after the list call. | List the work again and resend with the new version. |
output | this item was applied with a different output | The item already has a different saved result. | Do not resend. The saved result stays. |
item_id | the 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 result | A validation message from the reply or envelope model | The 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.
| State | Meaning for client work |
|---|---|
pending | The submission is queued. Results are not saved yet. |
running | The job is saving results. The job enters this state when it records its first progress update. |
done | The job finished. Read result for accepted and errors. A job can be done and still have refused results. |
failed | An 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. |
cancelled | The job stopped because cancellation was requested. |
interrupted | The server stopped before the job finished. Results saved before the stop stay saved. Call get_job before you retry. |
waiting | Not 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_budgetdoes not limit client work. In a PythonGraphinhybridmode, 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.
Related guides
- Run the MCP server covers the server settings, scope, and write access that client work needs.
- Tools reference lists the arguments and the read and write behavior of
list_pending_work,submit_work_results, andget_job. - Choose an extraction mode explains which chunks each mode queues.
- Control LLM cost during extraction explains the escalation budget for Python extraction.