BioErrorLog Tech Blog

試行錯誤の記録

MCP CLIでMCPサーバー開発を効率化する

MCP CLIの使い方をまとめます。

はじめに

MCP CLIはMCPサーバー開発を効率化するツールで、MCP SDKに内包されています。

今回はこのMCP CLIの使い方をメモします。

The English translation of this post is here.

MCP CLIの使い方

MCP Python SDKを使って、MCP CLIを利用する方法を見ていきます。

MCP CLIをインストールする

MCP CLIを使うには、MCP SDKをcliオプション付きmcp[cli]でインストールします。

# uvを使う場合
uv add "mcp[cli]"

# pipを使う場合
pip install "mcp[cli]"


インストールできたかを確認:

$ mcp version
MCP version 1.6.0

MCP CLIの機能一覧

mcp --helpで機能の一覧が確認できます。

$ mcp --help
                                                                       
 Usage: mcp [OPTIONS] COMMAND [ARGS]...                                
                                                                       
 MCP development tools                                                 
                                                                       
╭─ Options ───────────────────────────────────────────────────────────╮
│ --help          Show this message and exit.                         │
╰─────────────────────────────────────────────────────────────────────╯
╭─ Commands ──────────────────────────────────────────────────────────╮
│ version   Show the MCP version.                                     │
│ dev       Run a MCP server with the MCP Inspector.                  │
│ run       Run a MCP server.                                         │
│ install   Install a MCP server in the Claude desktop app.           │
╰─────────────────────────────────────────────────────────────────────╯
  • mcp version: バージョン確認
  • mcp run: MCPサーバーを実行
  • mcp dev: MCPサーバーをMCP Inspectorで実行
  • mcp install: MCPサーバーをClaude Desktopに接続

それではmcp version以外の3つの機能(run/dev/install)を見ていきます。

mcp runの使い方

mcp runではMCPサーバーが実行/起動されます。

まずは--helpで使い方を確認:

$ mcp run --help
                                                                                                
 Usage: mcp run [OPTIONS] FILE_SPEC                                                             
                                                                                                
 Run a MCP server.                                                                              
 The server can be specified in two ways:                                                       
 1. Module approach: server.py - runs the module directly, expecting a server.run() call.       
 2. Import approach: server.py:app - imports and runs the specified server object.              
                                                                                                
 Note: This command runs the server directly. You are responsible for ensuring all dependencies 
 are available.                                                                                 
 For dependency management, use `mcp install` or `mcp dev` instead.                             
                                                                                                
╭─ Arguments ──────────────────────────────────────────────────────────────────────────────────╮
│ *    file_spec      TEXT  Python file to run, optionally with :object suffix [default: None] │
│                           [required]                                                         │
╰──────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────────────────────╮
│ --transport  -t      TEXT  Transport protocol to use (stdio or sse) [default: None]          │
│ --help                     Show this message and exit.                                       │
╰──────────────────────────────────────────────────────────────────────────────────────────────╯

MCPサーバーのエントリーポイントを指定してmcp runを実行します。

  • mcp run server.py (Module approachで実行)
  • mcp run server.py:app (Import approachで実行)

例えば下記のようにrun()をスクリプト内で呼び出している場合はそのままmodule approachでmcp run server.py、そうでない場合はimport approachでmcp run server.py:appとします。

from mcp.server.fastmcp import FastMCP


app = FastMCP("HelloMCP")

# ~~ 中略 ~~

if __name__ == "__main__":
    app.run(transport='stdio')

mcp devの使い方

mcp devでは、ブラウザベースのMCPサーバーテストツールであるMCP Inspectorを起動できます。

mcp devの使い方:

$ mcp dev --help

                                                                       
 Usage: mcp dev [OPTIONS] FILE_SPEC                                    
                                                                       
 Run a MCP server with the MCP Inspector.                              
                                                                       
╭─ Arguments ─────────────────────────────────────────────────────────╮
│ *    file_spec      TEXT  Python file to run, optionally with       │
│                           :object suffix                            │
│                           [default: None]                           │
│                           [required]                                │
╰─────────────────────────────────────────────────────────────────────╯
╭─ Options ───────────────────────────────────────────────────────────╮
│ --with-editable  -e      DIRECTORY  Directory containing            │
│                                     pyproject.toml to install in    │
│                                     editable mode                   │
│                                     [default: None]                 │
│ --with                   TEXT       Additional packages to install  │
│ --help                              Show this message and exit.     │
╰─────────────────────────────────────────────────────────────────────╯

先述のmcp run同様、MCPサーバーのエントリーポイントをmodule approachまたはimport approachで指定して実行します。

# Module approach
mcp dev server.py

# Import approach
mcp dev server.py:app


MCP Inspector自体の使い方については別記事にまとめているので、よろしければこちらをご覧ください:

www.bioerrorlog.work

mcp installの使い方

mcp installでは、MCPサーバーをClaude Desktopにインストールします。

Claude DesktopにMCPサーバーを接続する手順を愚直にやるなら、Claude Desktopのconfigファイルを手動で編集する必要があります。

参考:
www.bioerrorlog.work

mcp installを使えば、MCPサーバーのClaude Desktop configファイルへの追記を自動で行うことができます。

$ mcp install --help
                                                                                       
 Usage: mcp install [OPTIONS] FILE_SPEC                                                
                                                                                       
 Install a MCP server in the Claude desktop app.                                       
 Environment variables are preserved once added and only updated if new values are     
 explicitly provided.                                                                  
                                                                                       
╭─ Arguments ─────────────────────────────────────────────────────────────────────────╮
│ *    file_spec      TEXT  Python file to run, optionally with :object suffix        │
│                           [default: None]                                           │
│                           [required]                                                │
╰─────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ───────────────────────────────────────────────────────────────────────────╮
│ --name           -n      TEXT       Custom name for the server (defaults to         │
│                                     server's name attribute or file name)           │
│                                     [default: None]                                 │
│ --with-editable  -e      DIRECTORY  Directory containing pyproject.toml to install  │
│                                     in editable mode                                │
│                                     [default: None]                                 │
│ --with                   TEXT       Additional packages to install                  │
│ --env-var        -v      TEXT       Environment variables in KEY=VALUE format       │
│ --env-file       -f      FILE       Load environment variables from a .env file     │
│                                     [default: None]                                 │
│ --help                              Show this message and exit.                     │
╰─────────────────────────────────────────────────────────────────────────────────────╯

mcp runmcp dev同様にMCPサーバースクリプトを指定してmcp installを実行します。

--nameオプションにはClaude Desktopで表示するMCPサーバー名を、--env-var (-v)--env-file (-f)からは環境変数を指定できます。

# 例
mcp install server.py

# MCPサーバー名を指定
mcp install server.py --name "My MCP"

# 環境変数を指定
mcp install server.py -v API_KEY=abc123 -v DB_URL=postgres://...
mcp install server.py -f .env


Claude Desktopのconfigファイル(claude_desktop_config.json)が空の状態でmcp installを実行した例:

mcp install main.py --name "My MCP"
# claude_desktop_config.json
{
  "mcpServers": {
    "My MCP": {
      "command": "uv",
      "args": [
        "run",
        "--with",
        "mcp[cli]",
        "mcp",
        "run",
        "/path/to/main.py"
      ]
    }
  }
}

このようにclaude_desktop_config.jsonが追記され、MCPサーバーがClaude Desktopに登録されます。

おわりに

以上、MCP CLIの使い方をまとめました。

どなたかの参考になれば幸いです。

[関連記事]

www.bioerrorlog.work

www.bioerrorlog.work

参考