Zenn
Closed4

anthropic-ai/claude-codeを読む

laisolaiso

npmパッケージがここにある。GitHubリポジトリはまだ公開されていない。
https://www.npmjs.com/package/@anthropic-ai/claude-code?activeTab=code

> npm pack  @anthropic-ai/claude-code
> tar zxfv anthropic-ai-claude-code-0.2.9.tgz
> cd package
> ls

LICENSE.md   README.md    cli.mjs      package.json vendor       yoga.wasm

❯ ls vendor/
ripgrep sdk

cli.mjsは4.6Mの巨大テキストファイルだが大半が依存モジュールの埋め込みなのでカットする

tail -n +300 cli.mjs > cli.mjs.tail
❯ askrepo cli.mjs.tail

追記

sourcemap付きのデバッグ版が誤ってnpmにアップロードされていたらしくて勝手に復元して転載しているユーザーがいた(DMCA takedownされていたのでリンクしない)

laisolaiso

依存ライブラリ

ink

  • reactの名前があって不思議だったけど、TUIにinkを使っているからだった
  • yoga.wasmはyogaだと思われる。inkが使ってる。
laisolaiso

tooling

  • {name:"で検索すると探せる

プレーンテキストでプロンプトが定義されている。

This is a tool for editing files. For moving or renaming files, you should generally use the Bash tool with the 'mv' command instead. For larger edits, use the Write tool to overwrite files. For Jupyter notebooks (.ipynb files), use the ${RI.name} instead.

Before using this tool:

1. Use the View tool to understand the file's contents and context
...

XMLでアノテーションされてる。

<example>
git commit -m "$(cat <<'EOF'
   Commit message here.

   \uD83E\uDD16 Generated with ${K4}
   Co-Authored-By: Claude <noreply@anthropic.com>
   EOF
   )"
</example>
  • Jupyter notebook用のToolがある(単純なプレーンテキスト処理でないから?)
laisolaiso

MCPサーバー(claude/tengu)

tools

Claude Codeのアーキテクチャは内部MCPクライアント+MCPサーバーという構成になっていて、デバッグの為かclaude mcp serveでサーバーを単体起動できるようになっている。

これをインスペクタで呼び出すとtool/listが取得できる

> npx @modelcontextprotocol/inspector claude mcp serve
> open http://localhost:5173 
ツール名 説明 必須パラメータ オプションパラメータ
dispatch_agent 新しいタスクを起動する prompt: エージェントが実行するタスク なし
Bash コマンドを実行する (API キー無効) command: 実行するコマンド timeout: タイムアウト(ミリ秒、最大600000)
Edit ファイルを編集するツール file_path: 変更するファイルの絶対パス
old_string: 置換する文字列
new_string: 置換後の文字列
なし
View ローカルファイルシステムからファイルを読み込む file_path: 読み込むファイルの絶対パス offset: 読み込み開始行番号
limit: 読み込む行数
GlobTool ファイルパターンマッチングツール pattern: 一致させるグロブパターン path: 検索するディレクトリ
GrepTool コンテンツ検索ツール pattern: ファイル内容で検索する正規表現パターン path: 検索するディレクトリ
include: 検索に含めるファイルパターン
Replace ローカルファイルシステムにファイルを書き込む file_path: 書き込むファイルの絶対パス
content: ファイルに書き込む内容
なし
LS 指定されたパスのファイルとディレクトリを一覧表示する path: 一覧表示するディレクトリの絶対パス なし

Claude Desktopから接続して使う

これは以下のように記述すると標準出力経由でToolを使える。

❯ cat  ~/Library/Application\ Support/Claude/claude_desktop_config.json
{
"mcpServers": {                                                                                                                                              
    "claude-code": {
      "command": "npx",
      "args": [
        "claude",
        "mcp",
        "serve"
      ]
    }
  }
}

成功するとToolを使ってくれる。

Anthropic APIを呼び出しているのはあくまでクライアントなのでDesktopから繋ぐと通常のClaudeのWeb APIで会話していた。

このスクラップは19日前にクローズされました
ログインするとコメントできます