Type-safe PHP DTOs for the Model Context Protocol (MCP) specification.
This package provides comprehensive Data Transfer Objects and Enums that ensure full compliance with the official MCP schema, enabling robust server and client implementations with complete type safety.
π― MCP Schema Version: 2025-03-26 (Latest)
composer require php-mcp/schemaRequirements: PHP 8.1+ β’ No dependencies
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']
],
'required' => ['operation', 'a', 'b']
],
description: 'Performs basic arithmetic operations'
);
// Serialize to JSON
$json = json_encode($tool);
// Deserialize from array
$tool = Tool::fromArray($decodedData);Every MCP protocol type is represented with full validation and type safety.
All DTOs use readonly properties to prevent accidental mutations.
- Factory Methods: Convenient
make()methods for fluent object creation - Array Conversion: Seamless
toArray()andfromArray()methods - JSON Ready: Built-in
JsonSerializableinterface support - Validation: Comprehensive input validation with clear error messages
| Component | Description |
|---|---|
| Tools | Tool definitions with JSON Schema validation |
| Resources | Static and template-based resource representations |
| Prompts | Interactive prompt definitions with arguments |
| Content | Text, image, audio, and blob content types |
| JSON-RPC | Complete JSON-RPC 2.0 protocol implementation |
| Requests/Results | All 15 request types and corresponding responses |
| Notifications | Real-time event notification messages |
| Capabilities | Client and server capability declarations |
// 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'
);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
- β 100% MCP Compliance - Matches official specification exactly
- β Type Safety - Catch errors at development time, not runtime
- β Zero Dependencies - Lightweight and self-contained
- β Production Ready - Immutable, validated, and thoroughly tested
- β Future Proof - Updated with latest MCP specification versions
MIT License. See LICENSE for details.
Part of the PHP MCP ecosystem β’ Build type-safe MCP applications with confidence