From c9f68a4361d0459b3e0b26c45e05201373c3effc Mon Sep 17 00:00:00 2001 From: m3ta-chiron Date: Tue, 10 Nov 2026 20:15:24 +0100 Subject: [PATCH] feat(coding/agents): hand the shared MCP catalogue to Claude Code - Move programs.mcp.servers out of agents.nix into mcp.nix so hosts that do not load the whole coding set (T3 Code servers) can import it alone - Add claude-code.nix: programs.claude-code with enableMcpIntegration, replacing the bare llm-agents.claude-code package (would collide on bin/claude). home-manager delivers the catalogue as a generated plugin, ~/.claude/settings.json and ~/.claude.json stay writable - ai-memory: register the session-aware Claude bridge via programs.claude-code.mcpServers instead of `ai-memory install-mcp`; activation only removes the stale user-scope entry now - Adjust ai-memory.test.sh accordingly Co-Authored-By: Claude Fable 5.1 --- profiles/sets/coding/agents/agents.nix | 44 ++---------------- profiles/sets/coding/agents/ai-memory.nix | 32 ++++++++----- profiles/sets/coding/agents/ai-memory.test.sh | 7 ++- profiles/sets/coding/agents/claude-code.nix | 20 ++++++++ profiles/sets/coding/agents/mcp.nix | 46 +++++++++++++++++++ 5 files changed, 97 insertions(+), 52 deletions(-) create mode 100644 profiles/sets/coding/agents/claude-code.nix create mode 100644 profiles/sets/coding/agents/mcp.nix diff --git a/profiles/sets/coding/agents/agents.nix b/profiles/sets/coding/agents/agents.nix index 36f4717..b1ebceb 100644 --- a/profiles/sets/coding/agents/agents.nix +++ b/profiles/sets/coding/agents/agents.nix @@ -18,50 +18,15 @@ }; imports = [ - # ai-memory client, skill mirror and OpenCode config + # ai-memory client, skill mirror, shared MCP catalogue, OpenCode and + # Claude Code config ./ai-memory.nix ./claude-skills.nix + ./mcp.nix ./opencode.nix + ./claude-code.nix ]; - programs.mcp = { - enable = true; - servers = { - ContextMode = { - command = "bash"; - args = ["-c" "exec bunx context-mode@latest"]; - }; - DeepWiki = { - url = "https://mcp.deepwiki.com/mcp"; - }; - Exa = { - command = "bash"; - args = ["-c" "EXA_API_KEY=$(cat /run/agenix/exa-key) exec bunx exa-mcp-server@latest tools=web_search_exa"]; - }; - SallyAI = { - url = "https://app.sally.io/api/v1/McpExternal"; - }; - Metabase = { - url = "https://kpi.az-gruppe.com/api/metabase-mcp"; - }; - Outline = { - url = "https://wiki.az-gruppe.com/mcp"; - }; - Playwright = { - command = "bash"; - args = ["-c" "exec bunx @playwright/mcp@latest"]; - }; - Ref = { - command = "bash"; - args = ["-c" "REF_API_KEY=$(cat /run/agenix/ref-key) exec bunx ref-tools-mcp@latest"]; - }; - Teams = { - command = "bunx"; - args = ["-y" "@floriscornel/teams-mcp@latest"]; - }; - }; - }; - # NOTE: llm-agent packages are consumed via the `llm-agents` namespace # (pkgs.llm-agents.*), provided by numtide/llm-agents.nix's # `overlays.shared-nixpkgs`. The consumer's nixos-config MUST apply that @@ -72,7 +37,6 @@ agent-lib llm-agents.agent-browser llm-agents.beads - llm-agents.claude-code llm-agents.claude-desktop crunch docker-sbx diff --git a/profiles/sets/coding/agents/ai-memory.nix b/profiles/sets/coding/agents/ai-memory.nix index 6514071..eeb0d40 100644 --- a/profiles/sets/coding/agents/ai-memory.nix +++ b/profiles/sets/coding/agents/ai-memory.nix @@ -3,12 +3,13 @@ # The server runs on m3-hermes (nixos-config) and is reached at # memory.m3ta.dev; no server runs on clients. Hybrid setup: # - Nix owns the package, the client config (server URL + bearer token -# file), the OpenCode MCP entry (via programs.mcp, since opencode.json -# is a home-manager symlink) and the scope markers (~/.ai-memory.toml -# plus one marker per workspace directory). -# - Activation runs ai-memory's own installers for the lifecycle hooks, -# the session-aware Claude Code MCP entry and the skills, so generated -# files always match the installed ai-memory version. +# file), the MCP entries (programs.mcp for OpenCode, and a session-aware +# override in programs.claude-code.mcpServers for Claude Code, both +# delivered through the agents' enableMcpIntegration) and the scope +# markers (~/.ai-memory.toml plus one marker per workspace directory). +# - Activation runs ai-memory's own installers for the lifecycle hooks and +# the skills, so generated files always match the installed ai-memory +# version. # # The bearer token (an `aim_` API key, one per host) is an agenix secret the # host repo provides at `tokenFile`. It is never copied into a generated @@ -100,6 +101,18 @@ in { args = ["mcp-bridge" "--no-session-id" "--server-url" "${cfg.serverUrl}/mcp"]; }; + # Claude Code gets the same server name through + # programs.claude-code.enableMcpIntegration, but session-aware: the stdio + # bridge forwards the lifecycle-hook session id and reads the token file + # itself. (A plain HTTP entry carries no credentials, and Claude Code then + # tries OAuth client registration, which ai-memory does not offer.) + # Claude Code's own mcpServers take precedence over the shared entry, so + # this is one server, not two. + programs.claude-code.mcpServers.ai-memory = mkIf (elem "claude-code" cfg.agents) { + command = exe; + args = ["mcp-bridge" "--server-url" "${cfg.serverUrl}/mcp"]; + }; + home.file = { # Client config: `ai-memory hook`, `mcp-bridge` and the installers @@ -130,13 +143,10 @@ in { '') cfg.agents} ${optionalString (elem "claude-code" cfg.agents) '' + # The MCP entry now comes from programs.claude-code (see above); + # drop the user-scope entry earlier versions wrote into ~/.claude.json. export PATH="${pkgs.llm-agents.claude-code}/bin:$PATH" run claude mcp remove ai-memory -s user >/dev/null 2>&1 || true - # --session-aware: stdio bridge that reads the token file itself. A - # plain HTTP entry carries no credentials, and Claude Code then tries - # OAuth client registration, which ai-memory does not offer. - run ${exe} install-mcp --client claude-code --apply --session-aware \ - --server-url ${escapeShellArg "${cfg.serverUrl}/mcp"} ''} # Into ~/.agents/skills; claude-skills-sync mirrors them to Claude. run ${exe} install-skills --scope global --agent agents diff --git a/profiles/sets/coding/agents/ai-memory.test.sh b/profiles/sets/coding/agents/ai-memory.test.sh index 3bbf81c..52653a6 100755 --- a/profiles/sets/coding/agents/ai-memory.test.sh +++ b/profiles/sets/coding/agents/ai-memory.test.sh @@ -51,7 +51,12 @@ check "hooks opencode2" "$act" "install-hooks --agent opencode2 --apply" check "auth-token symlink" "$act" "ln -sfn /run/agenix/ai-memory-token " refute "no token on command lines" "$act" "--auth-token" refute "activation must not write opencode.json (home-manager symlink)" "$act" "install-mcp --client opencode" -check "claude mcp is the session-aware bridge" "$act" "install-mcp --client claude-code --apply --session-aware" +refute "activation no longer registers the claude mcp itself" "$act" "install-mcp --client claude-code" +check "stale user-scope claude entry is removed" "$act" "claude mcp remove ai-memory -s user" + +claude="$(ev programs.claude-code.mcpServers.ai-memory.args --apply 'builtins.concatStringsSep " "')" +check "claude mcp is the session-aware bridge" "$claude" "mcp-bridge --server-url https://memory.m3ta.dev/mcp" +refute "claude mcp forwards the session id" "$claude" "--no-session-id" # Missing token file: skip with a warning, exit 0, never call ai-memory. script="$(printf '%s\n' "$act" | sed 's|/run/agenix/ai-memory-token|/nonexistent/ai-memory-token|g')" diff --git a/profiles/sets/coding/agents/claude-code.nix b/profiles/sets/coding/agents/claude-code.nix new file mode 100644 index 0000000..138b5c5 --- /dev/null +++ b/profiles/sets/coding/agents/claude-code.nix @@ -0,0 +1,20 @@ +# Claude Code via home-manager `programs.claude-code`. +# +# enableMcpIntegration hands the shared programs.mcp catalogue (mcp.nix) to +# Claude Code the same way opencode.nix does for OpenCode: home-manager +# generates a plugin carrying .mcp.json and registers it (as a personal plugin +# under ~/.claude/skills/claude-code-home-manager on home-manager master, via a +# `--plugin-dir` wrapper on release-26.05). Nothing in ~/.claude is turned +# into a store symlink: settings.json (ai-memory hooks, plugins, theme) and +# ~/.claude.json (logins) stay writable for Claude Code itself. +# +# Do NOT add llm-agents.claude-code to home.packages next to this module; +# programs.claude-code installs its (possibly wrapped) package and a second +# `claude` would collide in the profile. +{pkgs, ...}: { + programs.claude-code = { + enable = true; + package = pkgs.llm-agents.claude-code; + enableMcpIntegration = true; + }; +} diff --git a/profiles/sets/coding/agents/mcp.nix b/profiles/sets/coding/agents/mcp.nix new file mode 100644 index 0000000..16770bc --- /dev/null +++ b/profiles/sets/coding/agents/mcp.nix @@ -0,0 +1,46 @@ +# Shared MCP server catalogue (home-manager `programs.mcp`). +# +# Single source for every agent that integrates with programs.mcp: +# OpenCode (opencode.nix, enableMcpIntegration) and Claude Code +# (claude-code.nix, enableMcpIntegration). ai-memory adds its own entry in +# ai-memory.nix. Hosts that do not load the whole coding set (servers running +# T3 Code) import this file directly. +{...}: { + programs.mcp = { + enable = true; + servers = { + ContextMode = { + command = "bash"; + args = ["-c" "exec bunx context-mode@latest"]; + }; + DeepWiki = { + url = "https://mcp.deepwiki.com/mcp"; + }; + Exa = { + command = "bash"; + args = ["-c" "EXA_API_KEY=$(cat /run/agenix/exa-key) exec bunx exa-mcp-server@latest tools=web_search_exa"]; + }; + SallyAI = { + url = "https://app.sally.io/api/v1/McpExternal"; + }; + Metabase = { + url = "https://kpi.az-gruppe.com/api/metabase-mcp"; + }; + Outline = { + url = "https://wiki.az-gruppe.com/mcp"; + }; + Playwright = { + command = "bash"; + args = ["-c" "exec bunx @playwright/mcp@latest"]; + }; + Ref = { + command = "bash"; + args = ["-c" "REF_API_KEY=$(cat /run/agenix/ref-key) exec bunx ref-tools-mcp@latest"]; + }; + Teams = { + command = "bunx"; + args = ["-y" "@floriscornel/teams-mcp@latest"]; + }; + }; + }; +} -- 2.56.0