PHP MCP Client
- agent-framework
Model Context Protocol client implementation for PHP
About
What is PHP MCP Client?
A PHP client library for interacting with Model Context Protocol (MCP) servers. It runs on any PHP 8.2+ environment and is built on ReactPHP for asynchronous operations.
How to use PHP MCP Client?
Install via Composer (composer require swisnl/mcp-client), then create a client with a transport (SSE, Stdio, Process, or StreamableHttp), connect to the server, and call tools. For example, Client::withSse($endpoint) initiates an SSE connection, and $client->callTool('name', ['param' => 'value']) executes a tool.
Key features of PHP MCP Client
- Multiple transport mechanisms: SSE, Stdio, Process, and StreamableHttp
- Promise-based API with ReactPHP for async operations
- PSR-3 Logger interface support
- Tool annotation support (read‑only hint, title)
- Custom transporter support via TransporterInterface
- Integration with the SWIS Agents SDK
Use cases of PHP MCP Client
- Connect PHP applications to any MCP‑compatible server (e.g., math tools, data retrievers)
- Build AI agents by combining the client with the SWIS Agents SDK
- Call remote or local MCP tools with structured input and output
FAQ from PHP MCP Client
What is the PHP MCP Client?
It’s a PHP library that implements the client side of the Model Context Protocol, allowing PHP applications to interact with MCP servers over multiple transports using async I/O.
What PHP version is required?
PHP 8.2 or higher, along with ReactPHP packages.
Which transports are supported?
Four transports: Server‑Sent Events (SSE), standard I/O (Stdio), external process communication (Process), and HTTP with session management (StreamableHttp).
Is the PHP MCP Client free?
Yes, it’s open‑source under the MIT license. The project is Treeware – if used in production, you are asked to buy a tree.
Can I use a custom transport?
Yes. Implement the TransporterInterface and pass your transporter to the Client constructor.
Details
- Author
- swisnl
- Category
- agent-framework
- Repository
- swisnl/mcp-client
Model Context Protocol client implementation for PHP
A PHP client library for interacting with Model Context Protocol (MCP) servers.
Installation
You can install the package via composer:
composer require swisnl/mcp-client
Requirements
- PHP 8.2 or higher
- ReactPHP packages
Features
- Multiple transport mechanisms:
- SSE (Server-Sent Events)
- Stdio (Standard input/output)
- Process (External process communication)
- StreamableHttp (HTTP with session management)
- Promise-based API with ReactPHP
- PSR-3 Logger interface support
- Most of MCP protocol support (2025-03-26)
- Tool annotation support
Basic Usage
SSE Transport
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";
// 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 Transport
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 Transport
use Swis\McpClient\Client;
// Create client with StreamableHttp transporter
$endpoint = 'https://your-mcp-server.com/';
$client = Client::withStreamableHttp($endpoint);
// 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();
Use in combination with Agents SDK
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',
);
}
}
Advanced Usage
Custom Transporter
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);
Async Operations
The client supports async operations using ReactPHP promises:
$client->sendRequest(new ListToolsRequest())->then(...);
License
This package is open-sourced software licensed under the MIT license.
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.