PHP code example of sw9t / php-mcp-tool-definition

1. Go to this page and download the library: Download sw9t/php-mcp-tool-definition 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/ */

    

sw9t / php-mcp-tool-definition example snippets


use McpToolDefinition\McpProperty;
use McpToolDefinition\McpSchema;
use McpToolDefinition\PropertyType;
use McpToolDefinition\Latest\McpToolDefinition;
use McpToolDefinition\Latest\McpToolAnnotations;
use McpToolDefinition\Latest\McpToolExecution;
use McpToolDefinition\V20251125\TaskSupport;  // enum — import from version namespace

$tool = new McpToolDefinition(
    name:        'search_products',
    description: 'Searches the product catalogue and returns matching items',
    inputSchema: new McpSchema([
        McpProperty::string('query', 'Search term', 

use McpToolDefinition\Latest\McpToolDefinition;   // → V20251125\McpToolDefinition
use McpToolDefinition\Latest\McpToolAnnotations;  // → V20250326\McpToolAnnotations
use McpToolDefinition\Latest\McpToolExecution;    // → V20251125\McpToolExecution
use McpToolDefinition\V20251125\TaskSupport;      // enum — PHP cannot extend enums,
                                                  // import directly from version namespace

use McpToolDefinition\McpSchema;
use McpToolDefinition\McpProperty;

$schema = new McpSchema([
    McpProperty::string('name', 'Full name', 

use McpToolDefinition\V20241105\McpToolDefinition;

new McpToolDefinition(
    name:        string,      // � defaults to empty schema
)

use McpToolDefinition\V20250326\McpToolDefinition;
use McpToolDefinition\V20250326\McpToolAnnotations;

new McpToolDefinition(/* ... */, annotations: McpToolAnnotations)

new McpToolAnnotations(
    title:           ?string,  // display name shown in UIs
    readOnlyHint:    ?bool,    // true → tool never modifies state
    destructiveHint: ?bool,    // true → tool may make irreversible changes
    idempotentHint:  ?bool,    // true → repeated calls with same args are safe
    openWorldHint:   ?bool,    // true → tool may call external systems
)

use McpToolDefinition\V20250618\McpToolDefinition;

new McpToolDefinition(/* ... */, title: ?string, outputSchema: ?McpSchema)

use McpToolDefinition\V20251125\McpToolDefinition;
use McpToolDefinition\V20251125\McpToolExecution;
use McpToolDefinition\V20251125\TaskSupport;

new McpToolDefinition(/* ... */, execution: ?McpToolExecution)

new McpToolExecution(taskSupport: TaskSupport)

TaskSupport::Forbidden  // synchronous only (default)
TaskSupport::Optional   // client chooses sync or task
TaskSupport::Required   // task polling 

use McpToolDefinition\V20241105\McpToolDefinition;

$tool = new McpToolDefinition(
    name:        'ping',
    description: 'Returns server uptime and current timestamp',
);

// toArray() output:
// {
//   "name":        "ping",
//   "description": "Returns server uptime and current timestamp",
//   "inputSchema": { "type": "object", "properties": {} }
// }

use McpToolDefinition\McpProperty;
use McpToolDefinition\McpSchema;
use McpToolDefinition\V20241105\McpToolDefinition;

$tool = new McpToolDefinition(
    name:        'get_user',
    description: 'Retrieves a user by ID',
    inputSchema: new McpSchema([
        McpProperty::string('id',     'User UUID',             

// String enum
McpProperty::string(
    name:        'status',
    description: 'Filter by order status',
    enum:        ['pending', 'processing', 'shipped', 'delivered', 'cancelled'],
)

// Integer enum
McpProperty::integer(
    name:        'mode',
    description: '0 = 

McpProperty::integer('limit',   'Results per page',        minimum: 1,   maximum: 100)
McpProperty::integer('offset',  'Pagination offset',       minimum: 0)
McpProperty::number('score',    'Relevance score (0–1)',   minimum: 0.0, maximum: 1.0)
McpProperty::string('slug',     'URL-friendly identifier', maxLength: 100)

// Simple array of strings — emits "items": {"type": "string"}
McpProperty::array('tags', PropertyType::StringType, 'List of tags')

// Array of integers with length constraints
McpProperty::array('ids', PropertyType::IntegerType, 'Selected IDs', minItems: 1, maxItems: 50)

use McpToolDefinition\McpProperty;
use McpToolDefinition\McpSchema;

McpProperty::object(
    name:       'address',
    properties: new McpSchema([
        McpProperty::string('street', 'Street and house number',  alpha-2 code'),
    ]),
    description: 'Shipping address',
    

McpProperty::array(
    name:       'line_items',
    items:      new McpSchema([
        McpProperty::string('sku', 'Product SKU',  ]),
    description: 'Items in the order',
    

use McpToolDefinition\V20250326\McpToolAnnotations;
use McpToolDefinition\V20250326\McpToolDefinition;

// Read-only search — safe to call at any time
$tool = new McpToolDefinition(
    name:        'search_logs',
    description: 'Searches application logs',
    annotations: new McpToolAnnotations(
        title:        'Search Logs',
        readOnlyHint: true,
    ),
);

// Dangerous delete — client should warn the user
$tool = new McpToolDefinition(
    name:        'delete_account',
    description: 'Permanently deletes a user account and all associated data',
    annotations: new McpToolAnnotations(
        title:           'Delete Account',
        destructiveHint: true,
        idempotentHint:  false,
    ),
);

use McpToolDefinition\McpProperty;
use McpToolDefinition\McpSchema;
use McpToolDefinition\V20250618\McpToolDefinition;

$tool = new McpToolDefinition(
    name:        'create_invoice',
    description: 'Creates and returns a new invoice',
    title:       'Create Invoice',
    inputSchema: new McpSchema([
        McpProperty::string('customer_id', 'Customer ID',        ]),
);

use McpToolDefinition\V20251125\McpToolDefinition;
use McpToolDefinition\V20251125\McpToolExecution;
use McpToolDefinition\V20251125\TaskSupport;

// Tool that always needs task polling (e.g. a 30-second data export)
$tool = new McpToolDefinition(
    name:        'export_report',
    description: 'Generates and exports a full sales report — may take up to 2 minutes',
    execution:   new McpToolExecution(TaskSupport::Required),
);

// Tool that supports both sync and task modes
$tool = new McpToolDefinition(
    name:        'send_email',
    description: 'Sends an email via the configured SMTP provider',
    execution:   new McpToolExecution(TaskSupport::Optional),
);

use McpToolDefinition\McpProperty;
use McpToolDefinition\McpSchema;
use McpToolDefinition\Latest\McpToolAnnotations;
use McpToolDefinition\Latest\McpToolDefinition;
use McpToolDefinition\Latest\McpToolExecution;
use McpToolDefinition\V20251125\TaskSupport;

$tool = new McpToolDefinition(
    name:        'process_order',
    description: 'Validates, charges, and fulfils an order. May take 10–30 seconds.',
    inputSchema: new McpSchema([
        McpProperty::string('order_id',    'Order identifier',      ID'),
    ]),
    execution: new McpToolExecution(TaskSupport::Optional),
);

echo json_encode($tool->toArray(), JSON_PRETTY_PRINT);