Laravel MCP

by mohamedahmed01

29 440 downloads Not rated yet MIT

About

Laravel Based Implementation for Model Context Protocol

Details

License
MIT

Explore

- Multiple Transport Options
- HTTP Transport (RESTful API)
- WebSocket Transport (Real-time)
- Stdio Transport (Command-line)
- Configurable host and port settings

- Server Capabilities
- Tool registration and execution
- Resource management
- Prompt handling
- Progress tracking
- Logging system
- Model preferences configuration

- Tool System
- Register custom tools with parameters
- Handle tool calls with arguments
- Return structured responses
- Error handling and validation

- Resource Management
- URI-based resource access
- Resource templates
- Dynamic resource handling
- Content type support

- Prompt System
- Template-based prompts
- Dynamic argument handling
- Context management
- Message history support

- Progress Tracking
- Real-time progress updates
- Token-based tracking
- Total progress support
- Client notification system

- Logging System
- Multiple log levels
- Logger identification
- Structured logging
- Transport-agnostic logging

Install via Composer:

composer require laravelmcp/mcp

The package will automatically register its service provider and facade.

use LaravelMCP\MCP\Server\MCPServer;
use LaravelMCP\MCP\Transport\HttpTransport;

// Create and configure server
$server = new MCPServer();

// Configure transport
$transport = new HttpTransport($server, [
'host' => '127.0.0.1',
'port' => 8080
]);

$server->setTransport($transport);
$server->initialize();

// Start the server
$server->start();

use LaravelMCP\MCP\Server\FastMCP;
use LaravelMCP\MCP\Facades\MCP;

// Using FastMCP (recommended)
$mcp = new FastMCP();

// Register a simple calculation tool
$mcp->tool('calculate', [
'num1' => ['type' => 'number', 'required' => true],
'num2' => ['type' => 'number', 'required' => true],
'operation' => ['type' => 'string', 'required' => true]
])(function ($args) {
$num1 = $args['num1'];
$num2 = $args['num2'];
$operation = $args['operation'];

return match ($operation) {
'+' => ['result' => $num1 + $num2],
'-' => ['result' => $num1 - $num2],
'' => ['result' => $num1 $num2],
'/' => $num2 != 0 ? ['result' => $num1 / $num2] : ['error' => 'Division by zero'],
default => ['error' => 'Invalid operation']
};
});

// Register a tool with error handling
$mcp->tool('process-data', [
'items' => ['type' => 'array', 'required' => true],
'token' => ['type' => 'string', 'required' => false]
])(function ($args) use ($mcp) {
try {
$items = $args['items'];
$token = $args['token'] ?? uniqid();

foreach ($items as $index => $item) {
// Process item
$progress = ($index + 1) / count($items);
MCP::sendProgress($progress, $token, count($items));
MCP::sendLog("Processing item {$index + 1}", 'info', 'processor');
}

return ['processed' => count($items)];
} catch (\Exception $e) {
MCP::sendLog($e->getMessage(), 'error', 'processor');
return ['error' => $e->getMessage()];
}
});

A Laravel package for implementing the Model Context Protocol (MCP) in Laravel applications. This protocol facilitates communication between AI models and Laravel applications through standardized interfaces.

:star: We are now Featured on mcphub

📚 View Full Documentation

Requirements

System Requirements

- PHP 8.1 or higher - Laravel 10.x - Composer 2.x - OpenSSL PHP Extension - PDO PHP Extension - Mbstring PHP Extension - XML PHP Extension - Ctype PHP Extension - JSON PHP Extension - ReactPHP (for HTTP/WebSocket transport) - ext-json (for JSON handling)

Optional Requirements

- Redis (for WebSocket scaling) - Supervisor (for process management) - ext-pcntl (for signal handling) - ext-posix (for process management)

Features

- Multiple Transport Options
- HTTP Transport (RESTful API)
- WebSocket Transport (Real-time)
- Stdio Transport (Command-line)
- Configurable host and port settings

- Server Capabilities
- Tool registration and execution
- Resource management
- Prompt handling
- Progress tracking
- Logging system
- Model preferences configuration

- Tool System
- Register custom tools with parameters
- Handle tool calls with arguments
- Return structured responses
- Error handling and validation

- Resource Management
- URI-based resource access
- Resource templates
- Dynamic resource handling
- Content type support

- Prompt System
- Template-based prompts
- Dynamic argument handling
- Context management
- Message history support

- Progress Tracking
- Real-time progress updates
- Token-based tracking
- Total progress support
- Client notification system

- Logging System
- Multiple log levels
- Logger identification
- Structured logging
- Transport-agnostic logging

Documentation

The project includes comprehensive PHPDoc documentation for all classes and methods. The documentation is available directly in the source code and covers:

Core Components

- Server Capabilities (src/Capabilities/ServerCapabilities.php)
- Server feature management
- Experimental features configuration
- Logging configuration
- Component capabilities (prompts, resources, tools)

- Client Capabilities (src/Capabilities/ClientCapabilities.php)
- Feature flag management
- Root directory configuration
- State serialization/deserialization

Component Capabilities

- Tools Capability (src/Capabilities/ToolsCapability.php)
- Tool list change tracking
- State management
- Change notification support

- Resources Capability (src/Capabilities/ResourcesCapability.php)
- Resource subscription management
- Change tracking
- State serialization

- Prompts Capability (src/Capabilities/PromptsCapability.php)
- Prompt list change tracking
- State management
- Change notifications

- Roots Capability (src/Capabilities/RootsCapability.php)
- Root directory management
- Directory configuration
- Access control settings

Server Management

- MCP Server Command (src/Commands/MCPServerCommand.php)
- Server startup configuration
- Transport selection
- Signal handling
- Graceful shutdown

Each component includes:
- Detailed class descriptions
- Feature lists and capabilities
- Configuration examples
- Usage examples
- Method documentation
- Parameter descriptions
- Return value documentation

To explore the documentation:
1. Browse the source files in the src/ directory
2. Use your IDE's PHPDoc integration
3. Generate HTML documentation using phpDocumentor (optional)

To generate HTML documentation:

composer require --dev phpdocumentor/phpdocumentor
vendor/bin/phpdoc -d src/ -t docs/

Installation

Install via Composer:

composer require laravelmcp/mcp

The package will automatically register its service provider and facade.

Configuration

1. Publish the configuration:

php artisan vendor:publish --provider="LaravelMCP\MCP\MCPServiceProvider" --tag="config"

2. Configure environment variables:

MCP_SERVER_HOST=127.0.0.1
MCP_SERVER_PORT=8080
MCP_SERVER_TRANSPORT=http

Basic Usage

Starting the Server

```bash

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

No reviews posted yet.

Videos about Laravel MCP

Relevant YouTube tutorials, setups, and demos