Tech Verse Logo
Enable dark mode
Exploring Laravel MCP: Build AI-Ready Servers with Ease

Exploring Laravel MCP: Build AI-Ready Servers with Ease

Tech Verse Daily

Tech Verse Daily

4 min read

The Laravel team has introduced Laravel MCP, now in public beta. This package provides a smooth way to build Model Context Protocol (MCP) servers inside your Laravel projects.

Following in the footsteps of Laravel Boost, MCP continues Laravel’s trend of making AI-focused development both enjoyable and productive.

What is Laravel MCP?

At its core, Laravel MCP gives you the building blocks to let AI clients talk to your Laravel application. Instead of wiring everything from scratch, you can define servers, tools, resources, and prompts that form the backbone of your AI-driven workflows.

Defining an MCP Server

An MCP server acts as the hub for communication. Here’s a minimal example:

namespace App\Mcp\Servers;
 
use Laravel\Mcp\Server;
 
class WeatherServer extends Server
{
    /**
     * The tools registered with this MCP server.
     *
     * @var array<int, class-string<\Laravel\Mcp\Server\Tool>>
     */
    protected array $tools = [
        // ExampleTool::class,
    ];
 
    /**
     * The resources registered with this MCP server.
     *
     * @var array<int, class-string<\Laravel\Mcp\Server\Resource>>
     */
    protected array $resources = [
        // ExampleResource::class,
    ];
 
    /**
     * The prompts registered with this MCP server.
     *
     * @var array<int, class-string<\Laravel\Mcp\Server\Prompt>>
     */
    protected array $prompts = [
        // ExamplePrompt::class,
    ];
}

Each section (tools, resources, prompts) represents capabilities that your server can provide to connected AI clients.

Adding a Tool

Tools are where the action happens. Here’s a simplified example of a weather-fetching tool:

namespace App\Mcp\Tools;
 
use Illuminate\JsonSchema\JsonSchema;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Tool;
 
class CurrentWeatherTool extends Tool
{
    protected string $description = 'Fetches the current weather forecast for a specified location.';
 
    public function handle(Request $request): Response
    {
        $location = $request->get('location');
 
        // Get weather...
 
        return Response::text('The weather is...');
    }
 
    public function schema(JsonSchema $schema): array
    {
        return [
            'location' => $schema->string()
                ->description('The location to get the weather for.')
                ->required(),
        ];
    }
}

Notice how the schema method defines the input requirements, and the handle method processes requests and returns responses.