relay

MCP.Pizza Chef: prism-php

Relay is an MCP client that bridges Prism with MCP servers, enabling AI applications to leverage external tool capabilities efficiently. It simplifies integration by providing a ready-to-install package with configuration support, empowering developers to extend AI workflows with real-time, structured context and tool interactions across MCP-enabled environments.

Use This MCP client To

Integrate Prism-based AI apps with multiple MCP servers Enable AI models to access external tools via MCP protocol Manage context flow between Prism and MCP servers Extend AI workflows with real-time external data and tools Simplify configuration and deployment of MCP client in PHP apps

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

Arc 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,
],

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\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 (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.

relay FAQ

How do I install Relay in my project?
Install Relay via Composer using 'composer require prism-php/relay' and publish the config with 'php artisan vendor:publish --tag="relay-config"'.
What is the main function of Relay?
Relay acts as an MCP client that connects Prism to MCP servers, enabling AI applications to use external tools and data sources.
Can Relay work with different MCP servers simultaneously?
Yes, Relay is designed to integrate Prism with multiple MCP servers, facilitating complex AI workflows.
Is Relay limited to PHP environments?
Relay is a PHP package primarily for PHP applications using Prism, but it interacts with MCP servers which can be platform-agnostic.
How does Relay enhance AI application capabilities?
By enabling seamless communication with MCP servers, Relay allows AI models to access external tools and real-time context.
Does Relay require additional configuration after installation?
Yes, after installation, you need to publish and customize the configuration file to connect to your MCP servers.
Is Relay compatible with major LLM providers?
Relay supports integration with MCP servers that can connect to LLMs like OpenAI, Claude, and Gemini through the MCP protocol.