Selvedge MCP Server

Change tracking for AI-era codebases — captures the why behind every change as the agent makes it.

Documentation

selvedge

selvedge.sh  ·  PyPI  ·  GitHub

Tests PyPI License: MIT

Long-term memory for AI-coded codebases. A git blame for AI agents — but for the why, not just which line which model touched. Captured live, by the agent, as the change happens.

Selvedge is a local MCP server. AI coding agents (Claude Code, Cursor, Copilot) call it as they work to log structured change events with reasoning. Your data stays in a SQLite file under .selvedge/ next to your code.


Six months ago, your AI agent added a column called user_tier_v2. You don't know why. git blame points to a commit from claude-code with a generated message that says "Update schema." The session that made the change is long gone — and so is the prompt that produced it.

With Selvedge, you run this instead:

$ selvedge blame user_tier_v2

  user_tier_v2
  Changed     2025-10-14 09:31:02
  Agent       claude-code
  Commit      3e7a991
  Reasoning   User asked to add a grandfathering flag for legacy free-tier
              users during the pricing migration. Stores the original tier
              so we can backfill discounts without touching billing history.

That reasoning was captured by the agent in the moment — written into Selvedge from the same context that produced the change. Not inferred from the diff afterward by a second LLM. Not a hand-typed commit message.



Who Selvedge is for

Selvedge has two audiences. Same tool, same pip install, same SQLite file under .selvedge/. Different scale of pain.

Teams running long-term, AI-coded codebases. When the project is big enough that you (or someone else) will touch it again in six months, twelve months, three years — but most of it was written by an agent whose context evaporated the day each PR shipped. git blame tells you what changed. Selvedge tells you why — even after the agent session, the prompt template, the developer who asked for it, and the model version are all long gone. This is the original use case: production codebases, schema decisions, migrations, dependency changes that need an audit trail that survives turnover.

Solo developers using Claude Code on everyday projects. Side projects, weekend builds, the small internal tool you keep poking at. You don't need enterprise governance — you just need to remember why you (or your agent) did the thing you did yesterday, last week, last sprint. Run selvedge init once. Add four lines to your CLAUDE.md. From then on, selvedge blame is muscle memory — a way to talk to your past self when your past self was an LLM.

If you've ever come back to your own AI-built project and thought "what was this for again?", Selvedge is the missing piece.


The problem

Human-written code leaks intent everywhere — commit messages, PR descriptions, inline comments, the Slack thread that preceded it. AI-written code doesn't. The agent has perfect clarity about why it made each decision, but that context lives in the prompt and evaporates when the conversation ends.

Six months later, your team is debugging a schema decision with no trail. git blame tells you what changed and when. It can't tell you why.

Selvedge captures the why — live, by the agent itself, as the change is made. The diff is git's job. The why is Selvedge's.


What's new in v0.3.7

The brand-defining release: prior_attempts — the tool that lets an agent ask "was this tried before, and how did it turn out?" before it starts — plus the entity-canonicalization foundation it sits on. Drop-in upgrade for anyone on 0.3.6.

prior_attempts — the wedge

Given an entity (or a free-text description), prior_attempts returns the prior change attempts on it, each annotated with an inferred outcome (reverted / active), a confidence tier, and — for reverted attempts — the reasoning explaining why it was rejected. Outcome is inferred from add→remove proximity (no LLM, ever — it's a templated query over reasoning the agents wrote live).

Worked example — an agent about to re-add a column it doesn't know was already pulled:

// The agent is asked to keep mobile users signed in across restarts. Its
// instinct is to add a persistent token column. It checks FIRST:
prior_attempts({ "entity_path": "users.auth_token" })

// → one high-confidence hit: this was tried and reverted.
[
  {
    "entity_path": "users.auth_token",
    "change_type": "add",
    "reasoning": "Store a per-user auth token so the app stays signed in.",
    "outcome": "reverted",
    "confidence": "proximity_high",
    "outcome_reasoning": "Reverted: DB tokens couldn't be revoked without a write; moved to short-lived JWTs."
  }
]

The agent reads the rejection reason and changes its plan — a refresh-token endpoint on the stateless-JWT path, instead of re-introducing the column the team already pulled. Full demo transcript.

Conservative by design. min_confidence defaults to proximity_high, so prior_attempts only returns the clear "tried, then reverted" signal — an empty list is the trustworthy "nothing to worry about" answer, not noise. Pass min_confidence="proximity_low" to widen recall. Pull-only: the agent decides when to ask. This is the "alternatives tried, rejected paths" capability the line-attribution tools don't have.

Entity foundation (lands first)

prior_attempts is only as good as the entity matching under it, so v0.3.7 fixes the silent history-split problem first:

  • Canonicalization on write. src/auth.py::login and ./src/auth.py::login used to resolve to different entities. Now every write goes through one chokepoint that strips ./, collapses //, normalizes separators, and trims — preserving case on purpose (filesystems differ; silently lowercasing would collapse genuinely distinct entities on case-sensitive Linux). selvedge doctor warns on sibling paths that differ only by case instead of merging them.
  • selvedge migrate-paths backfills existing rows. Dry-run is the default — it prints a collisions report (pre-canonicalization paths that would merge) so you inspect before committing; pass --apply to write. Idempotent, with an audit row per run.
  • Renames, folded into log_change. Pass rename_from with change_type="rename" and Selvedge records the dual-event pattern (a rename on the old path, a create on the new path with metadata.renamed_from) so history follows the entity. No new tool.
  • Soft entity-path shape warnings — a function path without ::, a column without ., a file without a separator or extension get a nudge, never a rejection.

prior_attempts is the 7th MCP tool (the only one this release adds); the grouped-digest helper ships as a selvedge.aggregates.summary() library function, not a tool. See CHANGELOG.md for the full list, including the 47 new tests — a called-out overrun of the ≤30 per-phase budget (≤40 phase target) because the entity foundation and the wedge ship together and a review pass hardened the acceptance-criteria edges.


What's new in v0.3.6

Two themes in one release as a one-time exception to the single-theme cadence: stay-current (background PyPI version check) and retention basics (selvedge prune for the tool_calls table). Drop-in upgrade for anyone on 0.3.5.

Stay-current

Background version check in selvedge (the CLI only — never the MCP server). A daemon thread fetches the latest published version from PyPI's JSON endpoint, caches the result at ~/.selvedge/update_check.json (user-global so you don't re-check per project), and on process exit prints to stderr:

selvedge: v0.3.7 available (you're on 0.3.6) — https://selvedge.sh/upgrade

The notice is printed via atexit so it appears after the command's output, never interleaved. Cache TTL is 24h, matching gh and npm.

Generous suppression. The check is disabled when any of SELVEDGE_NO_UPDATE_CHECK=1, SELVEDGE_QUIET=1, or CI is set in the environment, when stderr isn't a TTY (piping, agent stdio, --json to a file), and on dev / editable installs. The TTY gate is re-checked at print time too — even a cached notice can't pollute redirected output. The 1.5s fetch timeout and the no-raise posture of every code path mean a network blip can't slow or break the CLI.

selvedge-server stays silent. The check is wired into the CLI group callback only — the MCP server's stdio is the JSON-RPC channel and an inadvertent stderr write would surface in the calling agent's logs as noise.

Retention basics

selvedge prune — trim old tool_calls rows. Default retention is 90 days; --days N overrides. The default is long enough that the previous month's agents are still in the data. Every run appends a tab-separated audit line to .selvedge/prune.log so the cadence is visible later — even empty prunes log, so you can tell the difference between "no prunes yet" and "nothing to prune."

selvedge prune                # 90-day default
selvedge prune --days 30      # tighter window
selvedge prune --json         # for cron / scripting

Only tool_calls is pruned in this release. The destructive events-table path waits for .selvedge/config.toml in v0.3.10 and will require both SELVEDGE_DESTRUCTIVE=1 and an interactive confirmation — the cron / non-interactive --yes footgun is defended against by design.

Doctor — Last prune row + oversized-tool_calls WARN. The doctor table now surfaces the tail of .selvedge/prune.log (most recent timestamp, rows pruned, day threshold) and WARNs when the tool_calls table exceeds 100k rows so users get a nudge to run selvedge prune before the noise table gets large.

Note on cadence

This release combines two themes — a one-time exception to the single-theme-per-release discipline locked in on 2026-05-10. The retention work could have slipped to v0.3.7 (entity foundation + prior_attempts wedge), but combining here avoided a renumbering pass on the phase plan. Single-theme resumes at v0.3.7.

See CHANGELOG.md for the full list including the new tests in test_prune.py, test_update_check.py, and the test_doctor.py extension.


Where Selvedge fits

Where Selvedge fits in the broader AI-coded-codebase tooling stack

AI agents call Selvedge as they work. Selvedge captures the why into a durable, queryable store and emits it back out — as Agent Trace records for cross-tool readers, as observability metadata that links into Sentry/Datadog stack traces, and as compliance artifacts for SOC 2 and EU AI Act audits.

Selvedge does not replace git (line-level what/when), PR review tools (review-time quality), agent observability (LLM call traces), or general-purpose code-host AI features. It sits between them — the provenance-as-first-class-citizen layer that everything else references.


How Selvedge compares

There's a fast-growing "git blame for AI agents" category. Here's where Selvedge fits — and where it deliberately doesn't.

Reasoning sourceGranularityMechanismGroupingStorage
SelvedgeCaptured live, by the agent in the same context that produced the changeEntity — DB column, table, env var, dep, API route, functionMCP server — agent calls it as work happensChangesets — named feature/task slugs across many entitiesSQLite, zero deps
AgentDiffInferred post-hoc by Claude Haiku from the diff at session endLineGit pre/post-commit hookNoneJSONL on disk
OriginCaptured at commit timeLineGit hookNoneLocal
Git AIAttribution metadataLineGit hook + Agent Trace allianceNoneGit notes
BlamePromptPrompt-onlyLineGit hookNoneLocal

Why "captured live" matters. AgentDiff and Origin generate reasoning after the change is made, by feeding the diff back to a second LLM call. Selvedge's reasoning is the agent's own intent, written from the same context window that produced the change — no inference, no hallucinated explanations, and an empty reasoning field is itself a useful signal (the agent didn't have one).

Why "entity-level" matters. Most tools attribute lines. Selvedge attributes things you actually search for: users.email, env/STRIPE_SECRET_KEY, api/v1/checkout, deps/stripe. The first question after git blame is usually "what's the history of this column", not "what's the history of lines 40–48 of users.py".

Why "changesets" matter. A Stripe billing rollout touches the users table, two new env vars, three new API routes, one dependency, and four functions across the codebase. Tag every event with changeset:add-stripe-billing and you can pull the entire scope back later — even if the original PR was broken into eight smaller ones over a month.

Selvedge ↔ Agent Trace. Agent Trace (Cursor + Cognition AI, RFC Jan 2026, backed by Cloudflare, Vercel, Google Jules, Amp, OpenCode, and git-ai) is an emerging open standard for AI code attribution traces. Selvedge isn't a competitor to it — it's a compatible producer. The design for selvedge export --format agent-trace is at docs/agent-trace-interop.md. Agent Trace is the wire format. Selvedge is the live capture + query layer that emits it.


Quickstart

pip install selvedge
cd your-project
selvedge setup

That's it. selvedge setup is an interactive wizard: it detects which AI tools you have (Claude Code, Cursor, Copilot), writes the MCP entry into each one's config, drops the canonical agent-instructions block into your project's prompt file (CLAUDE.md / .cursorrules / copilot-instructions.md), runs selvedge init, and installs the post-commit hook. Every modified file gets a .bak written next to it before any change reaches disk. Re-running is a no-op.

For CI bootstrap or devcontainer.json postCreateCommand:

selvedge setup --non-interactive --yes

Claude Code plugin marketplace (alternative)

If you're a Claude Code user and want to install Selvedge through the official plugin marketplace flow, run these inside Claude Code after pip install selvedge:

/plugin marketplace add masondelan/selvedge
/plugin install selvedge@selvedge

The plugin system wires the MCP server into Claude Code, but it does not install the Python package for you — pip install selvedge first, otherwise the selvedge-server command won't exist on your PATH and the plugin can't start. For the full setup (post-commit hook, project CLAUDE.md instructions block, etc.), selvedge setup is still the recommended path; the plugin marketplace install is just the lightweight Claude-Code-only entry point.

Verify the wiring — open a second terminal in the same project:

selvedge watch

Make any change in your AI tool — add a column, rename a function, add an env var. selvedge watch should print the new event within a second of the agent calling log_change. If nothing arrives, run selvedge doctor for a single-command health check that tells you which step is silently broken.

Query your history:

selvedge status                        # recent activity + missing-commit count
selvedge diff users                    # all changes to the users table
selvedge diff users.email              # changes to a specific column
selvedge blame payments.amount         # what changed last and why
selvedge history --since 30d           # last 30 days of changes
selvedge history --since 15m           # last 15 minutes ('m' = minutes)
selvedge changeset add-stripe-billing  # all events for a feature/task
selvedge search "stripe"               # full-text search
selvedge stats                         # log_change coverage report (per-agent)
selvedge import migrations/            # backfill from migration files
selvedge export --format csv           # dump history to CSV
Manual install — if you'd rather wire it up yourself

If you don't want to run the wizard, the four manual steps it automates:

1. Initialize in your project

cd your-project
selvedge init

2. Add to your Claude Code config

~/.claude/config.json:

{
  "mcpServers": {
    "selvedge": {
      "command": "selvedge-server"
    }
  }
}

For Cursor: ~/.cursor/mcp.json. For Copilot: .github/copilot-instructions.md (different format — see selvedge prompt --help).

3. Tell your agent to use it

selvedge prompt --install CLAUDE.md

This installs the canonical agent-instructions block, sentinel-bracketed (<!-- selvedge:start --> / <!-- selvedge:end -->) so future --install calls update the bracketed region without disturbing anything else in the file. Or pipe it:

selvedge prompt | tee -a CLAUDE.md

4. Install the post-commit hook

selvedge install-hook

That's the same four steps the wizard runs.


How it works

Selvedge runs as an MCP server. AI agents in tools like Claude Code call Selvedge's tools as they work — logging structured change events to a local SQLite database.

Each event records:

  • What changed (entity path, change type, diff)
  • When (timestamp)
  • Who (agent, session ID)
  • Why (reasoning — captured from the agent's context in the moment)
  • Where (git commit, project)

The diff is git's job. The why is Selvedge's.


Entity path conventions

users.email           DB column (table.column)
users                 DB table
src/auth.py::login    Function in a file (path::symbol)
src/auth.py           File
api/v1/users          API route
deps/stripe           Dependency
env/STRIPE_SECRET_KEY Environment variable

Prefix queries work everywhere: users returns users, users.email, users.created_at, and any other entity under the users. namespace.


MCP tools

When connected as an MCP server, Selvedge exposes:

ToolDescription
log_changeRecord a change event with entity, diff, and reasoning (pass rename_from with change_type="rename" for the dual-event rename pattern)
diffHistory for an entity or entity prefix
blameMost recent change + context for an exact entity
historyFiltered history across all entities
changesetAll events grouped under a named feature/task slug
searchFull-text search across all events
prior_attemptsPrior change attempts on an entity + inferred outcome (was it tried and reverted?) — call it before editing

CLI reference

selvedge init [--path PATH]               Initialize in project
selvedge status                           Recent activity summary
selvedge diff ENTITY [--limit N]          Change history for entity
selvedge blame ENTITY                     Most recent change + context
selvedge history [--since SINCE]          Browse all history
              [--entity ENTITY]
              [--project PROJECT]
              [--changeset CS]
              [--summarize]
              [--limit N]
selvedge changeset [CHANGESET_ID]         Show events in a changeset
                  [--list]                or list all changesets
                  [--project NAME]
                  [--since SINCE]
selvedge search QUERY [--limit N]         Full-text search
selvedge stats [--since SINCE]            Tool call coverage report (per-tool, per-agent)
selvedge doctor [--json]                  Health check: DB path, schema, hook, MCP wiring
selvedge install-hook [--path PATH]       Install git post-commit hook
                     [--window MIN]       (default 60 minutes)
selvedge backfill-commit --hash HASH      Backfill git_commit on recent events
                        [--window MIN]    (default 60 minutes)
selvedge import PATH                      Import migration files (SQL / Alembic)
              [--format auto|sql|alembic]
              [--project NAME]
              [--dry-run]
selvedge export [--format json|csv]       Export history to JSON or CSV
              [--since SINCE]
              [--entity ENTITY]
              [--output FILE]
selvedge log ENTITY CHANGE_TYPE           Manually log a change
             [--diff TEXT]                CHANGE_TYPE: add, remove, modify,
             [--reasoning TEXT]           rename, retype, create, delete,
             [--agent NAME]               index_add, index_remove, migrate
             [--commit HASH]
             [--project NAME]
             [--changeset CS]
             [--rename-from OLD]          OLD path when CHANGE_TYPE is 'rename'
selvedge migrate-paths                    Re-canonicalize stored entity paths
                      [--apply]           (dry-run by default; --apply writes)
                      [--json]

All read commands support --json for machine-readable output.

Relative time in --since:

  • 15m → last 15 minutes (m = minutes)
  • 24h → last 24 hours
  • 7d → last 7 days
  • 5mo → last 5 months (mo or mon = months)
  • 1y → last year

Unparseable inputs (e.g. --since yesterday) exit with a clear error rather than silently returning empty results. ISO 8601 timestamps are also accepted and normalized to UTC.


Configuration

MethodFormatExample
Env varSELVEDGE_DB=/path/to/dbPer-session override
Project initselvedge initCreates .selvedge/selvedge.db in CWD
Global fallback~/.selvedge/selvedge.dbUsed if no project DB found

Coverage checking

Wondering how often your agent actually calls log_change? Two ways to check:

# Quick summary in the terminal
selvedge stats

# Cross-reference against git commits
python scripts/coverage_check.py --since 30d

The coverage script compares your git log against Selvedge events and shows which commits have associated change events. Low coverage usually means the system prompt needs strengthening — see docs/fallbacks.md for guidance.


Contributing

git clone https://github.com/masondelan/selvedge
cd selvedge
pip install -e ".[dev]"
pytest

See CLAUDE.md for architecture details and the phase roadmap.


License

MIT — see LICENSE.