Run the MCP Client について
Run the MCP Client は、稼働中のファイルシステム MCP サーバーをテストする Python クライアントです。ツールの一覧表示や指定ツールの実行、結果の出力により、接続や動作を確認できます。
主な機能
- 01ファイルシステム MCP サーバーへの接続
- 02利用可能なツールの一覧表示
- 03引数を指定したツール呼び出し
- 04呼び出し結果の出力
活用シーン
- 01クライアントとサーバー間の接続確認
- 02引数を指定したファイルシステムツールのテスト
- 03ファイル作成・読み取り・一覧表示の検証
MCP 対応
稼働中のファイルシステム MCP サーバーに接続し、利用可能なツールを取得して指定ツールを呼び出します。
設定
ローカルサーバーの設定はどのクライアントでも共通です。コピーしてクライアントの MCP 設定ファイルに貼り付けてください。
このサーバーのパッケージやリモートエンドポイントはまだ取得できていません。プロジェクトの README の手順に従ってください。
ツール
ローカルサーバーのツールはマシン上で起動して初めて取得できます。メンテナーはまだツール一覧を提供していません。
詳細
MCP クライアントを実行する
このクライアントを実行する前に、サーバーが起動していることを確認してください。詳しくは、こちらをご覧ください:https://github.com/dazzaji/filesystem
ステップ 1:仮想環境を作成し、依存関係を正しくインストールする
次のコマンドを使用します:
```bash
mkdir mcp-client-py
cd mcp-client-py
```
```bash
python -m venv venv
source venv/bin/activate
```
```bash
pip install -r requirements.txt
```
これにより、仮想環境が作成されて有効化され、その環境に mcp と python-dotenv(およびそれらの依存関係)がインストールされます。
ステップ 2:.env の環境変数を確認または修正する
サーバーのパスと許可するディレクトリを指定した .env ファイルを作成します。テスト目的で、ツールと引数を指定することもできます。
SERVER_PATH=/Users/dazzagreenwood/filesystem/dist/index.js
ALLOWED_DIRECTORY=/Users/dazzagreenwood/mcp-hello/module1/files
# Test with arguments:
#TOOL=list_directory
#ARGS='{"path": "/Users/dazzagreenwood/mcp-hello/module1/files", "recursive": true}'
# Or test with other tools
#TOOL=read_file
#ARGS='{"path": "/Users/dazzagreenwood/mcp-hello/module1/files/test.txt"}'
ステップ 3:Python クライアントを実行する
クライアントを実行して、サーバーをテストします。
python client.py
```
* If you used the optional `TOOL` and `ARGS` add those arguments like so:
```bash
python client.py --tool "list_directory" --args '{"path": "/Users/dazzagreenwood/mcp-hello/module1/files", "recursive": true}'
```
This will:
* Connect to your filesystem server.
* List available tools.
* Call the `write_file` to create a test file with text content in the specified directory, if `tool` is not specified in the .env file.
* Display the results to the console.
### Step 4: Verifying Results
After successfully running the client, check the following:
1. **Client Output:** You should see output like `Available tools: [ ... ]`, and the result from the `write_file` (or other tool call).
2. **File Creation:** Verify that `testfile.txt` has been created with the correct content inside `/Users/dazzagreenwood/mcp-hello/module1/files/`. If testing with the read or list tools, make sure the output is what you'd expect.
### Testing Workflow
Here are steps to verify the project with the client and a server:
#### Starting the Server
1. **Open a Terminal**. You will need to start the server from a terminal.
2. **Navigate to Server Directory**. Change directory to the folder containing the server:
```
cd /Users/dazzagreenwood/filesystem
```
3. **Run the Server:**
```bash
node dist/index.js "/Users/dazzagreenwood/mcp-hello/module1/files"
```
* Replace `/Users/dazzagreenwood/filesystem/dist/index.js` with the actual path to the compiled file.
* Replace `/Users/dazzagreenwood/mcp-hello/module1/files` with the allowed directory.
#### Testing with the Client
1. **Open another Terminal window** or split your existing terminal tab.
2. **Navigate to Client Directory** Change directories into the `mcp-client-py` folder:
```bash
cd /Users/dazzagreenwood/mcp-client-server/mcp-client-py
source .venv/bin/activate
```
3. **Run the Client in Default Mode:**
To test basic connectivity and the `write_file` tool:
```bash
python client.py
```
This will attempt to call write file, to create a file named test.txt into the `/Users/dazzagreenwood/mcp-hello/module1/files` directory with the string `Hello from Python the new standup MCP client!` as it's content.
4. **Run the client with specific tools and arguments**: To test list_directory, use the following:
```bash
python client.py --server-path /Users/dazzagreenwood/filesystem/dist/index.js --allowed-dir /Users/dazzagreenwood/mcp-hello/module1/files --tool list_directory --args '{"path": "/Users/dazzagreenwood/mcp-hello/module1/files", "recursive": true}'
`read_file` ツールをテストするには、次のようにします:
python client.py --server-path /Users/dazzagreenwood/filesystem/dist/index.js --allowed-dir /Users/dazzagreenwood/mcp-hello/module1/files --tool read_file --args '{"path": "/Users/dazzagreenwood/mcp-hello/module1/files/test.txt"}'
* 上記の例にあるパスを、ローカルのパスに合わせて置き換えてください。
セットアップが正しく行われていれば、これらの操作が確認できる出力が表示されます。
この手順に従えば、Python および TypeScript/Node.js を使用して、さまざまなツールで MCP サーバーとクライアントをテストできます!
コメント
AI アプリケーションが外部のツールやデータソースに統一的な方法で接続するためのオープンなプロトコルです。クライアントはモデルとの対話を担い、MCP サーバーはツール・リソース・プロンプトテンプレートを提供します。標準メッセージでやり取りするため、1 つのサーバーを複数のクライアントで再利用できます。