prism-php/relay 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

prism-php/relay

Composer 安装命令:

composer require prism-php/relay

包简介

A Prism tool for interacting with MCP servers

README 文档

README

Total Downloads Latest Stable Version License

Relay

A seamless integration between Prism and Model Context Protocol (MCP) servers that empowers your AI applications with powerful, external tool capabilities.

Installation

You can install the package via Composer:

composer require prism-php/relay

After installation, publish the configuration file:

php artisan vendor:publish --tag="relay-config"

Configuration

The published config file (config/relay.php) is where you'll define your MCP server connections.

Configuring Servers

You must define each MCP server explicitly in the config file. Each server needs a unique name and the appropriate configuration parameters:

return [
    'servers' => [
        'puppeteer' => [
            'command' => ['npx', '-y', '@modelcontextprotocol/server-puppeteer'],
            'timeout' => 30,
            'env' => [],
            'transport' => \Prism\Relay\Enums\Transport::Stdio,
        ],
        'github' => [
            'url' => env('RELAY_GITHUB_SERVER_URL', 'http://localhost:8001/api'),
            'timeout' => 30,
            'transport' => \Prism\Relay\Enums\Transport::Http,
        ],
    ],
    'cache_duration' => env('RELAY_TOOLS_CACHE_DURATION', 60), // in minutes (0 to disable)
];

Basic Usage

Here's how you can integrate MCP tools into your Prism agent:

use Prism\Prism\Prism;
use Prism\Relay\Facades\Relay;
use Prism\Prism\Enums\Provider;

$response = Prism::text()
    ->using(Provider::Anthropic, 'claude-3-7-sonnet-latest')
    ->withPrompt('Find information about Laravel on the web')
    ->withTools(Relay::tools('puppeteer'))
    ->asText();

return $response->text;

The agent can now use any tools provided by the Puppeteer MCP server, such as navigating to webpages, taking screenshots, clicking buttons, and more.

Real-World Example

Here's a practical example of creating a Laravel command that uses MCP tools with Prism:

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Prism\Relay\Facades\Relay;
use Prism\Prism\Enums\Provider;
use Prism\Prism\Prism;
use Prism\Prism\Text\PendingRequest;

use function Laravel\Prompts\note;
use function Laravel\Prompts\textarea;

class MCP extends Command
{
    protected $signature = 'prism:mcp';

    public function handle()
    {
        $response = $this->agent(textarea('Prompt'))->asText();

        note($response->text);
    }

    protected function agent(string $prompt): PendingRequest
    {
        return Prism::text()
            ->using(Provider::Anthropic, 'claude-3-7-sonnet-latest')
            ->withSystemPrompt(view('prompts.nova-v2'))
            ->withPrompt($prompt)
            ->withTools([
                ...Relay::tools('puppeteer'),
            ])
            ->usingTopP(1)
            ->withMaxSteps(99)
            ->withMaxTokens(8192);
    }
}

This command creates an interactive CLI that lets you input prompts that will be sent to Claude. The agent can use Puppeteer tools to browse the web, complete tasks, and return the results.

Transport Types

Relay supports multiple transport mechanisms:

HTTP Transport

For MCP servers that communicate over HTTP:

'github' => [
    'url' => env('RELAY_GITHUB_SERVER_URL', 'http://localhost:8000/api'),
    'api_key' => env('RELAY_GITHUB_SERVER_API_KEY'),
    'timeout' => 30,
    'transport' => Transport::Http,
    'headers' => [
        'User-Agent' => 'prism-php-relay/1.0',
    ]
],

OAuth / Bearer Token Authentication

The MCP 2025-11-25 authorization spec defines an OAuth 2.1 flow for HTTP-based transports. Relay handles only the last step — attaching the token to every request. Your application is responsible for running the full OAuth flow and supplying the resulting access token at runtime.

What your application must handle

Before calling withToken(), your code must:

  1. Discover the authorization server — make an unauthenticated request to the MCP server and read the WWW-Authenticate header from the 401 response, or probe /.well-known/oauth-protected-resource. The response contains the authorization server URL.
  2. Run the OAuth 2.1 authorization code flow — including PKCE (S256 code challenge is required by the spec) and the resource parameter (RFC 8707) identifying the MCP server.
  3. Exchange the code for a token and store/refresh it as needed. Tokens are short-lived; implement refresh token rotation.

Note

The spec covers this in detail: MCP Authorization — 2025-11-25.

Passing the token to Relay

Once you have a valid Bearer token, pass it to Relay at request time. The runtime token takes priority over any static api_key set in config.

use Prism\Relay\Facades\Relay;
use Prism\Relay\Exceptions\AuthorizationException;
use Prism\Relay\Exceptions\TransportException;

// Via the Facade (returns a RelayBuilder)
$tools = Relay::withToken($request->user()->mcp_token)->tools('github');

// Or on a Relay instance
$relay = Relay::make('github');
$tools = $relay->withToken($request->user()->mcp_token)->tools();

When the MCP server rejects the token with HTTP 401, Relay throws AuthorizationException. Use this to trigger a re-authorization flow in your app:

try {
    $tools = Relay::withToken($token)->tools('github');
} catch (AuthorizationException $e) {
    // Token is missing, expired, or rejected (HTTP 401)
    // Re-run the OAuth flow to get a fresh token
    return redirect('/oauth/reconnect');
} catch (TransportException $e) {
    // Other transport failure (non-401)
    Log::error('MCP Transport error: ' . $e->getMessage());
}

Note

OAuth tokens are only supported with HTTP transport. Passing a token to a Stdio-configured server throws a ServerConfigurationException. For Stdio servers, provide credentials via the env config key instead.

STDIO Transport

For locally running MCP servers that communicate via standard I/O:

'puppeteer' => [
    'command' => [
      'npx',
      '-y',
      '@modelcontextprotocol/server-puppeteer',
      '--options',
      // Array values are passed as JSON encoded strings
      [
        'debug' => env('MCP_PUPPETEER_DEBUG', false)
      ]
    ],
    'timeout' => 30,
    'transport' => Transport::Stdio,
    'env' => [
        'NODE_ENV' => 'production',  // Set Node environment
        'MCP_SERVER_PORT' => '3001',  // Set a custom port for the server
    ],
],

Note

The STDIO transport launches a subprocess and communicates with it through standard input/output. This is perfect for running tools directly on your application server.

Tip

The env option allows you to pass environment variables to the MCP server process. This is useful for configuring server behavior, enabling debugging, or setting authentication details.

Advanced Usage

Using Multiple MCP Servers

You can combine tools from multiple MCP servers in a single Prism agent:

use Prism\Prism\Prism;
use Prism\Relay\Facades\Relay;
use Prism\Prism\Enums\Provider;

$response = Prism::text()
    ->using(Provider::Anthropic, 'claude-3-7-sonnet-latest')
    ->withTools([
        ...Relay::tools('github'),
        ...Relay::tools('puppeteer')
    ])
    ->withPrompt('Find and take screenshots of Laravel repositories')
    ->asText();

Error Handling

The package uses specific exception types for better error handling:

use Prism\Relay\Exceptions\AuthorizationException;
use Prism\Relay\Exceptions\RelayException;
use Prism\Relay\Exceptions\ServerConfigurationException;
use Prism\Relay\Exceptions\ToolCallException;
use Prism\Relay\Exceptions\ToolDefinitionException;
use Prism\Relay\Exceptions\TransportException;

try {
    $tools = Relay::tools('puppeteer');
    // Use the tools...
} catch (ServerConfigurationException $e) {
    // Handle configuration errors (missing server, invalid settings)
    Log::error('MCP Server configuration error: ' . $e->getMessage());
} catch (ToolDefinitionException $e) {
    // Handle issues with tool definitions from the MCP server
    Log::error('MCP Tool definition error: ' . $e->getMessage());
} catch (AuthorizationException $e) {
    // Handle HTTP 401 — token is missing, expired, or invalid
    return redirect('/oauth/reconnect');
} catch (TransportException $e) {
    // Handle communication errors with the MCP server
    Log::error('MCP Transport error: ' . $e->getMessage());
} catch (ToolCallException $e) {
    // Handle errors when calling a specific tool
    Log::error('MCP Tool call error: ' . $e->getMessage());
} catch (RelayException $e) {
    // Handle any other MCP-related errors
    Log::error('Relay general error: ' . $e->getMessage());
}

License

The MIT License (MIT). Please see the License File for more information.

prism-php/relay 适用场景与选型建议

prism-php/relay 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 70.6k 次下载、GitHub Stars 达 150, 最近一次更新时间为 2025 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 prism-php/relay 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 70.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 150
  • 点击次数: 23
  • 依赖项目数: 2
  • 推荐数: 1

GitHub 信息

  • Stars: 150
  • Watchers: 4
  • Forks: 20
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-02