1. Go to this page and download the library: Download fastmcphp/fastmcphp 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/ */
fastmcphp / fastmcphp example snippets
use Fastmcphp\Fastmcphp;
$mcp = new Fastmcphp('My Server');
// Register a tool
$mcp->tool(
callable: fn(string $text) => $text,
name: 'echo',
description: 'Echo the input text',
);
// Register a resource
$mcp->resource(
uri: 'config://app',
callable: fn() => ['version' => '1.0.0'],
name: 'config',
);
// Run with stdio transport
$mcp->run();
use Fastmcphp\Prompts\Message;
$mcp->prompt(
callable: fn(string $topic) => [
Message::user("Explain {$topic} in simple terms"),
],
name: 'explain',
);
// Stdio (default) - for subprocess communication
$mcp->run(transport: 'stdio');
// HTTP - JSON-RPC over HTTP (falls back to built-in PHP server without Swoole)
$mcp->run(transport: 'http', host: '0.0.0.0', port: 8080);
// SSE - Server-Sent Events (deprecated, use HTTP instead)
$mcp->run(transport: 'sse', host: '0.0.0.0', port: 8080);
use Fastmcphp\Server\Auth\AuthProviderInterface;
use Fastmcphp\Server\Auth\AuthRequest;
use Fastmcphp\Server\Auth\AuthResult;
use Fastmcphp\Server\Auth\AuthenticatedUser;
class MyAuthProvider implements AuthProviderInterface
{
public function authenticate(AuthRequest $request): AuthResult
{
$token = $request->getBearerToken();
if (!$token) {
return AuthResult::unauthenticated();
}
// Validate token against your auth system
$user = $this->validateToken($token);
if (!$user) {
return AuthResult::failed('Invalid token');
}
return AuthResult::success(new AuthenticatedUser(
id: $user['id'],
name: $user['name'],
level: $user['level'],
scopes: $user['scopes'],
workspace: $user['workspace'],
));
}
}
// Use the auth provider
$mcp->setAuth(new MyAuthProvider(),