Closed21

Claude Code の MCP server としての使用感を試す

ピン留めされたアイテム
snakasnaka

Claude Code を MCP サーバとして利用する手順

1. claude_desktop_config.json を編集する

~/Library/Application Support/Claude/claude_desktop_config.json
{
  "globalShortcut": "",
  "mcpServers": {
    "claude_code": {
      "command": "claude",
      "args": ["mcp", "serve"],
      "env": {
        "PATH": "/Users/snaka/.asdf/(path to node installed)/bin:/opt/homebrew/bin"
      }
    }
  }
}

2. Claude Desktop を再起動する

チャット欄にハンマーアイコンが出てることを確認

アイコン開いたところ

3. プロンプトから指示を出し、Tool の実行を許可する

たとえば

/Users/snaka/path/to/project
このディレクトリの内容を把握してCLAUDE.mdを作成してください
実行結果

このプロジェクトの Open されている Pull Request の一覧を列挙してください
実行結果

みたいな指示を出してみる

ピン留めされたアイテム
snakasnaka

さわってみて

  • 😇 ローカル環境に対象サーバと連携するためのCLIが入っていれば良いので、対象ごとにMCPサーバを探してインストールして... という手間が無くてとても楽
  • 💸 Claude の定額料金の範囲内で利用できるので Cursor Pro や Cline のような従量課金な API でモデルを利用するエージェントより財布に優しい
  • 🤔 WebFetchTool の使い方がいまいち分かりづらく、Webを参照するのは他のMCPサーバを利用したほうが良いかもしれない... ?
  • 💥 Bash 経由で何でもコマンド実行できるようにすると危険な気がする
snakasnaka
snakasnaka

デバッグしてみる

Interseptor 経由でサーバをを立ち上げる

$ npx @modelcontextprotocol/inspector /path/to/claude mcp serve
Starting MCP inspector...
⚙️ Proxy server listening on port 6277
🔍 MCP Inspector is up and running at http://127.0.0.1:6274 🚀

snakasnaka

http://127.0.0.1:6274 をブラウザで開くと

サーバとして機能していそうに見える

snakasnaka

mcp-server-claude_code.log を見る

$ tail -f ~/Library/Logs/Claude/mcp-server-claude_code.log
2025-04-05T13:08:19.453Z [claude_code] [info] Client transport closed
2025-04-05T13:17:00.327Z [claude_code] [info] Initializing server...
2025-04-05T13:17:00.363Z [claude_code] [info] Server started and connected successfully
2025-04-05T13:17:00.364Z [claude_code] [info] Message from client: {"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"claude-ai","version":"0.1.0"}},"jsonrpc":"2.0","id":0}
unknown command: claude. Perhaps you have to reshim?
2025-04-05T13:17:00.409Z [claude_code] [info] Server transport closed
2025-04-05T13:17:00.409Z [claude_code] [info] Client transport closed
2025-04-05T13:17:00.409Z [claude_code] [info] Server transport closed unexpectedly, this is likely due to the process exiting early. If you are developing this MCP server you can add output to stderr (i.e. `console.error('...')` in JavaScript, `print('...', file=sys.stderr)` in python) and it will appear in this log.
2025-04-05T13:17:00.409Z [claude_code] [error] Server disconnected. For troubleshooting guidance, please visit our [debugging documentation](https://modelcontextprotocol.io/docs/tools/debugging) {"context":"connection"}
2025-04-05T13:17:00.410Z [claude_code] [info] Client transport closed

unknown command: claude. Perhaps you have to reshim? これか...?

node を asdf で管理してるので、それが関係してそう

snakasnaka

config を修正 path to node installed の部分は実際にコマンドがインストールされている場所を指している

~/Library/Application Support/Claude/claude_desktop_config.json
{
  "globalShortcut": "",
  "mcpServers": {
    "claude_code": {
      "command": "/Users/snaka/.asdf/(path to node installed)/bin/claude",
      "args": ["mcp", "serve"],
      "env": {}
    }
  }
}
snakasnaka

ログの内容に変化あったがまだダメそう... 今度は node コマンドが見つけきれてない

unknown command: node. Perhaps you have to reshim?
snakasnaka

config を再度修正
環境変数 PATH でコマンドが存在する場所を指定するようにしてみる

~/Library/Application Support/Claude/claude_desktop_config.json
{
  "globalShortcut": "",
  "mcpServers": {
    "claude_code": {
      "command": "claude",
      "args": ["mcp", "serve"],
      "env": {
        "PATH": "/Users/snaka/.asdf/(path to node installed)/bin"
      }
    }
  }
}
snakasnaka

コードのディレクトリを読み込ませてみる

path/to/project
このディレクトリの内容を把握してください

と依頼すると Tool の実行許可を求めてくる

snakasnaka

path の指定が ~/hoge/fuga みたいだとだめそう

/Users/snaka/path/to/project
このディレクトリの内容を把握してCLAUDE.mdを作成してください

このような指定であればOK

snakasnaka

gh コマンドを使えるようにする

このプロジェクトの Pull Request の一覧を列挙してください

とやってみたが、 そのままでは Homebrew 経由でインストールしていた gh コマンドが利用できなかった。どうやら PATH が参照できてないのが原因っぽいので config に記述している PATH を修正

~/Library/Application Support/Claude/claude_desktop_config.json
{
  "globalShortcut": "",
  "mcpServers": {
    "claude_code": {
      "command": "claude",
      "args": ["mcp", "serve"],
      "env": {
        "PATH": "/Users/snaka/.asdf/(path to node installed)/bin:/opt/homebrew/bin"
      }
    }
  }
}
snakasnaka

ビルドを依頼してみる

普通にビルドできた。DEVELOP.md に事前にビルド方法などを記載していたので読み取ってくれてたのかもしれない(?)

snakasnaka

Claude Code MCP server 経由で web にアクセスしてみる

現在の情報を web から取ってくるように依頼したらどうもできないっぽかった

セキュリティ的な配慮なのかもしれない

snakasnaka

WebFetchTool で参照してみる

このようなプロンプトを与えてみて WebFetchTool を使ってみた

以下のwebサイトの情報を取り込んでください。
   * https://madhukarkumar.medium.com/a-comprehensive-guide-to-vibe-coding-tools-2bd35e2d7b4f
   * https://medium.com/@niall.mcnulty/getting-started-with-vibe-coding-ac7d759dbe39

が、だめだった

Error: Tool WebFetchTool input is invalid: Error: Cannot fetch URL "https://madhukarkumar.medium.com/a-comprehensive-guide-to-vibe-coding-tools-2bd35e2d7b4f". For security, you can only fetch URLs from hosts that the user has mentioned in their messages or that are found in CLAUDE.md files or project files.
snakasnaka

サイト自体が AI による fetch を拒否している?

プロジェクトの指示の中に含めてみたがダメだった。
エラー内容からすると AI エージェントによるサイトアクセスを拒否している可能性もある。

Error: Domain medium.com is not allowed to be fetched
snakasnaka

実際に使用する場合は、ユーザーが言及したURLを使用する必要があります。例えば「https://react.dev についての情報が欲しい」とユーザーが言及した後に、そのURLをWebFetchToolで取得することが可能になります。

と言っているがだめだった

結局 webから情報を取ってくるためのMCPサーバを入れる必要がありそう

このスクラップは5ヶ月前にクローズされました