Mcp client example について
Anthropic APIとMCPクライアント、MCPサーバーを組み合わせたコマンドラインチャットのサンプルです。サーバーへの接続、ツール情報の取得、アシスタントとのチャットでの利用方法を示します。
主な機能
- 01コマンドラインチャットアプリ
- 02Anthropic API統合
- 03MCPクライアントとサーバーの接続
- 04サーバーのツール情報を取得
活用シーン
- 01Anthropic APIを使ったMCPチャットの試用
- 02MCPサーバーが公開するツールの確認
- 03MCPクライアントの処理例の確認
MCP 対応
クライアントは設定されたMCPサーバーに接続し、チャットで使うツール情報を取得します。
設定
ローカルサーバーの設定はどのクライアントでも共通です。コピーしてクライアントの MCP 設定ファイルに貼り付けてください。
インストール方法は未掲載です
このサーバーのパッケージやリモートエンドポイントはまだ取得できていません。プロジェクトの README の手順に従ってください。
ツール
ツール一覧はまだ取得されていません
ローカルサーバーのツールはマシン上で起動して初めて取得できます。メンテナーはまだツール一覧を提供していません。
詳細
MCP クライアントの例
これは、MCP クライアントと MCP サーバーを組み合わせ、Anthropic API を使用する AI アシスタント向けの CLI チャットアプリケーションのサンプルです。
参考: クライアント開発者向け - Model Context Protocol
Anthropic API: 初期設定 - Anthropic メッセージ - Anthropic
システム図
シーケンス図
sequenceDiagram
participant User
participant MCPClient
participant AnthropicAPI
participant MCPServer
User->>MCPClient: Run
MCPClient->>MCPServer: Connect and get tool info
MCPServer-->>MCPClient: Tool info
loop Chat loop (until 'quit' is entered)
User->>MCPClient: Enter query
MCPClient->>AnthropicAPI: Send query
AnthropicAPI-->>MCPClient: AI response
alt When tool usage is required
MCPClient->>MCPServer: Call tool
MCPServer-->>MCPClient: Tool execution result
MCPClient->>AnthropicAPI: Send query again including tool result
AnthropicAPI-->>MCPClient: Final response
end
MCPClient-->>User: Display response
end
User->>MCPClient: Enter 'quit'
MCPClient->>MCPServer: Disconnect
MCPClient-->>User: Exit
セットアップ
pnpm install
.env
ANTHROPIC_API_KEY=your-api-key
MCP_SERVER_SCRIPT_PATH=/path/to/mcp-server.js
ビルド
pnpm build
実行
pnpm start
出力例
この MCP サーバーを使用します https://github.com/t-shiratori/time-tools-mcp-server
mcp-client-example $ pnpm start
> [email protected] start /path/to/mcp-client-example
> node build/index.js
Example MCP Server running on stdio
Connected to server with tools: [ 'get_current_date_time', 'get_elapsed_time' ]
MCP Client Started!
Type your queries or 'quit' to exit.
🖌 Query: Hello
Hello! I'm here to assist you. I can help you with tasks related to date and time, such as getting the current date and time or calculating the elapsed time between two dates.
Is there something specific you'd like to know about dates or times today?
🖌 Query: What time is it?
I can check the current date and time for you.
[Calling tool get_current_date_time with args {}]
Based on the given timestamp, the time is 10:37:19 AM on May 6th, 2025.
コメント
Markdown 対応
AI アプリケーションが外部のツールやデータソースに統一的な方法で接続するためのオープンなプロトコルです。クライアントはモデルとの対話を担い、MCP サーバーはツール・リソース・プロンプトテンプレートを提供します。標準メッセージでやり取りするため、1 つのサーバーを複数のクライアントで再利用できます。