Model Context Protocol

v0.6.0 2025-08-31 20:23 UTC

This package is auto-updated.

Last update: 2025-09-06 10:17:56 UTC


README

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.4+ Supports PHP 8.0+ Latest Stable Version

Implements the Model Context Protocol for the XP Framework.

Client

Connecting to an MCP server:

use io\modelcontextprotocol\McpClient;
use util\cmd\Console;

// Use streamable HTTP
$client= new McpClient('http://localhost:3001');

// Use standard I/O
$client= new McpClient(['docker', 'run', '--rm', '-i', 'mcp/time']);

$response= $client->call('tools/list');
Console::writeLine($response->value());

Server

Uses the xp-forge/web library:

use io\modelcontextprotocol\McpServer;
use io\modelcontextprotocol\server\{Tool, Param};
use web\Application;

class Test extends Application {

  public function routes() {
    return new McpServer([
      'greeting' => new class() {

        /** Sends a greeting */
        #[Tool]
        public function greet(#[Param('Whom to greet')] $name= null) {
          return 'Hello, '.($name ?? 'unknown user');
        }
      }
    ]);
  }
}

Run this via xp -supervise web Test.

Organizing code

MCP tools, resources and prompts may be organized into classes as follows:

namespace com\example\api;

use io\modelcontextprotocol\server\{Resource, Prompt, Tool, Param, Implementation};

#[Implementation]
class Greeting {

  /** Dynamic greeting for a user */
  #[Resource('greeting://user/{name}')]
  public function get($name) {
    return "Hello {$name}";
  }

  /** Greets users */
  #[Prompt]
  public function user(
    #[Param('Whom to greet')] $name,
    #[Param(type: ['type' => 'string', 'enum' => ['casual', 'friendly']])] $style= 'casual'
  ) {
    return "Write a {$style} greeting for {$name}";
  }

  /** Sends a given greeting by email */
  #[Tool]
  public function send(
    #[Param('Recipient email address')] $recipient,
    #[Param('The text to send')] $greeting
  ) {
    // TBI
  }
}}

The web application then becomes this:

use io\modelcontextprotocol\McpServer;
use web\Application;

class Test extends Application {

  public function routes() {
    return new McpServer('com.example.api');
  }
}

See also