Inspiration
Knip finds unused files, dependencies and exports in JavaScript and TypeScript projects by following imports from entry files. Mastra, a TypeScript framework for building agents and workflows, loads many files by folder instead of by import, so Knip flagged them as unused. joealden asked for a Mastra plugin in issue #2222, with src/mastra/index.ts as the main entry and support for file based agents.
What it does
The plugin, merged in webpro-nl/knip#2227 and released in Knip 6.44.0, turns on when a project depends on the mastra command line package. It marks the files Mastra discovers as entries:
- the main
indexfile plus the storage, observability, logger, server and studio files - workflows and tools, leaving test files out
- file based agents in
agents/<name>/, including their tools, scorers, skills, processors, schedules and nested subagents
It also reads mastra dev and mastra build scripts. With --dir, the same rules apply to the chosen folder. With --tools, each listed tool file, pattern or folder becomes an entry.
How I built it
I took the folder rules from Mastra's own discovery and bundler source, and linked both files in the plugin. The rules are globs (file patterns with wildcards), built by one function that takes the Mastra folder:
const getProductionPatterns = (dir: string) => {
const agentRoots = `${dir}/agents/*/*(subagents/*/)`;
// trimmed: returns the list of patterns for this folder
};
The default list uses src/mastra. A handler for the mastra command line arguments reuses the same function for --dir, and splits --tools on commas, turning a folder into its index file.
Challenges I ran into
The --dir option means different things on different commands. On dev and build it is the folder to scan, but on mastra start it points at the build output. The handler only acts on dev and build.
Agents can contain subagents, which follow the same layout. One pattern, agents/*/*(subagents/*/), matches both the top level agent folder and any subagent folder inside it.
Accomplishments that I'm proud of
On Mastra's examples/agent, the 7 false reports for its file based weather-fs agent are gone, which webpro confirmed in review. The fixture test checks both directions: files reached through the default folder, --dir and --tools are not reported, and three files Mastra never loads still are. webpro approved and merged it on 28 October 2026.
What I learned
A framework's source is the most reliable description of its conventions. Also, my first version listed --dir as not included, and review asked for it plus --tools before merging. Both lived in the same code I had already read, so they belonged in the first PR.
What's next
Nothing is pending on this PR. If Mastra adds new folder conventions later, the plugin's patterns and fixture will need matching updates, and the plugin already links the Mastra source files to compare against.
The full story, with all the code, is on my blog: https://ashokramsi.hashnode.dev/knip-mastra-plugin
Built With
- github
- knip
- mastra
- typescript