Laravel MCP
Comprehensive guide for building MCP (Model Context Protocol) servers with Laravel. Contains 7 rules across 5 categories for exposing tools, prompts, and resources to AI clients.
When to Apply
Reference these guidelines when:
- Creating MCP servers (web or local)
- Building tools that AI clients can call
- Defining prompts for reusable AI interactions
- Exposing resources for AI context
- Protecting MCP servers with OAuth or Sanctum
- Testing MCP servers, tools, prompts, and resources
Rule Categories by Priority
| Priority |
Category |
Impact |
Prefix |
| 1 |
Servers |
CRITICAL |
server- |
| 2 |
Tools |
HIGH |
tool- |
| 3 |
Prompts & Resources |
MEDIUM |
prompt-, resource- |
| 4 |
Authentication |
HIGH |
auth- |
| 5 |
Testing |
HIGH |
test- |
Quick Reference
1. Servers (CRITICAL)
server-create-register - Create and register web or local MCP servers
2. Tools (HIGH)
tool-create - Create tools with input/output schemas, DI, and annotations
tool-responses - Text, error, structured, streaming, and multi-content responses
3. Prompts & Resources (MEDIUM)
prompt-create - Create prompts with arguments, validation, and responses
resource-create - Create resources and resource templates with URI patterns
4. Authentication (HIGH)
auth-protect - Protect servers with OAuth 2.1, Sanctum, or custom auth
5. Testing (HIGH)
test-unit - Test with MCP Inspector and unit tests
Essential Patterns
Creating an MCP Server
<?php
namespace App\Mcp\Servers;
use Laravel\Mcp\Server;
use Laravel\Mcp\Server\Attributes\Instructions;
use Laravel\Mcp\Server\Attributes\Name;
use Laravel\Mcp\Server\Attributes\Version;
#[Name('Weather Server')]
#[Version('1.0.0')]
#[Instructions('This server provides weather information.')]
class WeatherServer extends Server
{
protected array $tools = [
CurrentWeatherTool::class,
];
protected array $resources = [];
protected array $prompts = [];
}
Registering in routes/ai.php
use App\Mcp\Servers\WeatherServer;
use Laravel\Mcp\Facades\Mcp;
Mcp::web('/mcp/weather', WeatherServer::class);
Mcp::local('weather', WeatherServer::class);
Creating a Tool
<?php
namespace App\Mcp\Tools;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Attributes\Description;
use Laravel\Mcp\Server\Tool;
#[Description('Fetches the current weather for a location.')]
class CurrentWeatherTool extends Tool
{
public function handle(Request $request): Response
{
$location = $request->get('location');
return Response::text("The weather in {$location} is sunny, 72°F.");
}
public function schema(JsonSchema $schema): array
{
return [
'location' => $schema->string()
->description('The location to get the weather for.')
->required(),
];
}
}
How to Use
Read individual rule files for detailed explanations and code examples.
Each rule file contains:
- YAML frontmatter with metadata (title, impact, tags)
- Brief explanation of why it matters
- Bad Example with explanation
- Good Example with explanation
- Laravel 13 and PHP 8.3 specific context and references
Full Compiled Document
For the complete guide with all rules expanded: AGENTS.md
1---2name: laravel-mcp3description: Laravel MCP server development. Use when building MCP servers, tools, prompts, or resources for AI client integration. Triggers on tasks involving laravel/mcp, MCP tools, MCP prompts, MCP resources, or AI client protocols.4license: MIT5---67# Laravel MCP89Comprehensive guide for building MCP (Model Context Protocol) servers with Laravel. Contains 7 rules across 5 categories for exposing tools, prompts, and resources to AI clients.1011## When to Apply1213Reference these guidelines when:14- Creating MCP servers (web or local)15- Building tools that AI clients can call16- Defining prompts for reusable AI interactions17- Exposing resources for AI context18- Protecting MCP servers with OAuth or Sanctum19- Testing MCP servers, tools, prompts, and resources2021## Rule Categories by Priority2223| Priority | Category | Impact | Prefix |24|----------|----------|--------|--------|25| 1 | Servers | CRITICAL | `server-` |26| 2 | Tools | HIGH | `tool-` |27| 3 | Prompts & Resources | MEDIUM | `prompt-`, `resource-` |28| 4 | Authentication | HIGH | `auth-` |29| 5 | Testing | HIGH | `test-` |3031## Quick Reference3233### 1. Servers (CRITICAL)3435- `server-create-register` - Create and register web or local MCP servers3637### 2. Tools (HIGH)3839- `tool-create` - Create tools with input/output schemas, DI, and annotations40- `tool-responses` - Text, error, structured, streaming, and multi-content responses4142### 3. Prompts & Resources (MEDIUM)4344- `prompt-create` - Create prompts with arguments, validation, and responses45- `resource-create` - Create resources and resource templates with URI patterns4647### 4. Authentication (HIGH)4849- `auth-protect` - Protect servers with OAuth 2.1, Sanctum, or custom auth5051### 5. Testing (HIGH)5253- `test-unit` - Test with MCP Inspector and unit tests5455## Essential Patterns5657### Creating an MCP Server5859```php60<?php6162namespace App\Mcp\Servers;6364use Laravel\Mcp\Server;65use Laravel\Mcp\Server\Attributes\Instructions;66use Laravel\Mcp\Server\Attributes\Name;67use Laravel\Mcp\Server\Attributes\Version;6869#[Name('Weather Server')]70#[Version('1.0.0')]71#[Instructions('This server provides weather information.')]72class WeatherServer extends Server73{74 protected array $tools = [75 CurrentWeatherTool::class,76 ];7778 protected array $resources = [];7980 protected array $prompts = [];81}82```8384### Registering in routes/ai.php8586```php87use App\Mcp\Servers\WeatherServer;88use Laravel\Mcp\Facades\Mcp;8990Mcp::web('/mcp/weather', WeatherServer::class);91Mcp::local('weather', WeatherServer::class);92```9394### Creating a Tool9596```php97<?php9899namespace App\Mcp\Tools;100101use Illuminate\Contracts\JsonSchema\JsonSchema;102use Laravel\Mcp\Request;103use Laravel\Mcp\Response;104use Laravel\Mcp\Server\Attributes\Description;105use Laravel\Mcp\Server\Tool;106107#[Description('Fetches the current weather for a location.')]108class CurrentWeatherTool extends Tool109{110 public function handle(Request $request): Response111 {112 $location = $request->get('location');113114 return Response::text("The weather in {$location} is sunny, 72°F.");115 }116117 public function schema(JsonSchema $schema): array118 {119 return [120 'location' => $schema->string()121 ->description('The location to get the weather for.')122 ->required(),123 ];124 }125}126```127128## How to Use129130Read individual rule files for detailed explanations and code examples.131132Each rule file contains:133- YAML frontmatter with metadata (title, impact, tags)134- Brief explanation of why it matters135- Bad Example with explanation136- Good Example with explanation137- Laravel 13 and PHP 8.3 specific context and references138139## Full Compiled Document140141For the complete guide with all rules expanded: `AGENTS.md`