PHP code example of php-mcp / schema
1. Go to this page and download the library: Download php-mcp/schema 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/ */
php-mcp / schema example snippets
use PhpMcp\Schema\Tool;
use PhpMcp\Schema\Resource;
use PhpMcp\Schema\Request\CallToolRequest;
// Create a tool definition
$tool = Tool::make(
name: 'calculator',
inputSchema: [
'type' => 'object',
'properties' => [
'operation' => ['type' => 'string'],
'a' => ['type' => 'number'],
'b' => ['type' => 'number']
],
'
// Initialize request
$request = InitializeRequest::make(
protocolVersion: '2025-03-26',
capabilities: ClientCapabilities::make(),
clientInfo: Implementation::make('MyClient', '1.0.0')
);
// Call tool request
$callRequest = CallToolRequest::make(
name: 'calculator',
arguments: ['operation' => 'add', 'a' => 5, 'b' => 3]
);
// Static resource
$resource = Resource::make(
uri: '/data/users.json',
name: 'User Database',
description: 'Complete user registry'
);
// Resource template
$template = ResourceTemplate::make(
uriTemplate: '/users/{id}',
name: 'User Profile',
description: 'Individual user data'
);
// Text content
$text = TextContent::make('Hello, world!');
// Image content
$image = ImageContent::make(
data: base64_encode($imageData),
mimeType: 'image/png'
);
bash
composer
src/
├── Content/ # Content types (Text, Image, Audio, Blob, etc.)
├── Enum/ # Protocol enums (LoggingLevel, Role)
├── JsonRpc/ # JSON-RPC 2.0 implementation
├── Notification/ # Event notification types
├── Request/ # Protocol request messages
├── Result/ # Protocol response messages
├── Tool.php # Tool definitions
├── Resource.php # Resource representations
├── Prompt.php # Prompt definitions
└── ... # Core protocol types