PHP code example of wordpress / php-mcp-schema

1. Go to this page and download the library: Download wordpress/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/ */

    

wordpress / php-mcp-schema example snippets


use WP\McpSchema\Server\Tools\DTO\Tool;

$tool = Tool::fromArray([
    'name' => 'get_weather',
    'description' => 'Get current weather for a location',
    'inputSchema' => [
        'type' => 'object',
        'properties' => [
            'location' => ['type' => 'string', 'description' => 'City name'],
        ],
        '

use WP\McpSchema\Server\Tools\DTO\Tool;

$tool = Tool::fromArray([
    'name' => 'get_weather',
    'description' => 'Get current weather for a location',
    'inputSchema' => ['type' => 'object', 'properties' => []],
]);

$array = $tool->toArray();
$json  = json_encode($array); // Ready to send over the wire

use WP\McpSchema\Server\Tools\DTO\CallToolRequest;

$json = '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"get_weather","arguments":{"location":"Paris"}}}';
$data = json_decode($json, true);

$request   = CallToolRequest::fromArray($data);
$tool_name = $request->getTypedParams()->getName(); // "get_weather"
$arguments = $request->getTypedParams()->getArguments(); // ['location' => 'Paris']

use WP\McpSchema\Common\Protocol\Factory\ContentBlockFactory;
use WP\McpSchema\Common\Content\DTO\TextContent;

$block = ContentBlockFactory::fromArray(['type' => 'text', 'text' => 'Hello, world!']);

// $block implements ContentBlockInterface; cast when you need the concrete API
if ($block instanceof TextContent) {
    echo $block->getText(); // "Hello, world!"
}

use WP\McpSchema\Common\JsonRpc\DTO\JSONRPCRequest;

$request = JSONRPCRequest::fromArray([
    'jsonrpc' => '2.0',
    'id'      => 1,
    'method'  => 'tools/list',
]);

$json = json_encode($request->toArray());
bash
composer