PHP MCP Client について
MCP サーバーに接続するアプリケーションを構築する PHP 開発者向けライブラリです。複数のトランスポートと、ReactPHP の Promise による非同期処理に対応します。
主な機能
- 01SSE、Stdio、Process、StreamableHttp に対応
- 02ReactPHP Promise ベースの API
- 03PSR-3 ロガーに対応
- 04ツールアノテーションと `_meta` に対応
- 05カスタムトランスポーターに対応
活用シーン
- 01SSE または HTTP 経由で MCP サーバーに接続
- 02ローカルの MCP サーバープロセスを起動して通信
- 03PHP から非同期の MCP リクエストを送信
MCP 対応
MCP サーバーと通信する PHP クライアントライブラリです。2025-03-26 版 MCP の大部分をサポートします。
詳細
PHP 向け Model Context Protocol クライアント実装
Model Context Protocol(MCP)サーバーと連携するための PHP クライアントライブラリです。
インストール
Composer 経由でパッケージをインストールできます。
composer require swisnl/mcp-client
要件
- PHP 8.2 以降
- ReactPHP パッケージ
機能
- 複数のトランスポート方式:
- SSE(Server-Sent Events)
- Stdio(標準入出力)
- Process(外部プロセス通信)
- StreamableHttp(セッション管理付き HTTP)
- ReactPHP を使用した Promise ベースの API
- PSR-3 ロガーインターフェースのサポート
- MCP プロトコルの大部分をサポート(2025-03-26)
- ツールのアノテーションをサポート
_metaをサポート
基本的な使い方
SSE トランスポート
use Swis\McpClient\Client;
// Create client with SSE transporter
$endpoint = 'https://your-mcp-server.com/sse';
$client = Client::withSse(
$endpoint,
headers: ['Authorization' => 'Bearer your-token']
);
// Connect to the server
$client->connect(function($initResponse) {
echo "Connected to server: " . json_encode($initResponse['serverInfo']) . "\n";
});
// List available tools
$tools = $client->listTools();
foreach ($tools->getTools() as $tool) {
echo "- {$tool->getName()}: {$tool->getDescription()}\n";
// Access tool annotations if available
if ($annotations = $tool->getAnnotations()) {
echo " * Read-only: " . ($annotations->getReadOnlyHint() ? 'Yes' : 'No') . "\n";
echo " * Title: " . ($annotations->getTitle() ?? 'N/A') . "\n";
}
}
// Call a tool
$result = $client->callTool('echo', ['message' => 'Hello World!']);
echo $result->getResult() . "\n";
Process トランスポート
use Swis\McpClient\Client;
// Create client with a process transporter
[$client, $process] = Client::withProcess('/path/to/mcp-server/binary');
// Connect to the server
$client->connect();
// Use the client...
// Disconnect when done
$client->disconnect();
StreamableHttp トランスポート
use Swis\McpClient\Client;
// Create client with StreamableHttp transporter
$endpoint = 'https://your-mcp-server.com/';
$client = Client::withStreamableHttp(
$endpoint,
headers: ['Authorization' => 'Bearer your-token']
);
// Connect to the server
$client->connect();
// The transporter will automatically manage session IDs from the Mcp-Session-Id header
// Use the client...
// Disconnect when done
$client->disconnect();
Agents SDK との組み合わせ
まず、Agents SDK をインストールします。
composer require swisnl/agents-sdk
use Swis\Agents\Agent;
use Swis\Agents\Mcp\McpConnection;
use Swis\McpClient\Client;
use Swis\Agents\Orchestrator;
$agent = new Agent(
name: 'Calculator Agent',
description: 'This Agent can perform arithmetic operations.',
mcpConnections: [
new MathMcpConnection(),
]
);
$orchestrator = new Orchestrator($agent);
echo $orchestrator
->withUserInstruction('What\'s 5 + 5?')
->run($agent)
class MathMcpConnection extends McpConnection
{
public function __construct()
{
[$client, $process] = Client::withProcess(
command: 'node ' . realpath(__DIR__ . '/node_modules/math-mcp/build/index.js'),
);
parent::__construct(
client: $client,
name: 'Math MCP',
);
}
}
高度な使い方
メタデータ
_meta パラメーターを使ってメタデータを送信できます。
use Swis\McpClient\Requests\CallToolRequest
$request = new CallToolRequest('search');
$request->withMeta(['traceparent' => '0000-0000-00-00x']);
カスタムトランスポーター
TransporterInterface を実装することで、独自のトランスポーターを実装できます。
use Swis\McpClient\TransporterInterface;
use Swis\McpClient\EventDispatcher;
class CustomTransporter implements TransporterInterface
{
// Implement required methods
}
// Create a client with your custom transporter
$transporter = new CustomTransporter();
$eventDispatcher = new EventDispatcher();
$client = new Client($transporter, $eventDispatcher);
非同期処理
クライアントは ReactPHP の Promise を使用した非同期処理をサポートしています。
$client->sendRequest(new ListToolsRequest())->then(...);
変更履歴
最近の変更については、CHANGELOG をご覧ください。
コントリビューション
詳細については、CONTRIBUTING をご覧ください。
セキュリティの脆弱性
セキュリティの脆弱性を報告する方法については、セキュリティポリシーをご確認ください。
クレジット
ライセンス
このパッケージは、MIT ライセンスの下でライセンスされたオープンソースソフトウェアです。
このパッケージはTreewareです。本番環境で使用する場合は、私たちの活動への感謝として世界に木を贈っていただけると幸いです。Treeware の森に貢献することで、地域の家族に雇用を生み出し、野生生物の生息地を回復できます。
SWIS :heart: オープンソース
SWIS は、オランダのライデンにあるウェブエージェンシーです。私たちはオープンソースソフトウェアに取り組むことが大好きです。
コメント
AI アプリケーションが外部のツールやデータソースに統一的な方法で接続するためのオープンなプロトコルです。クライアントはモデルとの対話を担い、MCP サーバーはツール・リソース・プロンプトテンプレートを提供します。標準メッセージでやり取りするため、1 つのサーバーを複数のクライアントで再利用できます。