{
  lib,
  buildNpmPackage,
  fetchFromGitHub,
  python3,
  fetchPypi,
  makeWrapper,
  nodejs,
  kicad,
  ...
}:

let
  kicad-skip = import ./kicad-skip.nix { inherit lib python3 fetchPypi; };

  pythonEnv = python3.withPackages (
    ps:
    [
      kicad-skip
    ]
    ++ (with ps; [
      cairosvg
      colorlog
      pillow
      pydantic
      python-dotenv
      requests
    ])
  );
in
buildNpmPackage rec {
  pname = "kicad-mcp";
  version = "2.1.0-alpha";

  src = fetchFromGitHub {
    owner = "mixelpixx";
    repo = "KiCAD-MCP-Server";
    rev = "216d3f189d194cdd539b274264b8b7ec00cbb13e";
    hash = "sha256-73yDzYlWMDnnytf/IcUuwQp/P7YYaxrNr+JsGrgMI40=";
  };

  npmDepsHash = "sha256-qR43NvSK28RLt5sL9BZQzSyFRszYMt2CC0wTs9jf/hU=";

  buildScript = "build";

  nativeBuildInputs = [ makeWrapper ];

  buildInputs = [
    pythonEnv
  ];

  postInstall = ''
    mkdir -p $out/bin

    makeWrapper ${nodejs}/bin/node $out/bin/${pname} \
      --add-flags "$out/lib/node_modules/${pname}/dist/index.js" \
      --prefix PATH : ${
        lib.makeBinPath [
          pythonEnv
        ]
      } \
      --prefix PYTHONPATH : "${kicad}/lib/python3/dist-packages"
  '';

  meta = {
    description = "Model Context Protocol server for KiCAD";
    homepage = "https://github.com/mixelpixx/KiCAD-MCP-Server";
  };
}
