Agent mode: test an AI agent against your org
Run the emulator as an MCP server, let an agent use it as its Salesforce, inject a failure it was not told about, and read back what it actually did.
What it is
The emulator runs as an MCP server. An agent uses it as its Salesforce — creating, querying and updating records through tools instead of a real org. You get a report of what the agent did: which records changed, which triggers fired, and whether it recovered from the failures you injected.
Start it
Start the emulator with --agent:
docker run -d --name sfemu-demo -p 8080:8080 \
-v /tmp/npsp/force-app:/corpus:ro \
fidelic/emulator:latest :8080 --agent --apex-src /corpus
--agent changes two things. POST /__emulator__/reset returns an empty org instead of
re-applying whatever was declared at startup, and execution tracing is on for every request
rather than only the ones carrying an opt-in header.
Then run the MCP server, which drives the emulator you just started:
docker exec -i sfemu-demo /sfemu mcp --base http://127.0.0.1:8080
sfemu mcp speaks MCP over stdio and expects a host that holds the pipe open, such as Claude
Code. Piping a single line with echo or printf closes stdin and the server exits without
answering.
Register it with Claude Code:
claude mcp add sfemu -- docker exec -i sfemu-demo /sfemu mcp --base http://127.0.0.1:8080
Or point it at a local build instead of the container’s binary:
claude mcp add sfemu -- sfemu mcp --base http://127.0.0.1:8080
Check it registered:
claude mcp list
# sfemu: docker exec -i sfemu-demo /sfemu mcp --base http://127.0.0.1:8080 - ✓ Connected
Tools
Fourteen tools, in two groups.
Agent tools — the five an agent needs to use the org as its Salesforce:
| tool | what it does |
|---|---|
sf.query | Run a SOQL query against the emulated org and return the matching records. |
sf.create | Create one record and return its new id, running whatever triggers the org has for that object. |
sf.update | Update one record by id, running whatever triggers the org has for that object. |
sf.delete | Delete one record by id, running whatever delete triggers the org has. |
sf.describe | Describe one sObject and return its fields, types and whether each is required. |
Driver tools — the nine you use to set up a run and read it back:
| tool | what it does |
|---|---|
session.start | Reset the org to empty, turn on execution tracing, and return a new session id to use for the rest of this run. |
session.reset | Clear all records, armed faults, hooks and governor limits, keeping the current session id. |
session.grade | Summarise what this session did: records changed, triggers fired, faults hit and whether each was recovered from. |
fault.arm | Arm one injected failure, so the next matching write returns a real Salesforce error and the code under test has something to catch. |
fault.clear | Disarm every injected failure and reset the injected latency to zero. |
limits.set | Turn governor limit counting on or off and optionally force low ceilings, so a LimitException can be triggered on demand. |
callout.mock | Register a canned HTTP response for one endpoint, so Apex callouts to it are answered instead of throwing. |
hook.add | Register one declarative trigger on an object, to set a field, reject the write, or create a child record. |
apex.testrun | Run the org’s Apex test classes and return pass, fail or refusal for each method. |
--agent-tools-only registers the five agent tools and hides the nine driver tools:
claude mcp add sfemu -- sfemu mcp --base http://127.0.0.1:8080 --agent-tools-only
Use it for a graded run. An agent that can arm its own faults and then grade itself is not reporting on the code under test.
Break something on purpose
Arm a failure yourself, before the agent starts:
curl -sX POST http://127.0.0.1:8080/__emulator__/faults \
-H 'Content-Type: application/json' \
-d '{"errorCode":"UNABLE_TO_LOCK_ROW","object":"Opportunity","trigger":"update","afterN":56}'
Fifty-six updates to Opportunity succeed; the fifty-seventh returns a real
UNABLE_TO_LOCK_ROW envelope. The fault is scoped to one object and to writes, so reads and
work on other objects are untouched.
Eight failures can be injected:
| failure | how |
|---|---|
UNABLE_TO_LOCK_ROW | fault.arm with that errorCode; HTTP 400 |
REQUEST_LIMIT_EXCEEDED | fault.arm with that errorCode; HTTP 403 |
| Injected latency | fault.arm with latencyMs; delays every data response |
| Hook rejection | hook.add with action.type: "reject"; HTTP 400, FIELD_CUSTOM_VALIDATION_EXCEPTION unless you name another code |
System.LimitException — too many SOQL queries | limits.set with a queries ceiling |
System.LimitException — too many DML statements | limits.set with a dmlStatements ceiling |
System.LimitException — too many DML rows | limits.set with a dmlRows ceiling |
| Failed callout | callout.mock with a 4xx or 5xx status |
The two error codes are the whole catalog. Any other errorCode is refused rather than armed:
curl -sX POST http://127.0.0.1:8080/__emulator__/faults -d '{"errorCode":"NOT_A_REAL_CODE"}'
# {"error":"unknown fault errorCode: NOT_A_REAL_CODE","success":false}
The grade
session.grade summarises the run. This is the grade from the run recorded in the demo, with
one UNABLE_TO_LOCK_ROW armed on Opportunity and an agent that was not told about it:
{
"callouts": {
"mocked": 0,
"unmocked": 0
},
"faults": [
{
"errorCode": "UNABLE_TO_LOCK_ROW",
"fired_at_request": "req-122",
"mechanism": "fault-injection",
"object": "Opportunity",
"record_id": "a03000000000079AAA",
"retried": true,
"retry_request": "req-129",
"retry_succeeded": true,
"verdict": "RECOVERED"
}
],
"limits": {
"dmlRows": {
"ceiling": 10000,
"exceeded": false,
"used": 0
},
"dmlStatements": {
"ceiling": 150,
"exceeded": false,
"used": 0
},
"enabled": false,
"label": "APPROXIMATE",
"note": "APPROXIMATE — counts modeled operations only; not Salesforce-exact (SIMULATED)",
"queries": {
"ceiling": 100,
"exceeded": false,
"used": 0
}
},
"ok": true,
"records": {
"created": 60,
"deleted": 0,
"rolled_back": 0,
"updated": 61
},
"requests": 131,
"session": "",
"triggers_fired": {},
"unexplained_changes": [],
"verdict": "0 passed, 1 recovered, 0 failed; 0 unexplained changes; limits APPROXIMATE"
}
requests, fired_at_request and retry_request vary per run because the agent is a language
model. The other fields were identical on a second run.
Each fault gets one of four verdicts:
PASSED— armed and never hit.RECOVERED— hit, and the next request to the same record succeeded.FAILED— hit, and no successful retry of that record.HIT— a governor limit, which addresses no record and so cannot be retried.
unexplained_changes lists records changed that the request did not address. Trigger side
effects appear here by design: a rollup written onto a record the agent never named is exactly
what this list is for.
limits carries the label APPROXIMATE because the governor counts modeled operations only and
is not Salesforce-exact. The label is the first word of the emulator’s own note, carried through
rather than restated, so the grade cannot claim an accuracy the counters do not have.
What it does not do
Governor limits count modeled operations only. They are not Salesforce-exact, which is why the numbers arrive labelled.
There is no authentication. A bearer token is checked only if a request presents one, and
the /__emulator__/ control endpoints have no auth check at all. Anything that can reach the
port can reset the org, arm a fault or read the trace.
Traces live in memory and are never written to disk. They carry record field values, and they die with the process.
triggers_fired is empty in the demo grade above, and that is a limit of the demo rather than
a clean run. NPSP ships TDTM_Opportunity, but only 8 of the corpus’s 26 triggers are
EXECUTE-classified in that build and Opportunity is not among them, so the REST path handled
every write and no trigger body was entered. A run that wants a populated triggers_fired
should target DataImportBatch__c, which is EXECUTE-classified and cascades.