Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(init): register xerj mcp in Codex's project config (#1682)

xerj-org275b54afd0c0
diff --git a/engine/crates/xerj-autoindex/src/init.rs b/engine/crates/xerj-autoindex/src/init.rs
index 528bd1fb8..3121ebf09 100644
--- a/engine/crates/xerj-autoindex/src/init.rs
+++ b/engine/crates/xerj-autoindex/src/init.rs
@@ -50,7 +50,9 @@ config for each — with a .bak backup for any file it edits:
.cursor/mcp.json the same registration for Cursor (if .cursor/)
.claude/skills/xerj/SKILL.md teach `xerj search` / `xerj def` (10 lines)
{hub_skill} .cursor/rules/xerj.mdc same teaching file for Cursor (if .cursor/)
-{hub_rule} AGENTS.md a short XERJ section (appended if the file
+{hub_rule} .codex/config.toml register `xerj mcp` for Codex (if .codex/;
+ trusted projects only; other servers kept)
+ AGENTS.md a short XERJ section (appended if the file
exists, created otherwise)
The teaching files carry an `xerj-init-version` marker. A re-run upgrades one
@@ -210,6 +212,8 @@ shell PATH. `command -v xerj` prints it (PowerShell: `(Get-Command xerj).Source`
- VS Code (`.vscode/mcp.json`): root key `servers`, not `mcpServers`. A block under the wrong key is
silently ignored.
`{"servers": {"xerj": {"type": "stdio", "command": "/abs/path/to/xerj", "args": ["mcp"]}}}`
+- Codex (`.codex/config.toml`, trusted projects only; `xerj init` writes it when `.codex/` exists):
+ `[mcp_servers.xerj]` with `command`, `args = ["mcp"]`, and `[mcp_servers.xerj.env]` `XERJ_URL`.
"##;
/// The MCP tool whose presence proves `xerj mcp` is registered. It must be a
@@ -247,9 +251,10 @@ fn mcp_registration(bin: &str, url: &str) -> String {
format!(
"MCP registration for other clients (restart the session after; success: `{MCP_SUCCESS_TOOL}` is in the tool list):\n\
\x20 Claude Code, every project: claude mcp add --scope user xerj{env} -- {} mcp\n\
- \x20 Cursor: `.cursor/mcp.json` when that directory exists (init writes it; root key \"mcpServers\")\n\
- \x20 Windsurf, Claude Desktop, Cline: copy the \"xerj\" entry from .mcp.json; the root key is \"mcpServers\"\n\
- \x20 VS Code (.vscode/mcp.json): root key \"servers\", not \"mcpServers\"; the entry needs \"type\": \"stdio\"",
+ \x20 Cursor: `.cursor/mcp.json` when that directory exists (init writes it; root key \"mcpServers\")\n\\
+ \x20 Windsurf, Claude Desktop, Cline: copy the \"xerj\" entry from .mcp.json; the root key is \"mcpServers\"\n\\
+ \x20 VS Code (.vscode/mcp.json): root key \"servers\", not \"mcpServers\"; the entry needs \"type\": \"stdio\"\n\\
+ \x20 Codex (.codex/config.toml, trusted projects only; xerj init writes it when .codex/ exists): [mcp_servers.xerj]",
shell_quote(bin)
)
}
@@ -285,7 +290,7 @@ fn self_bin() -> String {
/// `skill_md()` or `HUB_SKILL_MD` changes, so a file an older init wrote (and
/// the user never edited) is upgraded on the next run. A unit test pins the
/// texts' hashes to this number, so a text change without a bump fails CI.
-const INIT_FILES_VERSION: u32 = 3;
+const INIT_FILES_VERSION: u32 = 4;
/// The marker's prefix. The full line is
/// `<!-- xerj-init-version: <N> sha256:<16 hex> -->`: the generation, and the
@@ -451,6 +456,66 @@ fn merged_mcp_json(existing: Option<&str>, url: &str) -> Result<String, String>
serde_json::to_string_pretty(&root).map_err(|e| e.to_string())
}
+/// A TOML basic string. `command` and `XERJ_URL` are the only values init
+/// writes, and either can contain `"` or `\`.
+fn toml_basic_string(s: &str) -> String {
+ format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\""))
+}
+
+/// True when the file already has a xerj server table. A comment that merely
+/// mentions the header does not count. No TOML parser: xerj-autoindex does
+/// not depend on one, and a present header is enough to leave the file alone.
+fn codex_has_xerj(text: &str) -> bool {
+ text.lines().any(|line| {
+ let t = line.trim();
+ !t.starts_with('#') && (t == "[mcp_servers.xerj]" || t.starts_with("[mcp_servers.xerj."))
+ })
+}
+
+fn codex_mcp_block(url: &str) -> String {
+ format!(
+ "[mcp_servers.xerj]\ncommand = {}\nargs = [\"mcp\"]\n\n[mcp_servers.xerj.env]\nXERJ_URL = {}\n",
+ toml_basic_string(&self_bin()),
+ toml_basic_string(url)
+ )
+}
+
+/// Append `[mcp_servers.xerj]` to Codex's project config. The directory must
+/// already exist; this does not create `.codex/`.
+fn write_codex_config(path: &Path, url: &str, dry: bool) -> Result<String, String> {
+ let existing = match fs::read_to_string(path) {
+ Ok(text) => Some(text),
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
+ Err(e) => {
+ return Err(format!(
+ "could not read {} ({e}); not touching it",
+ path.display()
+ ))
+ }
+ };
+ if existing.as_deref().is_some_and(codex_has_xerj) {
+ return Ok(report("kept", path));
+ }
+ let block = codex_mcp_block(url);
+ let merged = match existing {
+ Some(text) if text.is_empty() => block,
+ Some(text) => {
+ let sep = if text.ends_with("\n\n") {
+ ""
+ } else if text.ends_with('\n') {
+ "\n"
+ } else {
+ "\n\n"
+ };
+ format!("{text}{sep}{block}")
+ }
+ None => block,
+ };
+ let had = backup(path, dry)?;
+ write_file(path, &merged, dry).map_err(|e| e.to_string())?;
+ Ok(report(if had { "appended" } else { "wrote" }, path))
+}
+
/// One detected surface, written. Returns the human line to print.
///
/// A dry run used to say `wrote` for a file it did not create. The verb
@@ -598,7 +663,19 @@ pub(crate) fn run_init_with(
}
}
- // 4) AGENTS.md — the cross-agent convention. Append a short section if the
+ // 4) Codex — only when the project already has a `.codex` directory.
+ // Project MCP is `[mcp_servers.xerj]` in `.codex/config.toml`, and
+ // Codex loads that file only for a trusted project. Init does not
+ // create `.codex/`.
+ if cwd.join(".codex").is_dir() {
+ out.push(write_codex_config(
+ &cwd.join(".codex/config.toml"),
+ url,
+ dry,
+ )?);
+ }
+
+ // 5) AGENTS.md — the cross-agent convention. Append a short section if the
// file exists without one; create the file if absent.
let agents = cwd.join("AGENTS.md");
match fs::read_to_string(&agents) {
@@ -629,7 +706,7 @@ pub(crate) fn run_init_with(
}
}
- // 5) Auth note — say plainly what will happen at tool-call time (#961).
+ // 6) Auth note — say plainly what will happen at tool-call time (#961).
//
// `.mcp.json` is Claude Code *project* scope, i.e. a file meant to be
// committed, so init deliberately writes no credential into it; auth is
@@ -717,6 +794,72 @@ mod tests {
);
let agents = fs::read_to_string(d.path().join("AGENTS.md")).unwrap();
assert!(agents.contains("xerj def"));
+ assert!(
+ !d.path().join(".codex").exists(),
+ "init must not create .codex/"
+ );
+ }
+
+ /// Codex reads `[mcp_servers.xerj]` in `.codex/config.toml`, and only in a
+ /// trusted project. Init appends that table when `.codex/` already exists
+ /// and leaves every other line in place.
+ #[test]
+ fn codex_config_is_appended_only_when_the_directory_exists() {
+ assert_eq!(toml_basic_string("a\"b\\c"), "\"a\\\"b\\\\c\"");
+
+ let bare = tmp();
+ run_init_in(bare.path(), "http://127.0.0.1:9200", false).unwrap();
+ assert!(!bare.path().join(".codex").exists());
+
+ let d = tmp();
+ fs::create_dir(d.path().join(".codex")).unwrap();
+ fs::write(d.path().join(".codex/config.toml"), "model = \"gpt-5\"\n").unwrap();
+ let lines = run_init_in(d.path(), "http://127.0.0.1:9200", false).unwrap();
+ assert!(
+ has_report(&lines, "appended", ".codex/config.toml"),
+ "{lines:?}"
+ );
+ let text = fs::read_to_string(d.path().join(".codex/config.toml")).unwrap();
+ assert!(text.starts_with("model = \"gpt-5\"\n"), "{text}");
+ assert!(text.contains("[mcp_servers.xerj]\n"), "{text}");
+ assert!(text.contains("args = [\"mcp\"]\n"), "{text}");
+ assert!(text.contains("[mcp_servers.xerj.env]\n"), "{text}");
+ assert!(
+ text.contains("XERJ_URL = \"http://127.0.0.1:9200\"\n"),
+ "{text}"
+ );
+ assert!(d.path().join(".codex/config.toml.bak").exists());
+
+ let again = run_init_in(d.path(), "http://127.0.0.1:9", false).unwrap();
+ assert!(
+ has_report(&again, "kept", ".codex/config.toml"),
+ "{again:?}"
+ );
+ assert_eq!(
+ fs::read_to_string(d.path().join(".codex/config.toml")).unwrap(),
+ text
+ );
+
+ let commented = tmp();
+ fs::create_dir(commented.path().join(".codex")).unwrap();
+ fs::write(
+ commented.path().join(".codex/config.toml"),
+ "# [mcp_servers.xerj]\n",
+ )
+ .unwrap();
+ run_init_in(commented.path(), "http://127.0.0.1:9200", false).unwrap();
+ let commented_text =
+ fs::read_to_string(commented.path().join(".codex/config.toml")).unwrap();
+ assert_eq!(
+ commented_text.matches("[mcp_servers.xerj]").count(),
+ 2,
+ "a comment is not a section: {commented_text}"
+ );
+
+ let dry = tmp();
+ fs::create_dir(dry.path().join(".codex")).unwrap();
+ run_init_in(dry.path(), "http://127.0.0.1:9200", true).unwrap();
+ assert!(!dry.path().join(".codex/config.toml").exists());
}
#[test]
@@ -752,7 +895,7 @@ mod tests {
let second = fs::read_to_string(d.path().join("AGENTS.md")).unwrap();
assert_eq!(first, second, "AGENTS.md must not grow on re-run");
// "kept" for every surface; the only other line init emits is the
- // auth note (section 5), which rewrites nothing.
+ // auth note (section 6), which rewrites nothing.
assert!(
lines
.iter()
@@ -1105,6 +1248,7 @@ mod tests {
".claude/skills/xerj-hub/SKILL.md",
".cursor/rules/xerj.mdc",
".cursor/rules/xerj-hub.mdc",
+ ".codex/config.toml",
"AGENTS.md",
] {
assert!(with_hub.contains(file), "--help does not list {file}");
@@ -1121,6 +1265,7 @@ mod tests {
".mcp.json",
".claude/skills/xerj/SKILL.md",
".cursor/rules/xerj.mdc",
+ ".codex/config.toml",
"AGENTS.md",
] {
assert!(no_hub.contains(file), "--help does not list {file}");
@@ -1302,7 +1447,7 @@ mod tests {
content_hash(&skill_md()).as_str(),
content_hash(HUB_SKILL_MD).as_str()
),
- (3, "c59537bc1064a30f", "6040c97600edc004"),
+ (4, "c59537bc1064a30f", "131aa9224f56d182"),
"a teaching text changed: bump INIT_FILES_VERSION and re-pin"
);
}