定制 redberry/mcp-client-laravel 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

redberry/mcp-client-laravel

Composer 安装命令:

composer require redberry/mcp-client-laravel

包简介

Package that enables you access to any mcp server that you define in the config

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A Laravel client for the Model Context Protocol. It speaks JSON-RPC 2.0 over both Streamable HTTP (2025-03-26) and STDIO, and exposes a single facade for listing and calling tools and reading resources. The HTTP transport content-negotiates with the server on every request, so you receive the final result whether the server replies with one JSON object or a stream of server-sent events.

Installation

Install the package via Composer:

composer require redberry/mcp-client-laravel

Then publish the configuration file:

php artisan vendor:publish --tag="mcp-client-config"

This will create a config/mcp-client.php file in your application.

Configuration

The mcp-client.servers array maps a server name to its connection details. Each server uses one of two transports — HTTP for remote servers, STDIO for local subprocesses:

use Redberry\MCPClient\Enums\Transporters;

return [
    'servers' => [
        'github' => [
            'type'     => Transporters::HTTP,
            'base_url' => 'https://api.githubcopilot.com/mcp',
            'token'    => env('GITHUB_API_TOKEN'),
            'timeout'  => 30,
        ],

        'memory' => [
            'type'    => Transporters::STDIO,
            'command' => ['npx', '-y', '@modelcontextprotocol/server-memory'],
            'cwd'     => base_path(),
        ],
    ],
];

HTTP Transport

The HTTP transport implements MCP's Streamable HTTP transport. On the first request, it performs the initialize handshake and captures the mcp-session-id for the rest of the session. If the server later signals that the session has expired with an HTTP 404, the client clears its session, re-handshakes, and retries the call once.

Key Default Description
base_url The MCP endpoint URL.
token null Bearer token; sent as Authorization: Bearer {token} when present.
timeout 30 Connection timeout in seconds.
read_timeout 60 Maximum gap between SSE chunks before the parser aborts a wedged stream. The clock resets on every chunk. Set to null to disable.
max_session_retries 1 Automatic retries after a session-loss 404. Set to 0 to disable.
headers [] Additional headers merged into every request.
id_type 'int' 'int' or 'string'; controls how JSON-RPC ids are cast.

STDIO Transport

The STDIO transport launches the configured command as a subprocess and exchanges newline-delimited JSON-RPC over its standard streams. The subprocess starts lazily on the first call and is reused for every subsequent request.

Key Default Description
command Array of command parts to launch.
cwd null Working directory for the subprocess.
env null Environment variables, merged on top of the parent environment.
inherit_env true When false, the subprocess receives only the keys listed in env.
request_timeout 30 Per-call wait for a JSON-RPC response, in seconds. Falls back to the legacy timeout key when only that is set.
process_timeout null Symfony Process kill timer, in seconds. Set this only if you need a hard upper bound on the subprocess lifetime.
startup_delay 100 Milliseconds to wait after Process::start() before sending the initialize handshake. Increase if a cold-start npx -y … is still booting when the handshake fires.
poll_interval 20 Milliseconds between reads of the subprocess output buffer.

Note. The STDIO transport does not work under php artisan serve. The built-in PHP development server tears down its worker between requests, which kills the long-running subprocess. Run your application via Octane, a queue worker, Sail, or Valet to use STDIO servers.

Connecting to a Server

Resolve the client and call connect with a server name from your configuration:

use Redberry\MCPClient\Facades\MCPClient;

$github = MCPClient::connect('github');

The container binds the client as a singleton and aliases the Redberry\MCPClient\Contracts\MCPClient interface to it, so the facade and dependency injection on the contract resolve the same instance:

use Redberry\MCPClient\Contracts\MCPClient;

class GithubToolService
{
    public function __construct(private MCPClient $client) {}

    public function tools()
    {
        return $this->client->connect('github')->tools();
    }
}

connect returns a per-server clone of the client, so you may hold handles to multiple servers at once without one routing through another:

$github = MCPClient::connect('github');
$memory = MCPClient::connect('memory');

$github->tools();
$memory->tools();

Repeated connect calls for the same server reuse a cached transporter, so the initialize handshake is paid once per server per root instance.

Listing Tools and Resources

The tools and resources methods return a Collection of associative arrays:

$tools = $github->tools();

$tools->all();                          // raw array
$tools->only('search', 'create_issue'); // by name
$tools->except('delete_repository');    // by name
$tools->map(fn ($tool) => $tool['name']);

The same Collection wraps both lists, but only and except know which key to filter on — name for tools and uri for resources.

Calling a Tool

Pass the tool name and an associative array of arguments. The method returns the decoded JSON-RPC result array:

$result = $github->callTool('create_issue', [
    'owner' => 'laravel',
    'repo'  => 'framework',
    'title' => 'Documentation feedback',
    'body'  => '',
]);

Reading a Resource

Pass the URI of the resource. The method returns the decoded JSON-RPC result array:

$result = $github->readResource('file:///project/src/main.rs');

Streaming Events

Both callTool and readResource accept an optional callback as the last argument. When the server replies with an SSE stream, the callback is invoked for every decoded JSON-RPC message — progress notifications, log entries, partial results, and the final result-bearing one — as each arrives. The call still blocks until the final result is returned:

$result = $github->callTool('long_running_task', $args, function (array $event) {
    Log::info('mcp event', $event);
});

The callback is invoked zero times when the server replies with a single JSON object, so it is safe to pass unconditionally. Streaming is an HTTP-only concept; the callback is a no-op under the STDIO transport.

Custom Transports

The package ships with HTTP and STDIO transports, and the IO seam is a single interface — Redberry\MCPClient\Core\Transporters\Transporter. To add another, implement the interface, register a case on Redberry\MCPClient\Enums\Transporters, and add a match arm to TransporterFactory::make. See ARCHITECTURE.md and .claude/rules/transporters.md for the full contract.

Testing

composer test

Upgrading

Upgrading from 1.x to 2.x? See UPGRADE.md for the migration guide.

Changelog

See CHANGELOG.md for the list of changes.

Contributing

See CONTRIBUTING.md for guidelines.

Security Vulnerabilities

Please review our security policy before reporting a vulnerability.

Credits

Built and maintained by Redberry, a Diamond-tier Laravel partner. We also run a 5-week AI agent PoC sprint for teams exploring agentic features in Laravel.

License

The MIT License (MIT). Please see LICENSE.md for more information.

redberry/mcp-client-laravel 适用场景与选型建议

redberry/mcp-client-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 83.6k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2025 年 09 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「redberry」 「mcp-client-laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 redberry/mcp-client-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 redberry/mcp-client-laravel 我们能提供哪些服务?
定制开发 / 二次开发

基于 redberry/mcp-client-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 83.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 19
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 13
  • Watchers: 0
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-29