- What is the Model Context Protocol (MCP), and why should I use it with Laravel?
- MCP is a standardized protocol for AI agents to interact with applications by exposing tools (functions, models, or APIs) in a structured way. Using it with Laravel lets AI clients (like LLMs or agents) call your Eloquent models, API resources, or custom logic securely, while leveraging Laravel’s conventions for routing, authentication, and validation. It’s ideal for AI-driven workflows where agents need to query or modify your app’s data.
- How do I install and set up Laravel MCP in my project?
- Install via Composer with `composer require laravel/mcp`, then run `php artisan mcp:install` to publish config, routes, and middleware. The package integrates with Laravel’s service providers, so no manual bootstrapping is needed. Configure OAuth (Sanctum or Passport) in the `mcp.php` config file, and you’re ready to register tools. The CLI command handles most boilerplate automatically.
- Which Laravel versions and PHP versions does MCP support?
- Laravel MCP is optimized for Laravel 10 and 11 and requires PHP 8.2+. PHP 8.1 support was dropped in v0.5.4, so ensure your stack meets these requirements. The package aligns with Laravel’s latest features, including Octane for real-time streaming, but test under load if using high-frequency AI requests.
- Can I expose Eloquent models as MCP tools without rewriting them?
- Yes, MCP supports registering Eloquent models as tools using the `#[McpTool]` attribute or manually via the `Mcp::tool()` facade. The package automatically maps model methods to MCP functions, handles relationships, and validates inputs against Laravel’s validation rules. This reduces boilerplate and keeps your tool definitions close to your existing business logic.
- How does authentication work for AI clients using MCP?
- MCP integrates with Laravel Sanctum (default) or Passport for OAuth2 authentication. AI clients authenticate via OAuth tokens, and you can define custom scopes or dynamic client registration (RFC 7591). For internal tools, API keys or JWT may be simpler alternatives, though MCP’s design prioritizes OAuth for security and scalability. Always test token expiration and refresh flows in your CI pipeline.
- What are the performance implications of using MCP in production?
- MCP adds minimal overhead for simple requests, but authenticated OAuth flows and streaming responses (e.g., for chatbots) may introduce latency. Under Laravel Octane, streaming tools require careful tuning to avoid memory leaks or timeouts. Benchmark your tools with realistic AI client loads, and consider caching frequent tool responses or using queue workers for heavy operations.
- How do I handle errors or malformed requests from AI clients?
- MCP converts Laravel exceptions into structured error responses (added in v0.8.1), but complex tool chains may still obscure root causes. Use the `assertStructuredContent` helper in tests to validate error formats, and log MCP traffic with Laravel Horizon or OpenTelemetry for observability. For database deadlocks or custom errors, extend the `McpExceptionHandler` to tailor responses.
- Can I use MCP for real-time applications like chatbots or live updates?
- Yes, MCP supports streaming responses and integrates with Laravel Octane for real-time workflows. Tools can return interactive HTML or structured content via WebSocket or HTTP transports. Test under load to ensure Octane’s event loop handles concurrent streams, and monitor memory usage if tools process large payloads (e.g., audio/image content).
- What alternatives exist to MCP for AI-Laravel integration?
- Alternatives include OpenAPI (for REST/gRPC APIs) or LangChain’s custom interfaces, which may fit better for non-agent workflows. If your AI clients need lightweight interactions, consider Laravel’s built-in API resources or custom JSON-RPC endpoints. MCP shines for agentic workflows where tools must be dynamically discoverable and securely invoked, but weigh its evolving spec against your project’s long-term needs.
- How do I test MCP tools in my CI/CD pipeline?
- Use MCP’s mocking utilities to simulate AI client requests in PHPUnit tests. The `assertStructuredContent` helper validates tool responses, and you can mock OAuth tokens or transports (e.g., HTTP/STDOUT) for isolated testing. For integration tests, spin up a local MCP server with `php artisan mcp:inspect` to debug tool registrations or request/response cycles.