npxでGitHubで公開している自分のツールをインストールできるようにするまでのエラー解決方法まとめ

## package.jsonの設定にbinがいる

```shell
{
  "name": "ipinfo",
  "version": "1.0.0",
  "main": "index.js",
  "license": "UNLICENSED",
  "bin": { "ipinfo": "servers/ipinfo/dist/index.js" },
  "dependencies": {
    "@modelcontextprotocol/sdk": "^1.10.1",
    "node-fetch": "2",
    "zod": "^3.24.2",
    "zod-to-json-schema": "^3.24.5"
  }
}
```

---

## package.jsonがリポジトリトップにないといけない

### 現象、npxを実行してもなにも返ってこない

```shell
# サブディレクトリにpackage.jsonを置いてうまく動かない例
npx -y github:RyosukeDTomita/mcp_tool_poisoning_attacks/servers/ipinfo
```

### 解決策 リポジトリトップにpackage.jsonを作った

RyosukeDTomita/mcp_tool_poisoning_attacks/servers/ipinfo/package.jsonとは別に、RyosukeDTomita/mcp_tool_poisoning_attacks/package.json
を作った。

参考: [Run your NPX script directly from Github. Create your own CLI commands and other stories](https://dev.to/ipreda/run-your-npx-script-directly-from-github-create-your-own-cli-commands-and-other-stories-4pn3)

## npm error path git

### 現象: gitがないエラーがでる

```shell
npx -y github:RyosukeDTomita/mcp_tool_poisoning_attacks#main ipinfo
npm error code ENOENT
npm error syscall spawn git
npm error path git
npm error errno -2
npm error enoent An unknown git error occurred
npm error enoent This is related to npm not being able to find a file.
npm error enoent
npm error A complete log of this run can be found in: /root/.npm/_logs/2025-04-22T02_59_32_181Z-debug-0.log
Failed to connect to MCP server: McpError: MCP error -32000: Connection closed
    at Client._onclose (/app/node_modules/@modelcontextprotocol/sdk/dist/cjs/shared/protocol.js:97:23)
    at _transport.onclose (/app/node_modules/@modelcontextprotocol/sdk/dist/cjs/shared/protocol.js:72:18)
    at ChildProcess.<anonymous> (/app/node_modules/@modelcontextprotocol/sdk/dist/cjs/client/stdio.js:92:77)
    at ChildProcess.emit (node:events:518:28)
    at maybeClose (node:internal/child_process:1101:16)
    at ChildProcess._handle.onexit (node:internal/child_process:304:5) {
  code: -32000,
  data: undefined
}
node:internal/process/promises:394
    triggerUncaughtException(err, true /* fromPromise */);
    ^
```

### 解決策: gitを入れる

```shell
apt-get update -y
apt-get install -y --no-install-recommends git
```

---

## npm verbose code 128

### 現象

```shell
npx -y --loglevel verbose github:RyosukeDTomita/mcp_tool_poisoning_attacks#main ipinfo
npm exec github:RyosukeDTomita/mcp_tool_poisoning_attacks#main ipinfo
npm verbose argv "exec" "--yes" "--loglevel" "verbose" "--" "github:RyosukeDTomita/mcp_tool_poisoning_attacks#main" "ipinfo"
npm verbose logfile logs-max:10 dir:/root/.npm/_logs/2025-04-22T03_45_00_809Z-
npm verbose logfile /root/.npm/_logs/2025-04-22T03_45_00_809Z-debug-0.log
npm verbose cwd /app
npm verbose os Linux 6.8.0-57-generic
npm verbose node v22.14.0
npm verbose npm  v10.9.2
npm verbose exit 128
npm verbose code 128
```

### 解決策: ca-certificatesをいれる


[npm install git+url exited with error code: 128](https://github.com/npm/npm/issues/16980)のようなissueが見つかった。

試しにgit cloneしてみたら証明書エラーになったことからnpx実行時に同様のエラーがでていると考えられる。

```shell
git clone --branch main https://github.com/RyosukeDTomita/mcp_tool_poisoning_attacks.git
Cloning into 'mcp_tool_poisoning_attacks'...
fatal: unable to access 'https://github.com/RyosukeDTomita/mcp_tool_poisoning_attacks.git/': server certificate verification failed. CAfile: none CRLfile: none
```
なのでca-certificatesを入れる

```shell
apt-get update -y
apt-get install -y --no-install-recommends git ca-certificates
```

---

## 参考
[自分のリポジトリ](https://github.com/RyosukeDTomita/mcp_tool_poisoning_attacks)