A PHP representation of the Model Context Protocol (MCP) schema types.
This package provides Data Transfer Objects (DTOs), Enums, and Unions that mirror the official MCP TypeScript schema.
It is not an SDK, client, or server implementation;
just the type definitions for building your own MCP-compatible applications in PHP.
composer require wordpress/php-mcp-schema
Requires PHP 7.4 or higher.
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'],
],
'required' => ['location'],
],
]);
Convert a DTO to a plain array for JSON encoding:
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
Decode incoming JSON into a fully typed DTO:
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 a factory to resolve polymorphic content blocks without knowing the concrete type up front:
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!"
}
Construct a generic JSON-RPC request for any MCP method:
use WP\McpSchema\Common\JsonRpc\DTO\JSONRPCRequest;
$request = JSONRPCRequest::fromArray([
'jsonrpc' => '2.0',
'id' => 1,
'method' => 'tools/list',
]);
$json = json_encode($request->toArray());
WP\McpSchema\Server\)Tool, CallToolRequest, CallToolResult, ListToolsRequest, ListToolsResultResource, ResourceTemplate, ReadResourceRequest, ReadResourceResultPrompt, PromptMessage, GetPromptRequest, GetPromptResultLoggingMessageNotification, SetLevelRequestWP\McpSchema\Client\)CreateMessageRequest, CreateMessageResult, SamplingMessageElicitRequest, ElicitResultListRootsRequest, ListRootsResult, RootWP\McpSchema\Common\)InitializeRequest, InitializeResult, PingRequestTextContent, ImageContent, AudioContentJSONRPCRequest, JSONRPCNotification, JSONRPCResultResponse, JSONRPCErrorResponseThe PHP code in src/ is auto-generated from the official MCP TypeScript schema. The generator is located in the generator/ directory and is not included in the Composer package.
See generator/README.md for setup and usage instructions.
GPL-2.0-or-later - see LICENSE.md for details.
We use cookies to analyze traffic and improve your experience. You can accept or reject analytics cookies.