Skip to main content
Glama

agent-context-router

An MCP server that loads agent instructions on demand.

A long AGENTS.md costs tokens on every request, and most of it applies to only a few tasks. agent-context-router splits it in two: a slim AGENTS.md that holds the rules for every turn and lists the rest as modules, each with a trigger, and this server, which reads a module when the agent reaches the work its trigger names. Modules live in ordinary git repos. Before reading one, the router fast-forwards the local clone from its remote (at most once every 30 minutes, and never over local work), then returns the module text with the commit it read.

It works with any MCP client. It's tested with goose and Claude Code.

HUMAN.md covers what changes for you once it's set up, what stays your call, and how to turn it off.

Results

In a test harness of four scenarios with five runs each, the agent used the router in all 20 runs and passed every behavior check. Median input tokens per run, slim AGENTS.md plus router vs the full AGENTS.md:

  • Device install: 84,964 vs 206,044

  • PR description: 60,782 vs 180,964

  • Review comment: 61,231 vs 117,576

  • Delegated device install: 139,984 vs 265,154. The subagent loaded the device module on its own before any device command, in 5 of 5 runs.

These come from a small fixture, so real sessions with long conversations will save less as a percentage. Method, rounds and raw data: docs/results/2026-10-25-router.md and docs/results/.

Related MCP server: Instruction MCP Server

Quick start

Requires Python 3.10+ and uv.

git clone https://github.com/dakotafabro/agent-context-router.git
cd agent-context-router
uv sync

Or install it with bpm, which puts a copy in ~/.bpm/packages/agent-context-router@<version> and lists it in your project's AGENTS.md:

bpm install agent-context-router

With bpm, use that folder wherever these steps say /path/to/agent-context-router.

The lockfile points at public PyPI. If your network blocks it and you install through a mirror, relock against the mirror once from the package folder, then add --frozen after run wherever you start the server, so later runs don't relock:

uv lock --default-index https://your-mirror.example/simple
uv run --frozen agent-context-router

Try it with the bundled example, which has three modules and a slim AGENTS.md in examples/:

ACR_REGISTRY="$PWD/examples/modules.yaml" uv run agent-context-router

Register it in goose

Add this under extensions: in ~/.config/goose/config.yaml, with your clone's absolute path:

  agent-context-router:
    enabled: true
    type: stdio
    name: agent-context-router
    description: Loads agent instruction modules on demand
    cmd: uv
    args: [run, --directory, /path/to/agent-context-router, agent-context-router]
    envs:
      ACR_REGISTRY: /path/to/agent-context-router/examples/modules.yaml
    timeout: 60

Or try it for one session without editing config:

goose session --with-extension "ACR_REGISTRY=/path/to/agent-context-router/examples/modules.yaml uv run --directory /path/to/agent-context-router agent-context-router"

Register it in Claude Code

claude mcp add -s user agent-context-router \
  -e ACR_REGISTRY=/path/to/agent-context-router/examples/modules.yaml \
  -- uv run --directory /path/to/agent-context-router agent-context-router

Then copy examples/AGENTS.md into a project (or your global hints file) and ask the agent to write a PR description. It should call load_module with pr-description before writing.

Checked on 2026-10-26 in both clients with the bundled example: goose v1.56.0 (--with-extension) loaded pr-description before writing a PR description, and Claude Code 2.1.42 (claude -p with --mcp-config, project settings only) loaded commit-messages before writing a commit message. Each load appeared in the router's log.

Tools

  • list_modules: every module available on this machine, with its trigger

  • load_module(id): the module's text, plus the commit it was read at, the repo's sync status and any warning

  • search_context(query, top_k): modules that match a task description, for when no trigger obviously applies

Each load_module and search_context call is appended to ~/.local/state/agent-context-router/loads.jsonl, so you can see which modules agents actually load.

Migrating an existing AGENTS.md

The router never changes your config or your instruction files. To split a file you already have, use migrate, which works on one file at a time and keeps the original until you choose to switch:

uv run agent-context-router migrate plan ~/.config/goose/AGENTS.md ~/agent-context
uv run agent-context-router migrate decide ~/agent-context   # answers the plan's questions, then offers to run apply
uv run agent-context-router migrate apply ~/agent-context
uv run agent-context-router migrate review ~/agent-context
uv run agent-context-router migrate switch ~/agent-context
  • plan reads the source and writes only plan.yaml, into a folder that must be empty. It splits the file at its main heading level, suggests sections over about 1.2 KB as modules, picks out one-line "never / always / before" rules to keep loaded, and warns about anything that looks like a secret.

  • decide asks the plan's questions in the terminal: for each suggested module, keep it loaded or make it a module, when it should load, and which of its rules stay loaded; then which kept sections to move; then whether you approve the split. Answers are saved as you go, so q pauses and the next run resumes. It needs a terminal. You can edit plan.yaml by hand instead, and set approved_by_person: true yourself.

  • apply refuses until the plan is approved (approved_by_person: true). It copies the source into backup/ with its sha256, then writes modules/, a slim AGENTS.md and modules.yaml into the same folder. With git installed, the folder becomes a repo whose first commit is the untouched original, with no remote. It refuses a plan with a module that has no trigger, or a source that changed since the plan, and it won't overwrite files in the folder it didn't write.

  • verify checks that every non-blank line of the original is in the slim file or a module. Text may move; it may not be dropped or reworded. apply and switch run it too.

  • review shows what switch would install (sizes, kept sections, each module's trigger and kept rules, and a diff with --diff), asks for your approval, and records one hash over those exact files.

  • switch installs only the reviewed files: it refuses if anything changed after the review. It shows the summary again and asks before replacing the source, refuses if the source changed since the backup, and copies the current file aside first. It doesn't ask when there's no terminal, so it never switches unattended unless you pass --yes.

  • restore puts the original back. It refuses if the file was edited after the switch (pass --force to restore anyway; the edited file is copied aside first).

Which sections stay loaded is your call: plan only suggests. The full walkthrough, including where you approve each change, is in docs/migrate.md. Project and subdirectory AGENTS.md files aren't handled yet, since moving their rules changes which rule wins. On the author's 60 KB policy, plan found 36 sections and suggested 15 as modules; after apply, the always-loaded file was 13 KB and verify found all 330 lines of the original.

Writing the slim AGENTS.md

The pattern that held up in testing:

  1. Keep the rules that apply every turn, and keep a one-line version of any safety rule even when its module has the detail. A rule should still hold if its module never loads.

  2. List each module with a trigger that names the moment to load it, phrased as "before X" where order matters.

  3. Name the router as the way to load, not one of two options. When the policy offered "call the tool or read the file," the agent read the file in 18 of 20 runs and skipped the sync.

  4. Give the exact call. In clients with tool discovery (goose Code Mode), a policy that left the agent guessing the parameter name cost a failed call and two discovery round trips per load.

  5. Tell delegates which module to load. A subagent gets its own context, so name the module id in its instructions when its task hits a trigger.

See examples/AGENTS.md.

Registry

The registry is a YAML file. The router reads it from ACR_REGISTRY, or from ~/.config/agent-context-router/modules.yaml when that's unset.

repos:
  team-policy:
    remote: git@github.com:your-org/agent-policy.git
    path: ~/src/agent-policy
    branch: main
  local:
    path: .

modules:
  - {id: pr-description, repo: team-policy, file: modules/pr-description.md, trigger: "Writing or updating a PR description"}
  - {id: device-testing, repo: team-policy, file: modules/device-testing.md, trigger: "Before any adb command or device install", profile: work}
  • path may start with ~, and a relative path is resolved from the registry file's directory.

  • A repo without remote is read as it is, with status local.

  • A module with profile loads only on machines with that profile, from AGENT_MACHINE or ~/.agent-machine (work or personal, default work). A module without one loads everywhere.

  • A module's file must stay inside its repo.

Sync

For repos with a remote, the router fetches and fast-forwards only. It never resets, rebases or stashes. When it can't update, it reads the local copy and says why, so the agent can tell you the module may be stale:

  • current, updated: up to date with the remote

  • dirty, ahead, diverged, other-branch, behind: local state it won't overwrite

  • offline: the fetch failed or timed out (15 seconds)

  • missing: no clone at path

Configuration

  • ACR_REGISTRY: registry file

  • ACR_SYNC_WINDOW: seconds between fetches of the same repo (default 1800)

  • ACR_CACHE_DIR: sync state (default ~/.cache/agent-context-router)

  • ACR_LOG: load log (default ~/.local/state/agent-context-router/loads.jsonl)

  • AGENT_MACHINE: machine profile

Development

uv run pytest

tests/harness/run.py is the scenario harness behind the results above. It runs each scenario in an isolated goose root (its own GOOSE_PATH_ROOT, so your sessions and config aren't touched), with fake adb, gradlew and iterm-title that only log their calls. It builds both policies with the author's policy repo, so to run it against your own, point ACR_POLICY_REPO at a repo with a scripts/assemble-goose-policy.sh that honors POLICY_SET=full|slim. ACR_GOOSE_CONFIG sets the goose config to copy provider settings from.

Background

License

Apache 2.0

Available Tools

3 tools
list_modulesList ModulesA

List every policy module available on this machine, with the trigger that says when to load it.

NameRequiredDescriptionDefault

No parameters

Output Schema

NameRequiredDescription
resultYes

TDQS

A3.7/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations exist, so the description carries the full burden. 'List every ... on this machine' usefully discloses exhaustiveness and locality, but says nothing about safety, cost, or failure modes; the mention of the load trigger largely restates what the output schema already shows.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

One front-loaded sentence, no waste. Both clauses (what is listed, what is included) earn their place.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a no-param, read-only enumeration with an output schema, the description covers scope and content adequately. The only gap is routing guidance relative to load_module, which matters because the two are easy to confuse.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Zero parameters, so there is nothing to mis-specify and the baseline of 4 applies. The description correctly implies no filtering or pagination inputs exist.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

Specific verb and resource ('list every policy module') with an explicit scope ('available on this machine'). It reads as distinct from load_module (list vs. load), though it never names siblings directly.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Use is only implied: listing modules so one can be chosen for loading. There is no statement of when to prefer this over load_module or search_context, and no prerequisites are given.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

load_moduleLoad ModuleB

Load a policy module's full text. Call before the work its trigger names.

NameRequiredDescriptionDefault
idYesThe module id from the AGENTS.md module list or list_modules (e.g. "device-testing").

Output Schema

NameRequiredDescription
resultYes

TDQS

B3.4/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the burden. 'Load ... full text' implies a read-only retrieval with no mutation, but it says nothing about module size, cost, or whether repeated loads are cheap. The output schema covers the return value.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Two short sentences, front-loaded with the core action and zero filler. The second sentence is terse to the point of being cryptic, but nothing is wasted.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a one-parameter read tool with an output schema and full schema coverage, most needs are met. The remaining gap is the undefined 'trigger' concept, which the agent cannot resolve from the description alone.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Single parameter with 100% schema coverage; the schema already explains the id source and gives an example value. The description adds nothing about the id beyond what is structured, so the baseline of 3 applies.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

States a specific verb and resource plus the return shape: 'Load a policy module's full text.' That distinguishes it from list_modules (which enumerates) clearly enough, though it never names the sibling explicitly.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

'Call before the work its trigger names' gives a sequencing rule, but 'trigger' is undefined and there is no mention of when not to call it or why search_context would be the alternative. Usage is implied rather than spelled out.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

search_contextSearch ContextB

Find modules relevant to a task when no trigger obviously matches.

NameRequiredDescriptionDefault
queryYesA short description of the work about to happen.
top_kNoMaximum number of modules to return.

Output Schema

NameRequiredDescription
resultYes

TDQS

B3.4/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations exist, so the description carries the full burden. 'Find' implies a non-destructive read, but it does not state that explicitly, nor does it explain ranking/scoring of results, behavior when nothing matches, or any cost/latency traits. The presence of an output schema relieves it of explaining return values.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

A single front-loaded sentence with no waste. It is efficient, though its brevity borders on under-specification rather than optimal conciseness.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

With only 2 params, full schema coverage, and an output schema, much is already covered structurally. However, the description leaves undefined what a 'trigger' or a 'module' contextually is and how discovery should be sequenced, leaving meaningful gaps for a fallback discovery tool.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, so both parameters are fully documented in the schema. The description adds no further meaning about the query or top_k, so the baseline 3 is appropriate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

States a specific verb and resource ('Find modules relevant to a task') plus a qualifying circumstance ('when no trigger obviously matches'), which implicitly contrasts with list_modules (enumerate all) and load_module (load a specific one). It does not name those siblings explicitly, so it falls short of a 5.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The clause 'when no trigger obviously matches' gives one positive usage condition, but it never states when NOT to use this tool or points to the alternatives (list_modules/load_module). Usage is implied rather than prescribed.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

  1. 3 tool updatesv0.1.2
    • First observedlist_modules
    • First observedload_module
    • First observedsearch_context

TDQS

A3.6/5.0

Scored across 3 tools

Disambiguation4/5

List versus search are the closest pair, but the descriptions draw a clear line: list enumerates everything with triggers, search is the fallback for task-relevance lookup when no trigger matches. Load is unambiguously the fetch-the-body step. Only minor overlap in the discovery paths.

Naming Consistency4/5

All three follow a consistent verb_noun snake_case pattern (list_modules, load_module, search_context). The noun shifts from 'module' to 'context' on the third tool, which is a small but defensible deviation since it describes the lookup target rather than the module itself.

Tool Count4/5

Three tools is on the lean side, but each covers a distinct step of a coherent workflow (enumerate, look up by relevance, load). Nothing appears redundant or padding-like for a context router.

Completeness4/5

The core lifecycle of discovering and loading policy modules is covered, including a fallback search path. Minor gaps exist around loaded-state introspection or unloading/refreshing a module, but agents can work around these by re-listing or re-loading.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues

Related MCP Connectors

Related MCP Servers