关于 PHP MCP Client
面向构建 MCP 服务连接应用的 PHP 开发者库,支持多种传输方式,并通过 ReactPHP promise 提供异步操作。
主要功能
- 01支持 SSE、Stdio、Process 和 StreamableHttp
- 02基于 ReactPHP promise 的 API
- 03支持 PSR-3 日志接口
- 04支持工具注解和 `_meta`
- 05支持自定义传输器
适用场景
- 01通过 SSE 或 HTTP 连接 MCP 服务
- 02启动并通信本地 MCP 服务进程
- 03在 PHP 中发送异步 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(服务器发送事件)
- Stdio(标准输入/输出)
- Process(外部进程通信)
- StreamableHttp(支持会话管理的 HTTP)
- 基于 ReactPHP Promise 的 API
- 支持 PSR-3 Logger 接口
- 支持大部分 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 是一家来自荷兰莱顿的网络代理机构。我们热爱使用开源软件。
评论
支持 Markdown
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。