A PHP client library for interacting with Model Context Protocol (MCP) servers.
You can install the package via composer:
composer require swisnl/mcp-client
- PHP 8.2 or higher
- ReactPHP packages
- Multiple transport mechanisms:
- SSE (Server-Sent Events)
- Stdio (Standard input/output)
- Process (External process communication)
- Promise-based API with ReactPHP
- PSR-3 Logger interface support
- Most of MCP protocol support
use Swis\McpClient\Client;
// Create client with SSE transporter
$endpoint = 'https://your-mcp-server.com/sse';
$client = Client::withSse($endpoint);
// 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";
}
// Call a tool
$result = $client->callTool('echo', ['message' => 'Hello World!']);
echo $result->getResult() . "\n";
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();
First, install 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',
);
}
}
You can implement your own transporter by implementing the 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);
The client supports async operations using ReactPHP promises:
$client->sendRequest(new ListToolsRequest())->then(...);
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
This package is open-sourced software licensed under the MIT license.