What is MCP?
The Model Context Protocol (MCP) is a specification for AI clients (such as Claude, Cursor, and GitHub Copilot) to communicate with applications through a standardized protocol. By implementing an MCP server, you can allow AI agents to access data in your Laravel application or perform actions on your behalf.Laravel MCP is an official package added in Laravel 13. Distributed as
laravel/mcp, it provides the features you need to build an MCP server.Installation
Install the package via Composer.vendor:publish Artisan command to generate the routes/ai.php file.
routes/ai.php file. This is where you register your MCP servers.
Creating a server
Generate a server class with themake:mcp-server Artisan command.
app/Mcp/Servers directory.
Registering a server
Once you’ve created a server, register it inroutes/ai.php. There are two registration types: web servers and local servers.
Web servers
A web server is accessed via HTTP POST requests. This is ideal for remote AI clients or web-based integrations.Local servers
A local server runs as an Artisan command. Use it to integrate with local AI clients like Claude Desktop.Tools
A tool is a function the AI client can call. You can implement data retrieval, integrations with external APIs, database operations, and more.Creating a tool
Generate a tool class with themake:mcp-tool Artisan command.
$tools property.
Tool name and description
Default names and titles are generated from the class name.CurrentWeatherTool becomes name current-weather and title Current Weather Tool. You can customize both with the Name and Title attributes.
Input schema
Use theschema method to define the input parameter schema. You can specify types and constraints using Laravel’s JSON schema builder.
Output schema
TheoutputSchema method lets you define the structure of the response. This makes it easier for AI clients to parse the response.
Validation
You can use Laravel’s standard validation features inside thehandle method.
Dependency injection
Tools are resolved through Laravel’s service container, so you can type-hint dependencies in the constructor orhandle method.
Annotations
You can add annotations to tools to provide additional information about the tool’s behavior to the AI client.Conditional registration
ImplementshouldRegister to conditionally register the tool at runtime.
false hides the tool from the AI client.
Responses
A tool must return aLaravel\Mcp\Response instance.
Text response
Text response
Error response
Error response
Image and audio responses
Image and audio responses
Multi-content response
Multi-content response
Structured response
Structured response
Returns structured data that’s easy for AI clients to parse.
Streaming response
Streaming response
Sends progress updates in real time during long-running operations.
Prompts
A prompt is a reusable prompt template. You can use them to provide standardized boilerplate queries that AI clients use to interact with language models.Creating a prompt
$prompts property.
Prompt arguments
Use thearguments method to define the prompt’s parameters.
Validation
Prompt arguments are automatically validated based on their definitions, but you can also apply more complex validation rules. Laravel MCP integrates seamlessly with Laravel’s validation features. You can validate arguments inside a prompt’shandle method.
Dependency injection
Prompts are resolved through Laravel’s service container, so you can type-hint dependencies in the constructor orhandle method.
handle, and the service container will resolve and inject dependencies automatically.
Conditional registration
ImplementshouldRegister to conditionally register a prompt at runtime.
false hides the prompt from the AI client and prevents it from being called.
Prompt responses
A prompt’shandle method can return user messages and assistant messages. Use asAssistant() to mark a message as coming from the assistant.
Resources
Resources are data or information that AI clients can load as context. You can provide documents, configuration information, dynamic data, and any other information that improves the quality of the AI’s response.Creating a resource
$resources property.
URIs and MIME types
By default, the URI is auto-generated from the class name (for example,weather://resources/weather-guidelines). You can customize this with the Uri and MimeType attributes.
Resource templates
To define a dynamic resource with URI variables, implement theHasUriTemplate interface.
get.
Resource requests
Unlike tools and prompts, resources cannot define an input schema or arguments. However, you can still access request information via the request object insidehandle.
Resource dependency injection
Resources are resolved through Laravel’s service container, so you can type-hint dependencies in the constructor orhandle method.
handle, and the service container will resolve and inject dependencies automatically.
Resource annotations
You can attach audience, priority, and last-modified annotations to a resource.Conditional resource registration
ImplementshouldRegister to conditionally register a resource at runtime.
false hides the resource from the AI client and prevents access.
Resource responses
A resource must return aLaravel\Mcp\Response instance.
For text content, use the text method.
Resource link response
Use theresourceLink method to return a resource link. Unlike an embedded resource, it returns a URI pointer that the AI client will fetch independently.
Blob response
To return binary content, use theblob method. Set the MIME type using the #[MimeType] attribute on the resource.
Error response
Use theerror method to indicate an error.
Apps
Laravel MCP supports MCP Apps, an extension of the Model Context Protocol that lets tools render interactive HTML applications inside a sandboxed iframe in supported hosts. This enables dashboards, forms, visualizations, and other rich experiences that go beyond plain text responses. An MCP app is composed of two pieces working together.- App resource — returns the self-contained HTML for the application.
- Tool — linked to the app resource with the
#[RendersApp]attribute. When the tool is invoked, the host fetches and renders the linked resource.
Creating an app resource
You can create an app resource with themake:mcp-app-resource Artisan command.
app/Mcp/Resources and a Blade view in resources/views/mcp. The view name is inferred from the class name. For example, WeatherDashboardApp maps to mcp.weather-dashboard-app.
AppResource extends the base Resource class and automatically applies the ui:// URI scheme and text/html;profile=mcp-app MIME type required by the MCP Apps specification. Like other resources, you must register it in the server’s $resources array.
The generated Blade view uses the <x-mcp::app> component. This component renders a complete HTML document that includes the bundled client-side MCP SDK.
createMcpApp function is provided by the bundled SDK. It handles the iframe’s connection to the server, applies the host theme, and exposes helpers such as callServerTool, sendMessage, and openLink along with event callbacks. See the MCP Apps specification for the full client-side API.
Rendering an app from a tool
To display an app resource, link it from a tool with the#[RendersApp] attribute. When the tool is invoked, Laravel MCP includes the resource’s URI in the tool metadata so the host can render the app inside a sandboxed iframe.
When an
AppResource is registered, Laravel MCP automatically advertises the io.modelcontextprotocol/ui capability. No additional server configuration is required.App tool visibility
Each#[RendersApp] tool can restrict its callers via the visibility argument. This is useful for hiding private app-only tools the UI calls to load or update data from the model.
Visibility enum has two cases, Model and App, and defaults to both. Use [Visibility::App] for backend actions that only the UI calls directly, and [Visibility::Model] to make the tool unavailable to the UI.
App configuration
The#[AppMeta] attribute on an app resource configures the iframe’s Content Security Policy, browser permissions, and any library scripts to include in the view’s <head>.
Library enum ships preconfigured CDN scripts for common frontend libraries such as Library::Tailwind and Library::Alpine, and the CDN origins are automatically merged into the CSP. The Permission enum covers browser permissions such as Camera, Microphone, Geolocation, and ClipboardWrite.
Developing apps with Boost
Laravel MCP includes a dedicated Boost skill reference for building MCP Apps. When Laravel Boost is installed, AI coding agents can invoke themcp-development skill and automatically generate an app resource, its Blade view, and the linked tool.
For the complete protocol reference (including client-side API and schema details), see the official MCP Apps documentation.
Metadata
You can attach the MCP spec’s_meta field to tool, resource, and prompt responses.
Response::make.
$meta property.
Icons
MCP clients can display icons for the server and its primitives. Use theIcon attribute to declare icons on the server, tools, resources, and prompts.
Icon attribute is repeatable, so you can declare multiple icons to provide different sizes or light/dark theme variants.
Alternatively, you can override the icons method to define icons programmatically. This is useful when icons depend on runtime conditions.
icons method are combined automatically. Icon paths are resolved as follows.
- Paths with a URI scheme such as
https:ordata:are used as-is. - Relative paths are resolved to URLs using Laravel’s
assethelper.
Authentication
Web servers can be authenticated with Laravel’s standard middleware.Sanctum
Token authentication using Laravel Sanctum. The MCP client sends anAuthorization: Bearer <token> header.
OAuth 2.1
OAuth authentication using Laravel Passport. This suits situations that call for stronger security.Authorization
You can retrieve the authenticated user via$request->user() and perform authorization checks inside tools and resources.
MCP client
Laravel MCP doesn’t just help you build servers — it also provides a client for connecting to other MCP servers. With the client, you can discover and invoke tools exposed by external MCP servers. This is especially useful when providing external MCP server capabilities to your AI agents.Connecting to a server
UseClient::web to connect to an HTTP-accessible MCP server, passing the server URL.
Client::local, passing the command and its arguments.
connect, connected, ping, and disconnect methods.
withTimeout to customize the request timeout.
Named clients
Instead of constructing a client each time, you can register a reusable named client. This is typically done in a service provider’sboot method using the Mcp facade.
Client authentication
To connect to a web MCP server protected by a bearer token, use thewithToken method. You can pass a token string or a closure that resolves lazily.
withOAuth method.
If the MCP server supports dynamic client registration, you can omit
clientId and clientSecret. The client will register itself automatically.routes/ai.php with oAuthRoutesFor. The closure receives the client name and a TokenSet after the authorization code has been exchanged for an access token.
mcp.oauth.{client}.connect) that redirects the user to the authorization server, and the callback route (mcp.oauth.{client}.callback) that exchanges the authorization code and calls your handler. Both use the web middleware group by default (which you can override via the middleware argument).
To begin the authorization flow, redirect the user to the connect route.
Tools
Thetools method fetches tools exposed by the MCP server. It returns a collection keyed by name.
limit argument to cap the number of results.
callTool and pass the tool name and an argument array. The returned ToolResult instance carries the response.
Prompts
Theprompts method fetches prompts exposed by the MCP server. It returns a collection keyed by name.
limit to cap the number of results.
getPrompt, passing the prompt name and an argument array. The returned PromptResult instance carries the generated messages.
Resources
Theresources method fetches resources exposed by the MCP server. It returns a collection keyed by URI.
limit to cap the number of results.
readResource and pass the resource URI. The returned ResourceReadResult instance carries the resource content.
Testing
MCP Inspector
Use the interactive MCP Inspector debugging tool to verify your MCP server.Unit tests
You can write unit tests against tools, resources, and prompts.actingAs to run as an authenticated user.
assertHasErrors / assertHasNoErrors to check for errors.
assertSentNotification and assertNotificationCount to verify streaming response notifications.
dd or dump to debug the response contents.