Dead Code Detection MCP: Boosting Agent Intelligence Fast
Part of the Dead Code guide. Start with The function passed its test. The application never called it.
In the captured result below, PermissionContainer sits under Dead, usernameHandler sits under Possibly unused, and the latter comes with three files to inspect before anyone deletes it. Both lack a detected path from an entry point. They need different follow-up work.
I build Pharaoh, a hosted MCP server for TypeScript, JavaScript, Python, C and C++ (its dead-code labels cover the first three), and I want an agent to stop at that second label, open the files behind it and explain what it found. A deletion proposal should include the evidence that makes it reasonable. This post walks through that check, including the branch limit that matters when you're working on an unmerged change.
MCP carries the result; the server does the analysis
For tool delivery, the MCP tools specification describes how servers expose tools to a model, how clients discover and call them, and what a tool result contains, leaving the analysis itself to each tool's implementation. Connecting an MCP server doesn't establish that its dead-code detector is accurate.
For Pharaoh, the useful result is a classification backed by repository structure, with separate treatment for code that no entry point reaches and no other file names and whose file has no production import, code that is unreached but still named or imported elsewhere, and exports referenced only by tests. The code graph MCP guide covers the broader architecture questions; here I want to keep the agent on a single cleanup decision.
Connect a repository and ask for a report
Install the GitHub App on the account or organization that owns the repository, authorize the repositories it can read, then connect your coding client.
Each person signs in with GitHub. npx @pharaoh-so/mcp is the command to run in a Claude Code terminal.
npx @pharaoh-so/mcp
npx runs commands from npm packages. npx launches Pharaoh's installer for Claude Code, which takes you through GitHub sign-in, registers Pharaoh with the client, and leaves your next Claude Code session to connect. claude mcp add performs that registration when the installer invokes it. The installer doesn't scan your working directory. The connection guide covers the remaining details.
For another MCP client, add the remote server at https://mcp.pharaoh.so/mcp and sign in with GitHub.
In either client, ask about one of your repositories. Every repository is live during the trial and on Pro.
Start with a module you recognize. Give the agent this instruction, with your repository and module in place of the placeholders:
Use get_unused_code for the repository <repo> and the module <module>.
Keep Dead, Possibly unused and Used only by tests separate.
Show the evidence for each candidate. Don't edit files yet.
This is a prompt for the agent, not a shell command or captured response. If it needs help finding the module, ask it to use get_codebase_map first, then inspect that module with get_module_context, keeping the cleanup scoped to code you can review.
The separate guide to giving a coding agent repository context covers that initial orientation.
Read Dead and Possibly unused as different findings
get_unused_code separates a missing call path from the other evidence that might explain it. Dead requires every signal to agree: no entry point reaches the function, no other file names it, and no production file imports its file. Entry points include endpoints and CLI commands, as well as cron jobs, event handlers, workers and entry files.
Here's a trimmed capture of features/auth from revision 54343aa6 in the public repository Pharaoh-so/cal.diy. Only whole lines were removed, the summary retains the full module totals, and the excerpt keeps one example from each of the first two categories, so it isn't a complete deletion list.
# Unused code in cal.diy / features/auth: 3 dead, 4 possibly unused, 1 used only by tests
## Dead (3): unreached, named in no other file, file not imported
- PermissionContainer — packages/features/auth/PermissionContainer.tsx (14 LOC)
## Possibly unused (4): unreached, but named elsewhere or imported. Check before deleting.
### Named in `apps/web/app/api/auth/signup/handlers/calcomSignupHandler.test.ts`, `apps/web/app/api/auth/signup/handlers/calcomSignupHandler.ts`, `packages/lib/server/username.ts` (1)
- usernameHandler — packages/features/auth/signup/username.ts
PermissionContainer is a stronger deletion candidate under those rules. I'd still have the agent open its file, check the surrounding source and the intended entry points, then propose a small change, especially if the local checkout differs from the revision represented in the graph.
For usernameHandler, there is already specific work to do. get_unused_code names a signup handler, its test, and another production file, so the agent should inspect those references before making a cleanup proposal. A name in another file can mean string dispatch or dynamic loading, a use that isn't a detected call path.
Possibly unused also covers an unreached function whose file is imported by production code. That import is enough to keep the function out of Dead, even if the graph hasn't traced a call into it.
The full capture also reports deriveNameFromOrgUsername under Used only by tests. Its test references establish a different kind of use, the production connection still needs checking, and the right change might be to wire the export into the application, rather than remove an intended feature. The separate example of exports whose only caller is a test goes through that decision.
Give the agent a review step before deletion
Ask it for the source evidence first. For each candidate, have it inspect the listed files, explain any framework loading convention, identify what the code was intended to do, and say whether the right patch is deletion or a missing production call. Those are review instructions, not claims that a tool can infer your feature requirements.
get_unused_code can also check named functions through its functions parameter.
Use this review sequence for each candidate:
- Open the candidate's source file and the files named in the report.
- Check whether an endpoint, CLI command, cron job, event handler, worker or entry file loads it.
- Check named references and production imports, including string dispatch or dynamic loading that the graph may not detect as a call path.
- If the source shows a path from an entry point, treat the function as wired in and verify that path instead of treating the function as an orphan.
- Ask whether the right patch is deletion, a missing production call, or no change.
- For a proposed deletion, inspect the diff, run the repository's existing checks and exercise the application entry that should still work.
For example, if the report lists usernameHandler as possibly unused, open packages/features/auth/signup/username.ts and the named signup handler, its test, and packages/lib/server/username.ts before proposing a deletion. A name in another file can mean string dispatch or dynamic loading, a use that isn't a detected call path. If the report lists PermissionContainer as Dead, open its file and check the surrounding source and intended entry points before making the smaller change, especially if the local checkout differs from the revision represented in the graph.
When the answer shows a path from an entry point, the function is wired in; check that path against the source instead of treating the function as an orphan.
This separate, untrimmed result is from the same cal.diy repository and revision, with functions: ["checkAdminOrOwner"]:
# Reachability in cal.diy
- checkAdminOrOwner (packages/features/auth/lib/checkAdminOrOwner.ts): reachable: getAtomEventTypeApp → getEventTypesAppIntegration → checkAdminOrOwner
get_blast_radius reports production callers and affected endpoints. Before changing shared code, open each listed caller in the source.
Use the change impact guide for the wider review, including what else the change could affect.
Test-file callers are excluded from that tool's results. Check tests separately while preparing the patch.
Keep the first deletion small. Once you've reviewed the references, run the repository's existing checks and exercise the application entry that should still work, reading the resulting diff as well, because a structural report doesn't establish that the behavior you need is correct. For a local TypeScript cleanup workflow, use the guide to Knip and other TypeScript tools.
Check which branch the answer describes
Pharaoh's graph describes the repository's default branch, not your current feature branch, and it doesn't include uncommitted edits, so compare the report with the source you're about to change. This matters if you've just added a caller locally or removed the last one in a pull request.
pharaoh_account can request a refresh before the next push, while automatic refresh follows pushes to the default branch for a live repository, so check the revision you're asking about, particularly after a recent merge. Neither turns the interactive map into an analysis of your working tree.
For Python, use the same report-and-review process, inspect the entry points the application actually loads, and check any named references before removing a candidate, keeping that same default-branch limit in mind. get_unused_code produced the TypeScript capture above, so treat it as a result-format example rather than a Python accuracy benchmark.
Catch new dead code during pull-request review
PR Guard runs on Pro workspaces, posts a GitHub check named "Pharaoh PR Guard", and is advisory by default, with a neutral result that doesn't block merging, so review its findings alongside your existing checks. Its four checks cover new unreachable exports, breaking signature changes with existing callers, exact function duplicates and the blast radius of changed functions.
Review newly added exports while the intended production caller is still fresh in your mind. A missing connection may need an implementation change.
A .pharaoh.yml file in the repository root handles legitimate exceptions, with entry_points marking files whose exports count as entry points, such as handlers a framework loads, and allowed_orphans excluding exports that are unused on purpose. Write down the actual loading convention before adding an exception. PR Guard reads this file from the pull request's branch; the interactive map uses it after it reaches the default branch.
The GitHub App can read authorized repositories and publish check results, but it cannot edit files, push commits, create branches or open pull requests. Pharaoh retains graph metadata, such as names and signatures, instead of keeping source-file or function bodies. The security documentation covers those permissions and storage details.
To try the workflow, connect one repository, ask for the unused-code report in a module you know, and review one candidate with your agent, leaving the edit until you've checked its references against the code you're running.
Article Record
ReferencesPrimary sources used for this article
Update HistoryMeaningful revisions to this article
- Added a practical example showing how an agent could review code before deleting it, helping readers apply the workflow more safely.