Skip to content

Add Mastra plugin - #2227

Merged
webpro merged 2 commits into
webpro-nl:mainfrom
ashokram-si:fix/mastra-plugin-2222
Oct 28, 2026
Merged

webpro merged 2 commits into
webpro-nl:mainfrom
ashokram-si:fix/mastra-plugin-2222

Conversation

@ashokram-si

Copy link
Copy Markdown
Contributor

Closes #2222

Adds a plugin for Mastra, enabled by the mastra CLI package.

mastra dev and mastra build pick up files by convention under src/mastra, so these were reported as unused:

  • index.{ts,js} and the storage/observability/logger/server/studio singletons
  • workflows/* and tools/** (minus tests, like Mastra's own globs)
  • file-based agents in agents/<name>/: config, instructions, memory, workspace, tools/, scorers/, skills/, processors/{input,output}/, schedules/**, and nested subagents/<name>/

Patterns follow Mastra's source (deployer/src/build/fs-routing/discover.ts and bundler/index.ts, linked in the plugin). Also checked against Mastra's examples/agent: the 7 false positives for its file-based weather-fs agent are gone.

Not included: the --dir CLI option (custom dirs can use mastra.entry for now).

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the plugin, Ashok! The patterns follow Mastra's discovery code, and on Mastra's examples/agent all seven weather-fs files are no longer reported as unused, including the nested subagents.

One request before merging: let's support the --dir and --tools options right away instead of leaving them for later, see the inline comments.

Comment on lines +17 to +29
const agentRoots = 'src/mastra/agents/*/*(subagents/*/)';

const production = [
`src/mastra/index.${extensions}`,
'src/mastra/{storage,observability,logger,server,studio}.{js,ts,mjs,mts}',
`src/mastra/workflows/*.${extensions}`,
`src/mastra/{,agents/*/}tools/**/*.${extensions}`,
`${agentRoots}{config,instructions,memory,workspace}.${extensions}`,
`${agentRoots}{tools,scorers,skills,processors/input,processors/output}/*.${extensions}`,
`${agentRoots}schedules/**/*.${extensions}`,
`!src/mastra/**/*.{test,spec}.${extensions}`,
'!src/mastra/**/__tests__/**',
];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mastra dev --dir <dir> and mastra build --dir <dir> use that folder instead of src/mastra for the entry file and all file-based discovery (dev, build). Could you build these patterns from the Mastra folder, and add them with toProductionEntry from an args.resolveInputs handler when --dir is set? The React Email plugin handles its dir option the same way. Note that mastra start --dir points at the build output instead, so the handler needs to check the command. A fixture script like mastra dev --dir app/mastra would cover it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 781635f: the production patterns are built by getProductionPatterns(dir), used for the default src/mastra and, from args.resolveInputs, for --dir/-d on dev and build (start is skipped since its --dir is the build output). Fixture script dev:app: mastra dev --dir app/mastra; the test checks app/mastra/index.ts, a file-based agent's tool and the file it imports are no longer reported.

`src/mastra/index.${extensions}`,
'src/mastra/{storage,observability,logger,server,studio}.{js,ts,mjs,mts}',
`src/mastra/workflows/*.${extensions}`,
`src/mastra/{,agents/*/}tools/**/*.${extensions}`,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same commands also take --tools <paths> (-t), a comma-separated list of tool files, globs or directories. Mastra bundles each match as a separate input next to the default tools/** (listToolsInputOptions) and imports them all from the generated tools.mjs (here), so a tool listed only there is an entry file, along with the dependencies it imports. These can be added as production entries from the same handler (a directory resolves to its index.ts or index.js), covered by a script like mastra build -t lib/tools/*.ts.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in 781635f: --tools/-t is split on commas and each item becomes a production entry; a directory resolves to <dir>/index.{js,ts}, files and globs are used as given. Fixture script build:tools: mastra build -t lib/tools/*.ts,lib/extra-tools; the test checks both tool files, the module one of them imports and lib/extra-tools/index.ts are reached, while lib/unused.ts next to them is still reported.

`mastra dev --dir <dir>` and `mastra build --dir <dir>` discover the entry
file and the file-based agents, tools and workflows from that folder instead
of `src/mastra`, so the production patterns are now built from the folder
and added as production entries from `args.resolveInputs` when `--dir` is
set. `mastra start --dir` points at the build output, so it adds nothing.

The same commands take `--tools` (`-t`), a comma-separated list of tool
files, globs or directories that Mastra bundles as separate inputs and
imports from the generated `tools.mjs`. Each is added as a production entry;
a directory resolves to its `index.{js,ts}`.

The fixture gets `mastra dev --dir app/mastra`, `mastra build -t
lib/tools/*.ts,lib/extra-tools` and a `start --dir` script, and the test
checks the files they reach are no longer reported.

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding both options! --dir on dev and build (with start left out) and --tools as production entries work as expected, and the fixtures cover both. Looks good to me.

@pkg-pr-new

pkg-pr-new Bot commented Oct 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/knip@2227
npm i https://pkg.pr.new/@knip/language-server@2227
npm i https://pkg.pr.new/@knip/mcp@2227

commit: 781635f

@webpro
webpro merged commit dd4e1f7 into webpro-nl:main Oct 28, 2026
32 checks passed
@ashokram-si
ashokram-si deleted the fix/mastra-plugin-2222 branch October 28, 2026 06:44
@webpro

webpro commented Oct 29, 2026

Copy link
Copy Markdown
Member

🚀 This pull request is included in v6.44.0. See Release 6.44.0 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

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.

💡 Mastra Plugin

2 participants