GitHub Stars
15
User Rating
Not Rated
Forks
0
Issues
0
Views
1
Favorites
0
README
PHP MCP Schema
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)
Installation
composer require php-mcp/schema
Requirements: PHP 8.1+ ⢠No dependencies
Quick Start
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);
Core Features
šļø Complete Schema Coverage
Every MCP protocol type is represented with full validation and type safety.
š Immutable Design
All DTOs use readonly properties to prevent accidental mutations.
š Developer Experience
- 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
š¦ Schema Components
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 |
Usage Patterns
Creating Protocol Messages
// 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]
);
Working with Resources
// 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'
);
Content Handling
// Text content
$text = TextContent::make('Hello, world!');
// Image content
$image = ImageContent::make(
data: base64_encode($imageData),
mimeType: 'image/png'
);
Package Structure
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
Why Use This Package?
- ā 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
License
MIT License. See LICENSE for details.
Part of the PHP MCP ecosystem ⢠Build type-safe MCP applications with confidence
Author Information
57
Followers
5
Repositories
0
Gists
5
Total Contributions
Top Contributors
Threads