1. Go to this page and download the library: Download xp-forge/mcp library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xp-forge / mcp example snippets
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());
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');
}
}
]);
}
}
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
}
}}
use io\modelcontextprotocol\McpServer;
use web\Application;
class Test extends Application {
public function routes() {
return new McpServer('com.example.api');
}
}
use io\modelcontextprotocol\McpServer;
use io\modelcontextprotocol\server\{ImplementationsIn, OAuth2Gateway, Clients, UseSessions};
use web\Application;
use web\session\InFileSystem;
class Test extends Application {
public function routes() {
$clients= new class() extends Clients { /* TBI */ };
$sessions= (new InFileSystem())->named('oauth');
$gateway= new OAuth2Gateway('/oauth', $clients, new UseSessions($sessions));
$auth= /* Some class extending web.auth.Authentication */;
$server= new McpServer(new ImplementationsIn('impl'));
return [
'/mcp' => $gateway->authenticate($server),
'/oauth' => $gateway->flow($auth, $sessions),
'/.well-known/oauth-protected-resource' => $gateway->resource(),
'/.well-known/oauth-authorization-server' => $gateway->metadata(),
];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.