# Config files

Agents keep their settings in files people edit by hand: `~/.claude.json`, `opencode.jsonc`, `~/.codex/config.toml`. skillrc changes one key in such a file and leaves the rest as it was: comments, key order, layout, a byte order mark, line endings and the file's mode.

```ts
import { editConfigFile } from "skillrc/io";

const result = editConfigFile(places, "/home/me/.claude.json", () => [
  { path: ["mcpServers", "github"], value: { command: "npx", args: ["-y", "github-mcp"] } },
]);
// { changed: true }
```

## Edit a JSON or JSONC file

`editConfigFile(places, file, plan, options?)` reads the file and calls `plan` with the object it holds, or `undefined` when the file is missing. `plan` returns the edits. skillrc places each one in the text and writes the file once.

```ts
import { editConfigFile, valueAt } from "skillrc/io";

editConfigFile(places, "/home/me/.claude.json", (root) =>
  valueAt(root, ["mcpServers", "old"]) === undefined
    ? []
    : [{ path: ["mcpServers", "old"], value: undefined }],
);
```

An empty list writes nothing. Decide inside `plan`, not before the call: it gets the object read under the lock, so no other write slips in between.

A `.jsonc` file is read as JSONC, which allows comments and trailing commas. Any other file is read as strict JSON. Pass `format` to choose.

### Key edits

A `KeyEdit` names a key path from the root and the value to put there.

| Edit | What it does |
| --- | --- |
| `{ path, value }` | Sets the value. Missing objects on the way are created. |
| `{ path, value: undefined }` | Removes the key, with its lines and any comment that trails it |
| `{ path, item, value }` | Replaces the item at `item` in the list at `path`. An index equal to the list's length adds one. |
| `{ path, item, value: undefined }` | Removes the item |
| `{ path, item, insert: true, value }` | Puts the value before the item at `item` |

A new member copies the layout of its neighbours. In a one-line object it goes on the same line; in a multi-line object it gets its own lines at the same indent.

```jsonc [before]
{
  "theme": "dark", // mine
  "mcpServers": {}
}
```

```jsonc [after]
{
  "theme": "dark", // mine
  "mcpServers": {
    "github": {
      "command": "npx"
    }
  }
}
```

After the edits, skillrc parses the result and checks that it holds exactly the edited value. If not, nothing is written and `error` says the edit could not be placed.

### The result

| Field | When it is set |
| --- | --- |
| `changed` | The file changed, or would in a dry run |
| `skipped` | Not done this time, and worth trying again: the lock was busy, the file changed meanwhile, or the file is missing and `create` is off |
| `error` | Not done: the file doesn't parse, it repeats a key on the path, or the edit can't be placed |

`editConfigFile` doesn't throw for a file it can't edit, so a sync can report it and go on.

### Options

| Option | Default | What it does |
| --- | --- | --- |
| `format` | By extension | `"json"` or `"jsonc"` |
| `create` | `false` | Write a missing file, starting from `{}` |
| `mode` | `0o600` | The mode of a file this creates. An existing file keeps its own. |
| `dryRun` | `false` | Return whether the file would change, and write nothing |
| `lock` | `true` | Hold `<file>.lock` while editing. Never taken in a dry run. |
| `staleMs` | `10_000` | How old a lock may be before it is taken as left by a dead process |

## Locks and races

Claude Code writes its files through proper-lockfile, which holds a `<file>.lock` folder. `editConfigFile` takes the same folder, so the two never write at once. A lock older than 10 seconds is stale and broken.

The new text goes to a temporary file, then renames over the file only if the file still holds what was read. A program that writes without the lock between that check and the rename still loses its write.

A file that is a link is edited at its target. A link to nothing gets an `error` and is left alone.

`lockFile(file, staleMs?)` takes the lock on its own. It returns the release, or `undefined` while another holder has it. The [TOML](https://skillrc.loadsheet.dev/guide/config-files#toml) example below uses it.

## Read without writing

`readConfigFile(file, format?)` returns `{ root }`, `{ error }` for a file that doesn't parse, or `{}` for a missing file. `valueAt(root, path)` walks a key path and returns `undefined` at the first missing key.

```ts
import { readConfigFile, valueAt } from "skillrc/io";

const { root } = readConfigFile("/home/me/.claude.json");
const servers = valueAt(root, ["mcpServers"]);
```

## TOML

Codex and Grok keep MCP servers in TOML, one `[mcp_servers.<name>]` table each. `setTomlTable(places, text, table, name, value)` returns the text with that table and its subtables set, or removed when `value` is `undefined`.

```ts
import { readTomlTable, setTomlTable } from "skillrc/io";

const edited = setTomlTable(places, text, "mcp_servers", "github", {
  command: "npx",
  args: ["-y", "github-mcp"],
});
if ("refused" in edited) throw new Error(edited.refused);
```

```toml
[mcp_servers.github]
command = "npx"
args = [ "-y", "github-mcp" ]
```

It writes header tables only. It refuses, with the reason, text that doesn't parse, an entry written inline or with dotted keys, and a result that would change another table. A parse error says where, not what: the text could hold a secret.

`readTomlTable(text, table, name)` reads one entry back, and throws when the text doesn't parse.

`setTomlTable` works on text. To edit a file, hold the lock and write only if the file is unchanged, as `editConfigFile` does:

```ts
import { lockFile, readText, replaceIfUnchanged, setTomlTable } from "skillrc/io";

const file = "/home/me/.codex/config.toml";
const release = lockFile(file);
if (release) {
  try {
    const before = readText(file);
    const edited = setTomlTable(places, before ?? "", "mcp_servers", "github", server);
    if ("text" in edited) replaceIfUnchanged(places, file, before, edited.text, 0o600);
  } finally {
    release();
  }
}
```

`replaceIfUnchanged` returns `false` when the file changed after `readText`.

## Hand-written manifests

`editJsonText(text, before, after)` edits JSON text that parses to `before` so it parses to `after`, changing only the parts that differ. A one-line array stays on one line. Use it for a file your program owns and people edit, such as its own manifest.

```ts
import { editJsonText } from "skillrc/io";

const out = editJsonText(text, before, { ...before, agents: [...before.agents, "codex"] });
```

It returns `undefined` for text it can't follow, such as comments. Write the whole file then.

<!-- vitehub-docs:section-links -->
## In this section

- [Introduction](https://skillrc.loadsheet.dev/raw/guide.md)
- [Installation](https://skillrc.loadsheet.dev/raw/guide/installation.md)
- [Config files](https://skillrc.loadsheet.dev/raw/guide/config-files.md) (this page)
- [Fetching](https://skillrc.loadsheet.dev/raw/guide/fetching.md)
- [Definitions](https://skillrc.loadsheet.dev/raw/guide/definitions.md)
- [Sync](https://skillrc.loadsheet.dev/raw/guide/sync.md)
- [Coding agents](https://skillrc.loadsheet.dev/raw/guide/agents.md)

Next: [Fetching](https://skillrc.loadsheet.dev/raw/guide/fetching.md)

All pages: [llms.txt](https://skillrc.loadsheet.dev/llms.txt)

Something on this page does not work as written? Tell the user, and draft an issue for them to post: [issue tracker](https://github.com/words-with-agents/skillrc/issues). Report a security vulnerability privately, never in a public issue.
