Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🤖 Add Model Context Protocol (MCP) Support
Summary
This PR introduces comprehensive Model Context Protocol (MCP) support to Laravel Restify, enabling
AI agents to interact with your REST API resources through structured tool interfaces. This
integration provides powerful AI-driven data access and manipulation while maintaining security and
authorization controls.
What's New
✨ MCP Server Integration
🛠 CRUD Operations as MCP Tools
🎯 Token Usage Optimization
🔒 Authorization & Security
⚡ Actions & Getters Integration
Key Features
use Binaryk\LaravelRestify\MCP\RestifyServer;
use Laravel\Mcp\Facades\Mcp;
// Basic registration
Mcp::web('restify', RestifyServer::class)->name('mcp.restify');
// With authentication and middleware
Mcp::web('restify', RestifyServer::class)->middleware([
'auth:sanctum',
'verified',
])->name('mcp.restify');
use Binaryk\LaravelRestify\MCP\Concerns\HasMcpTools;
class UserRepository extends Repository
{
use HasMcpTools;
}
// 70% token reduction for index operations
public function fieldsForMcpIndex(RestifyRequest $request): array
{
return [
Field::make('title'),
Field::make('author_name'),
Field::make('created_at'),
Field::make('status'),
];
}
// AI-specific metadata for detailed views
public function fieldsForMcpShow(RestifyRequest $request): array
{
return [
Field::make('title'),
Field::make('content'),
Field::make('reading_time'),
Field::make('topic_classification'),
Field::make('sentiment_score'),
];
}
// Actions automatically become MCP tools
public function actions(RestifyRequest $request): array
{
return [
PublishPostAction::make(),
BulkUpdateStatusAction::make(),
];
}
// Getters provide AI with analytical data
public function getters(RestifyRequest $request): array
{
return [
PostAnalyticsGetter::make(),
PopularPostsGetter::make(),
];
}
Security & Authorization
🔐 Authorization Guarantees
Performance Benefits
📊 Token Usage Optimization
Breaking Changes
None. This is a purely additive feature that doesn't affect existing functionality.
Requirements
Usage Example
// 1. Add MCP support to repository
class PostRepository extends Repository
{
use HasMcpTools;
}
// 2. Register MCP server
Mcp::web('restify', RestifyServer::class)->middleware(['auth:sanctum']);
// 3. AI agents can now use tools like:
// - posts-index-tool (list posts)
// - posts-show-tool (get specific post)
// - posts-store-tool (create new post)
// - posts-update-tool (update post)
// - posts-delete-tool (delete post)
Documentation
Complete documentation available in docs-v2/content/en/mcp/mcp.md covering:
Testing