{ lib, python3Packages, fetchFromGitHub, }: python3Packages.buildPythonApplication rec { pname = "magg"; version = "1.3.0"; pyproject = true; # Upstream emits an alias tag for every release that points at the same commit # (v1.3 -> 1b18b87fdcda, the very commit v1.3.0 peels to) plus a moving # `latest-publish`/`latest-release` tag, a `root` tag and legacy # `magg/v0.5.1.53`-style names. nix-update normally picks the dotted tag, but # eyeball every bump so the version does not silently degrade to "1.3". src = fetchFromGitHub { owner = "sitbon"; repo = "magg"; rev = "v${version}"; hash = "sha256-Dj8f+MwefUD5wk4JFTkrC72QGqfJoWQd8ToOeNI6rmM="; }; # PEP 621 + hatchling (pyproject.toml [build-system]). Pure Python: the wheel # is py3-none-any, needs no compiler, and `[build-system] requires = # ["hatchling"]` is unpinned, so no pin-relaxing patch is needed. build-system = with python3Packages; [ hatchling ]; # The 11 runtime imports of [project.dependencies], each present in nixpkgs and # at or above upstream's floor (fastmcp 3.4.7, mcp 1.29.0, aiohttp 3.14.3, # pydantic 2.13.4, pydantic-settings 2.12.0, rich 15.0.0, prompt-toolkit 3.0.52, # cryptography 50.0.0, pyjwt 2.13.0, watchdog 6.0.0, art 6.5). Upstream commits # only `uv.lock`, which Nix does not consume, so there is no vendorHash: the # dependency closure comes from python3Packages and the wheel build is offline. dependencies = with python3Packages; [ aiohttp art cryptography fastmcp mcp prompt-toolkit pydantic pydantic-settings pyjwt rich watchdog ]; # [project.optional-dependencies] test = pytest + pytest-asyncio; pytest-asyncio # is mandatory because ~180 async tests carry @pytest.mark.asyncio and its strict # mode silently skips unmarked coroutines. pytestCheckHook only contributes # pytest itself (see dontUsePytestCheck below). The `dev` dependency-group extras # (keyring, anthropic, packaging, ruff) are release/lint tooling and are # deliberately not pulled in. nativeCheckInputs = with python3Packages; [ pytestCheckHook pytest-asyncio ]; doCheck = true; # mcp's stdio client starts every backend with a hand-picked environment: # mcp/client/stdio/__init__.py `get_default_environment()` keeps only HOME, # LOGNAME, PATH, SHELL, TERM and USER, so PYTHONPATH never reaches a backend # process. Upstream's CI runs pytest from a uv venv, where `python` and # `sys.executable` already carry the dependencies in their own site-packages; # run the suite on nixpkgs' plain interpreter and the 12 tests that mount a # real stdio backend (test_in_memory.py, test_mounting_real.py, # test_client_mounting.py, test_server_robustness.py, test_transport.py) die # with "ModuleNotFoundError: No module named 'fastmcp'" and then report the # resulting "Connection closed" as a missing tool. Two of them # (test_transport.py:36,51) additionally assert that the resolved interpreter # ends in "python"/"python3", which the store path # -python3.14.7/bin/python3.14 never does. # # So recreate the venv layout instead of skipping those tests, and run pytest # from it. pytestCheckHook cannot do this: its phase hard-codes the unwrapped # interpreter path, and it ignores the obsolete `pytestOptions` attribute # entirely (it only reads pytestFlags/pytestFlagsArray and the # enabled/disabledTestMarks lists), which is why the marker deselection is # spelled out in the command line below. dontUsePytestCheck = true; checkPhase = '' runHook preCheck # `--without-pip`: the sandbox has no network and nothing is installed into # this venv. The .pth mirrors PYTHONPATH (which pypa-install-hook has already # extended with $out/lib/python3.14/site-packages) into the venv's own # site-packages, i.e. into sys.path entries that survive the stripped child # environment, so a backend spawned as `sys.executable server.py` can import # magg, fastmcp and friends without any environment variable. venv="$TMPDIR/magg-check/venv" mkdir -p "$venv" python -m venv --without-pip "$venv" # Written with Python's os.linesep rather than `tr ':' '\n'`: that backslash # escape does not survive the Nix indented string verbatim (the .pth came out # joined by literal backslashes instead of newlines, so site.py never split # it), and a here-doc for one line would be more noise than signal. python -c 'import os; print(os.environ["PYTHONPATH"].replace(":", os.linesep))' \ > "$venv/lib/${python3Packages.python.libPrefix}/site-packages/nix-pythonpath.pth" # First on PATH so the tests that mount a backend through # NoValidatePythonStdioTransport, whose python_cmd defaults to the bare string # "python" (test_mounting_real.py:44, test_client_mounting.py:41), pick the # same venv interpreter. export PATH="$venv/bin:$PATH" # `-m "not integration"`: the three integration tests (test/magg/test_basic.py:97, # test_e2e_mounting.py:18, test_e2e_simple.py:16) boot a real HTTP backend on # the hard-coded localhost:54321/54322 and poll it for up to 10 s # (test_e2e_simple.py:65,91). That fixed port plus wall-clock polling is the # only flake source in an otherwise network-free suite (the registry/glama/ # github/npm discovery clients are monkeypatched in the unit tests), so every # remaining test is selected and runs for real. A handful of them skip # themselves: upstream capability guards in test/magg/test_client_api.py # probe whether the installed fastmcp exposes a given Client API variant # (command keyword syntax, positional command syntax, WebSocket URLs) and # report a skip when it does not - upstream's own skips, not a packaging # defect. No test count is quoted here on purpose: the suite grows on almost # every bump, so any hard-coded number rots immediately. Drop the marker # filter once upstream picks an ephemeral port. "$venv/bin/python" -m pytest test/magg -m "not integration" runHook postCheck ''; # Two plausible sandbox hardenings are deliberately absent: # - MAGG_AUTO_RELOAD=false (the config-reload watcher otherwise starts poller # threads): pydantic-settings reads that variable into the config default, so # pinning it breaks the 5 tests that assert the shipped default - 2 in # test_config_safety.py and 3 in test_config_reload.py. # - a pinned HOME/TMPDIR: the suite only reads $HOME for the ~/.magg and # ~/.ssh/magg defaults (test_config.py:95, test_auth.py:25, # test_config_safety.py:76) and keeps scratch state in tempfile directories, # so the sandbox defaults (HOME=/homeless-shelter, TMPDIR=/build) are enough - # verified by running the suite with HOME=/homeless-shelter. # Importing magg pulls in fastmcp/mcp/aiohttp/cryptography, so this still # catches a cut transitive dependency if the check phase is ever disabled. pythonImportsCheck = [ "magg" ]; passthru = { category = "MCP Servers"; updateScript = [ "nix-update" "--flake" ".#magg" ]; }; meta = { description = "MCP aggregator server that manages, aggregates and proxies other MCP servers so LLMs can discover, add and manage tools at runtime"; homepage = "https://github.com/sitbon/magg"; changelog = "https://github.com/sitbon/magg/releases"; # pyproject.toml `license = {text = "AGPL-3.0-or-later"}`, matching license.md # (the verbatim AGPLv3 text, shipped as license.md rather than LICENSE). license = lib.licenses.agpl3Plus; sourceProvenance = with lib.sourceTypes; [ fromSource ]; # [project.scripts] installs `magg` (magg.cli:main) and `mbro` # (magg.mbro.cli:main); `magg` is the documented entry point. mainProgram = "magg"; # Pure py3-none-any wheel, "Operating System :: OS Independent" classifier and # no platform markers in the dependency set. The SIGHUP config reload is # guarded by hasattr(signal, "SIGHUP") and the interactive mbro REPL degrades # on non-TTY stdout, so nothing is Unix-only in practice. platforms = lib.platforms.all; maintainers = [ ]; }; }