Skip to content

feat(mcp): entity tools on the CLI MCP server (#2095) - #342

Merged
nc9 merged 17 commits into
mainfrom
feat/2095-mcp-entities-cli
Sep 13, 2026
Merged

nc9 merged 17 commits into
mainfrom
feat/2095-mcp-entities-cli

Conversation

@nc9

@nc9 nc9 commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Adds the five entity tools to the squirrel mcp server: list_entities, get_entity, get_entity_graph, compare_entities, get_entity_findings. They read the project store, so they are local and free: no auth, no credits, no API round trip.

Implements section 8 of the entity map epic (private squirrelscan/repo#2095).

The loop, and what makes it verifiable

Every tool description states the same fix-and-verify loop, because an agent reads whichever tool it called first and has to learn the sequence from that one:

  1. list_entities with problem: "no-id" finds entities declared on several pages with nothing tying them together.
  2. Give each an absolute @id.
  3. Re-audit.
  4. compare_entities and check gainedId.

Step 4 is the one that is easy to get wrong. An entity that gains an @id changes key, because the key IS the @id when there is one, so a naive comparison reports one removal plus one addition and the fix reads as damage.

gainedId on its own turned out not to be enough, in two ways.

It matched two entities on type and name and stopped there, so a re-audit that visited a different part of the site paired them and reported the fix as landed while the pages that were broken still carried the old markup. The coverage test that separates removed from notCrawled was being built after the identity block, so an entity matched as an id change never reached it.

Then recrawling the broken pages turned out to be necessary but not sufficient. An audit that visits /a and /b, finds the anonymous entity gone from both because the markup was deleted, and pairs it with an identified entity that appeared on /c passes a recrawl-only test. That is a regression on two pages plus an addition on a third, reported as a clean fix.

So every gainedId and lostId entry carries coverage: "proven" | "partial", and proven requires both halves: every declaring page recrawled, and the replacement declared on every one of them.

The reverse is documented rather than fixed: a missing gainedId is not proof of failure, because the match needs the type and name unchanged, so renaming in the same edit reads as a removal plus an addition.

Sharing the contract with the cloud server

packages/core-contracts/src/entity-mcp.ts holds what both servers must agree on: tool names, tool descriptions, per-field wording, the enum value lists, the limits, and ENTITY_MCP_INPUT_FIELDS, which says what each field is — type, requiredness, accepted values, default, bounds.

The input schema object cannot be shared, and the file records why in a header comment. The MCP SDK's inputSchema accepts z3.ZodTypeAny | z4.$ZodType or a raw shape of those; a TypeBox object throws at runtime with inputSchema must be a Zod schema or raw shape, received an unrecognized object. This package cannot carry zod instead: it dropped zod for TypeBox because the private API is on zod 3 while the CLI and rules are on zod 4. So each server assembles its own zod shape from the constants, and a test asserts the JSON Schema it advertises matches the field table exactly — an equality, not a subset, with the cloud-only fields named separately. The cloud server can run the same assertion against the same table.

Result shapes are TypeBox schemas here, since a handler's return value never passes through the SDK as a schema.

Absence of evidence

Five places where an empty or confident answer and a true one looked identical:

  • get_entity_findings returns analyzed. An audit that was crawled but never analyzed returned the same three empty arrays as a site with a flawless entity graph.
  • get_entity_graph returns generatedIds for the jsonld rendering. The export mints an @id for every entity a site left anonymous, and JSON-LD has nowhere to mark one as a placeholder, so the document read as a site where everything is identified — the exact opposite of the finding that sends an agent there.
  • A filtered graph that matches nothing says so, rather than reusing the sentence for a site that declares nothing.
  • Every result carries warnings. The loader picks the latest audit that stored at least one entity, skipping a newer one that stored none, because zero rows is also what an audit predating the entity map looks like. It records which audit it passed over and the tools were dropping that. A template change that wipes a site's JSON-LD makes today's audit store nothing, and the agent asking whether the structured data is fine was shown yesterday's healthy graph.
  • A finding's keys and pages are a sample, clipped by the rule before this tool sees them, with nothing recording how many were dropped. truncation on get_entity_findings is standing rather than conditional, because truncated: false reads as a complete affected set.

A finding's keys also carried whatever the rule used as an item id, and one rule ids its items "<key> <property>", so an agent was told which entity is broken in a form get_entity cannot look up. Fixed to use the real key.

Every result carries a truncation field for the same underlying reason: an agent that cannot tell a capped list from a complete one will describe a site from a sample.

Adversarial pass over the descriptions

A separate review asked of each sentence whether an agent believing it literally would act wrongly. It found ten. Four needed code, six needed accurate wording: "every property" is nine properties plus @type; collapsing is by resolved @id, so a relative #organization yields one row per page; json/dot/graphml have no node cap, which is not the same as complete, since the CLI map stores no per-edge pages; mermaid's cap bounds declared entities and not dangling placeholders; has_more is hasMore; and naming both runs orders them chronologically whichever field named them.

Also

Resolving a named run no longer searches a bounded window of recent audits. The window reported a run that exists as missing once the store held 50 newer ones, under an error code that reads as a statement about the site.

Tests

apps/cli/tests/mcp/entity-tools.test.ts, driven through a real seeded project store rather than a stubbed controller, because the questions these tools answer are questions about storage. The fixtures are the two docs.squirrelscan.com snapshots from #336. The loop is tested end to end, including the narrow re-audit that must come back partial.

Docs

docs/entity-map/mcp.mdx and the five tools on the CLI MCP reference page. make validate passes.

https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP

nc9 added 10 commits September 13, 2026 15:45
…rvers (#2095)

The two servers read different sources — the CLI reads the project store, the
cloud reads the API — and must expose the same five tools with the same names,
the same accepted values and the same descriptions.

Constants rather than input schemas, because a shared input schema is not
reachable. The MCP SDK's `inputSchema` takes `z3.ZodTypeAny | z4.$ZodType` or a
raw shape of those; handing it a TypeBox object throws at runtime with
"inputSchema must be a Zod schema or raw shape, received an unrecognized
object", verified against a real McpServer rather than read off the types. And
this package cannot carry zod instead: it dropped zod for TypeBox precisely
because the private API is zod 3 while the CLI and rules are zod 4.

So each server assembles its own zod shape from the names, descriptions, enum
values and limits here. What has to match matches by construction; only the
validator object is unshareable.

RESULT shapes ARE TypeBox schemas, because a handler's return value never
passes through the SDK as a schema. Every one of them carries a `truncation`
field: an agent that cannot tell a capped list from a complete one will reason
about a site from a sample and state the conclusion as though it saw
everything, and saying so in the payload is cheaper than any documentation.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
list_entities, get_entity, get_entity_graph, compare_entities and
get_entity_findings, reading the project store. Local and free: no auth, no
credits.

Every name, description, accepted value and limit comes from the shared
contract rather than being written here, so the CLI and cloud servers agree by
construction. The zod shapes are assembled from those constants because the
SDK's inputSchema takes zod only; nothing an agent reads or sends differs
between the two servers.

The controller reuses what #2092 built — loadEntityMap, filterEntityMap,
findEntity, diffEntityMaps and the export renderers — rather than asking the
store the same questions a second way.

Three choices worth naming. Filters take ARRAYS, not comma-separated strings:
a client sends JSON, and the CLI's comma handling only exists because a
terminal has no arrays. get_entity reads the UNFILTERED map, because a lookup
by key is a direct question and hiding page-local entities would make a key
that list_entities just returned unfetchable. And findings are read from
rule_results rather than re-evaluated, so an agent is handed what the audit
decided rather than a second opinion that could disagree with the report the
user is looking at.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
… end to end (#2095)

Driven through a REAL project store rather than a stubbed controller, because
the questions these tools answer are questions about storage: which crawl is
latest, which two belong to the same site, whether a map survived the round
trip. A stub answers all of those by assumption.

Called through the `tools/call` request handler rather than the registered
callback, so the zod input schemas run too. A test that bypassed them would
pass with arguments no real client could send.

The loop test is the one that matters: list the no-id entities, seed the map a
re-audit would produce, and assert the fixed entity appears under `gainedId`
with nothing in `added` or `removed`. That is the promise every tool
description makes, and a fixed entity changes key, so a naive diff would report
it as one removal plus one addition.

Three things the fixtures forced into the open.

`os.homedir()` is read once at process start in Bun and does not follow a later
`process.env.HOME`. Every squirrel path derives from it, so the first run of
this file came back with a crawl id from a real kinsta.com audit in my own
~/.squirrel. Mocked at `node:os` so every path agrees.

`createCrawl` mints its own id and ignores a caller-supplied one, so the tests
assert relationships rather than ids they invented and the store never used.

`docs-after-jsonld.json` carries `pages: []` beside `pagesTotal: 60`, exported
before the publish projection stopped clipping the report's own copy. The store
derives occurrence rows from that array, so seeding it as-is gives every entity
zero declaring pages. Rebuilt in the test by inverting the nodes' own lists,
with the one lossy case named.

Also records that `compare_entities` DECLINES the default when the older audit
stored no entities, which is the docs "before" snapshot exactly: zero rows
cannot be told from a crawl predating the feature, and guessing would report
all 183 of today's entities as new.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
#2095)

`docs/entity-map/mcp.mdx` is the epic's page: the fix-and-verify loop, the five
tools with their inputs and what is capped, a worked split-identity example,
and what the tools will not do.

The loop section leads on the part that is easy to get wrong by hand. An entity
that gains an `@id` CHANGES KEY, because the key is the `@id` when there is
one, so a naive comparison reports a successful fix as one removal plus one
addition and it looks like damage. `gainedId` is the field that says the fix
landed, and `notCrawled` is the field that says the audit did not cover the
evidence either way.

Five new rows in the CLI MCP reference, all marked no-auth, because these read
the project store and spend no credits.

A script checks both pages against the contract: every tool name, every problem
value, every format, the four limits a reader plans around, and that every
description really does carry the loop the page says it does.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
…thing (#2095)

The mermaid and markdown renderers write a sentence when the map is empty:
"This site declares no JSON-LD entities." They cannot tell an empty site from a
filter that matched nothing, because a filtered map and an empty one are the
same object by the time they see it.

So an agent that filtered for a type the site does not use got back that
sentence about a site with 183 entities, and had no way to know. It is the
worst shape of wrong answer this feature can give: confident, readable, and
about the whole site.

The distinction is made in the MCP layer, which knows the unfiltered count. A
filtered empty result now says no entity matches and gives the real total. The
machine-readable formats need none of this, because an empty `nodes` array is
unambiguous data; only prose can be wrong. A site that genuinely declares
nothing still gets the original sentence, and a test pins that too.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
`gainedId` matched two entities on type and name and stopped there, so a
re-audit of a different part of the site paired them and reported the fix as
landed while the pages that were broken still carried the old markup. The
coverage test that separates `removed` from `notCrawled` was built AFTER the
identity block, so an entity matched as an id change never reached it.

Hoist the test and stamp every id change `proven` or `partial`. Dropping the
pair instead would split one fix back into an add plus a remove, which is the
outcome the module exists to avoid.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
The export mints an `@id` for every entity a site left anonymous, and JSON-LD
has nowhere to mark one as a placeholder. A reader sent there BY a no-id
finding sees a graph in which everything is identified.

Same function the export uses, so the two cannot disagree about which ids
are real.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
…#2095)

Contract: add `ENTITY_MCP_INPUT_FIELDS`, which says what each field IS — type,
requiredness, accepted values, default, bounds — not only what it means. Prose
cannot be asserted against, and two independently written zod shapes agreeing
was the whole point. Cloud-only fields are named separately so each server can
assert an exact set rather than a subset. `summary` and `entity` were
`Type.Unknown()`: the two biggest payloads, unchecked by the schema that exists
to check them.

Three descriptions that did not match the code: `page` substring-matches rather
than prefix-matches, `key` resolves @id before key, and `format` defaults to
json.

`analyzed` on get_entity_findings: an audit that was never analyzed returned the
same three empty arrays as a flawless entity graph, and only one is good news.

`generatedIds` on get_entity_graph, for the jsonld rendering.

Resolving a named run no longer searches a 50-row window of recent audits. The
window told a caller their run did not exist as soon as the store held 50 newer
ones, under an error code that reads as a statement about the site.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
…e claim

The store rebuilds a map's page set from the pages table, not from the stored
map, so a crawl seeded without page rows produced a map that had visited
nothing. Every coverage assertion would have passed for the wrong reason.

Also: assert the four "complete" formats contain every entity rather than
trusting their own `truncated: false`; assert `analyzed` rather than emptiness;
check the advertised JSON Schema against the contract's field table; restore
the node:os mock on teardown and patch its `default` export too.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the feat/2095-mcp-entities-cli branch from 37f94c5 to db2c04a Compare September 13, 2026 05:45
nc9 added 7 commits September 13, 2026 15:53
…pages

Recrawling the pages that were broken is necessary and not sufficient. An audit
that visits /a and /b, finds the anonymous entity gone from both because the
markup was DELETED, and pairs it with an identified entity that appeared on /c
passed the coverage test. That is a regression on /a and /b plus an addition on
/c, reported as a proven fix.

`proven` now requires both: every declaring page recrawled, AND the replacement
declared on every one of them. Either half alone is satisfied by a site that is
not fixed.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
Three things the tools knew and did not say.

A finding's `keys` carried whatever the rule used as an item id, and
schema/entity-conflicts ids its items `"<key> <property>"` so two conflicts on
one entity stay two rows. An agent was told which entity is broken in a form
`get_entity` cannot look up. Prefer `meta.key`, and dedupe.

A finding's keys and pages are a SAMPLE: the rule clips its own lists, typically
to five pages, before this tool sees them, and nothing records how many were
dropped. `truncation` on get_entity_findings is now standing rather than
conditional, because `truncated: false` would be read as a complete affected set.

The loader picks the latest audit that stored at least one entity, skipping a
newer one that stored none, because zero rows is also what an audit predating
the entity map looks like. It records which audit it passed over, and the tools
dropped that. A template change that wipes a site's JSON-LD made today's audit
store nothing and the agent was shown yesterday's healthy graph. Every result
now carries `warnings`, empty when clean.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
From an adversarial pass over the tool descriptions, asking of each sentence
whether an agent that believed it literally would be wrong.

- "every property" is nine properties plus @type
- collapsing is by RESOLVED @id, so a relative "#organization" is one row per page
- json/dot/graphml have no node cap, which is not the same as complete: the CLI
  map stores no per-edge pages and no per-page references
- mermaid's cap bounds declared entities, not dangling placeholders
- `has_more` is `hasMore`
- naming both runs orders them chronologically whichever field named them

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
The standing truncation notice on get_entity_findings fired even when there
were no findings, claiming a sample of nothing. It says what it says only when
there is a finding whose pages the rule already clipped.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
…needs

The loop paragraph is appended to all five descriptions and so is paid five
times on every tools/list. Adding the coverage caveats grew it to more than half
of the 8.3k characters the five descriptions cost.

The gainedId matching rule can only be acted on while reading a compare_entities
RESULT, so it moves to that tool. Same information, 180 fewer tokens per
tools/list.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 merged commit 824a0a4 into main Sep 13, 2026
14 checks passed
@nc9
nc9 deleted the feat/2095-mcp-entities-cli branch September 13, 2026 06:13
nc9 added a commit that referenced this pull request Sep 13, 2026
Completes the Entity map group: index and fixing from #338, mcp from #342, and
now the three that were missing.

**cli.mdx** — where the map appears in each of the six report formats and what
each one actually carries, `squirrel entities` with every flag and format, the
diff, and the three store tables with the queries worth knowing. The one worth
calling out is `entity_occurrences`: a node's `pages` array is capped at 50, so
"every page that declares this Organization" is answerable from that table or
from the document's own uncapped page index, and never from the node.

**format.mdx** — the version 1 document, the JSON-LD export and the change set,
with a real sample of each from an audit of docs.squirrelscan.com. Covers the
four key shapes, what can and cannot dangle, and why `coverage` on a gainedId
row needs both of its halves.

**cloud.mdx** — the dashboard Entities page, its filters and exports, and the
two API endpoints. Two differences from the local behaviour are documented
rather than left to be discovered: the API's `page` filter is a prefix match
where the CLI's is a substring match, and a filtered API response keeps a
whole-site `summary` with the filtered totals in `meta.counts`, which is the
opposite of what `squirrel entities` returns.

Also corrects `--page`'s own help text, which claimed a prefix match. It has
always been a substring match.

Every page cross-links the others. `make validate` passes, and a filesystem
link check over all 2,785 internal links finds none dead in this group.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>
nc9 added a commit that referenced this pull request Sep 13, 2026
* docs(entity-map): cli, format and cloud pages (#2098)

Completes the Entity map group: index and fixing from #338, mcp from #342, and
now the three that were missing.

**cli.mdx** — where the map appears in each of the six report formats and what
each one actually carries, `squirrel entities` with every flag and format, the
diff, and the three store tables with the queries worth knowing. The one worth
calling out is `entity_occurrences`: a node's `pages` array is capped at 50, so
"every page that declares this Organization" is answerable from that table or
from the document's own uncapped page index, and never from the node.

**format.mdx** — the version 1 document, the JSON-LD export and the change set,
with a real sample of each from an audit of docs.squirrelscan.com. Covers the
four key shapes, what can and cannot dangle, and why `coverage` on a gainedId
row needs both of its halves.

**cloud.mdx** — the dashboard Entities page, its filters and exports, and the
two API endpoints. Two differences from the local behaviour are documented
rather than left to be discovered: the API's `page` filter is a prefix match
where the CLI's is a substring match, and a filtered API response keeps a
whole-site `summary` with the filtered totals in `meta.counts`, which is the
opposite of what `squirrel entities` returns.

Also corrects `--page`'s own help text, which claimed a prefix match. It has
always been a substring match.

Every page cross-links the others. `make validate` passes, and a filesystem
link check over all 2,785 internal links finds none dead in this group.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>

* fix(cli): --page matches a substring, not a prefix

The flag's help text has always said "matching this URL or prefix". The
implementation calls `String.includes`, so `--page /blog` matches
`/tag/blog` too. Say what it does.

Claude-Session: https://claude.ai/code/session_01T8w822GqnrviVMytnAsKFP
Signed-off-by: Nik Cubrilovic <[email protected]>

---------

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9 nc9 mentioned this pull request Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant