MCP Client について
MCP Client は完全な MCP 仕様を実装する TypeScript クライアントライブラリです。LLM エージェント向けに設計され、stdio と HTTP+SSE トランスポートをサポートし、Claude のネイティブツール呼び出しとシームレスに統合します。
主な機能
- 01完全な MCP 仕様実装
- 02Stdio および HTTP+SSE トランスポート対応
- 03MCP サーバープロセス管理
- 04Claude ネイティブツール呼び出し統合
- 05イベントベースの非同期 API
- 06リソース、ツール、プロンプト、サンプリング対応
活用シーン
- 01標準 MCP サーバーで LLM エージェントを強化
- 02ナレッジベースとファイルシステムサービスを統合
- 03ウェブ検索と自動化を備えた AI アプリを構築
MCP 対応
LLM エージェント向け MCP クライアントで、MCPConnectionManager を使用して複数の MCP サーバーへの接続を管理し、Claude のネイティブツール呼び出しと統合します。
詳細
MCP クライアント
LLM エージェント用の Model Context Protocol (MCP) クライアントの TypeScript 実装です。
インストール
npm install mcp-client
機能
- MCP 仕様の完全な実装
- stdio と HTTP+SSE トランスポートの両方に対応
- MCP サーバープロセス管理の組み込み
- Claude のネイティブツール呼び出しとの統合
- 型安全な API
- イベントベースアーキテクチャ
- Promise ベースの async/await API
- すべての MCP 操作に対応:
- リソース
- ツール
- プロンプト
- サンプリング
使用方法
MCP サーバーとの使用
MCP クライアントを使用する最も一般的な方法は、npx 経由で標準的な MCP サーバーを使用することです。設定ファイル(mcp-config.json)を作成してください:
{
"mcpServers": {
"memory": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-memory"
]
},
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/path/to/allowed/directory"
]
},
"brave-search": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-brave-search"
],
"env": {
"BRAVE_API_KEY": "your-api-key"
}
}
}
}
次に、MCPConnectionManager を使用してサーバーに接続します:
import { MCPConnectionManager } from 'mcp-client';
const manager = new MCPConnectionManager();
await manager.initialize('./mcp-config.json');
// Get clients for specific servers
const memoryClient = manager.getClient('memory');
const fsClient = manager.getClient('filesystem');
// Use tools from the servers
const memoryTools = await memoryClient?.listTools();
const fsTools = await fsClient?.listTools();
// Clean up when done
await manager.cleanup();
Claude との統合
クライアントは Claude のネイティブツール呼び出しとシームレスに連携するように設計されています:
import { Anthropic } from '@anthropic-ai/sdk';
import { MCPConnectionManager } from 'mcp-client';
// Initialize Claude and MCP
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
const manager = new MCPConnectionManager();
await manager.initialize('./mcp-config.json');
// Get a client
const memoryClient = manager.getClient('memory');
const tools = await memoryClient?.listTools();
// Use with Claude
const response = await anthropic.messages.create({
model: 'claude-3-sonnet-20240229',
max_tokens: 1024,
messages: [{ role: 'user', content: 'your prompt' }],
tools: tools?.map(tool => ({
name: tool.name,
description: tool.description || '',
parameters: tool.inputSchema
}))
});
// Handle tool calls
if (!response.content[0].text && response.content[0].type === 'tool_call') {
const toolCall = response.content[0].tool_calls[0];
const result = await memoryClient?.callTool(
toolCall.name,
toolCall.parameters
);
// Send result back to Claude...
}
低レベルの使用方法
さらに詳細な制御が必要な場合は、クライアントを直接使用できます:
import { MCPClient, StdioTransport } from 'mcp-client';
// Create a transport
const transport = new StdioTransport();
// Create and connect client
const client = new MCPClient({ transport });
await client.connect();
// List available tools
const tools = await client.listTools();
// Call a tool
const result = await client.callTool('tool-name', {
// tool parameters
});
利用可能な MCP サーバー
npx で利用可能な一般的な MCP サーバー:
@modelcontextprotocol/server-memory- ナレッジグラフ操作@modelcontextprotocol/server-filesystem- ファイルシステム操作@modelcontextprotocol/server-brave-search- Web 検索機能@modelcontextprotocol/server-puppeteer- Web オートメーション@modelcontextprotocol/server-fetch- HTTP リクエスト- その他多数...
例
完全な例については /examples ディレクトリを確認してください:
memory-app- メモリサーバーを使用したナレッジグラフ操作llm-app- MCP ツール対応の基本的な LLM アプリllm-app-tools- Claude ネイティブツール呼び出し対応の高度な LLM アプリ
API リファレンス
MCPConnectionManager
class MCPConnectionManager {
initialize(configPath: string): Promise<void>;
getClient(serverName: string): MCPClient | undefined;
cleanup(): Promise<void>;
}
MCPClient
class MCPClient {
constructor(options: MCPClientOptions);
// Connection
connect(): Promise<void>;
close(): Promise<void>;
// Tools
listTools(): Promise<Tool[]>;
callTool(name: string, params: Record<string, any>): Promise<any>;
// Resources
listResources(): Promise<Resource[]>;
readResource(uri: string): Promise<string | Buffer>;
// Prompts
listPrompts(): Promise<Prompt[]>;
getPrompt(name: string, args?: Record<string, any>): Promise<string>;
}
貢献
貢献を歓迎します。ぜひ Pull Request を送信してください。
ライセンス
MIT
コメント
AI アプリケーションが外部のツールやデータソースに統一的な方法で接続するためのオープンなプロトコルです。クライアントはモデルとの対話を担い、MCP サーバーはツール・リソース・プロンプトテンプレートを提供します。標準メッセージでやり取りするため、1 つのサーバーを複数のクライアントで再利用できます。