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/schema
Requirements: 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
JsonSerializable
interface 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