Architecture

Custom MCP Tools in Laravel: Add One from a Module

By Lara Dashboard 1 view
Custom MCP Tools in Laravel: Add One from a Module
Custom MCP tools in Laravel let an AI client such as Claude call your own code, not just the generic tools a CMS ships with. In Lara Dashboard you add one from a module: write a tool class, register it with one filter hook, map its ability to a permission, and test it. No core files change, and the tool disappears when the module is disabled.
Disclosure: Lara Dashboard is our open-source, MIT-licensed Laravel CMS and admin, so this is our own guide. Every class, hook and file name below comes from the develop branch on GitHub as of November 2026 (v1.5.0, Laravel 13, PHP 8.3+, laravel/mcp pinned to ^0.6.0). We use a made-up Bookings module as the example. Swap in your own domain.

Quick answer: four steps and a test

Here is the whole job in one list. The rest of the post walks through each step.
  1. Pick a permission (booking.view) and an MCP ability (mcp:bookings.read).
  2. Write a tool class that extends Laravel\Mcp\Server\Tool and checks that ability first.
  3. In your module service provider, add the class to McpFilterHook::TOOLS and the ability to McpFilterHook::ABILITY_PERMISSION_MAP.
  4. Issue a fresh agent token, so the new ability is on it.
  5. Write a Pest test that calls the tool through LaraDashboardServer.
If you are new to the idea of an MCP server on a CMS, start with our plain-English guide to MCP servers for a CMS. If you have not built a module yet, read build a Laravel module step by step first. This post assumes you have one.

Why a custom tool beats asking the agent to improvise

Lara Dashboard ships 25 core MCP tools. You can see them in app/Services/Mcp/McpRegistryService.php: posts, terms, media, email templates, the daily briefing, logs, site health and module management. Those cover content work. They know nothing about your bookings table, your invoices or your client's stock levels.
Without a tool for that data, an agent can only guess or ask you to paste rows into the chat. A custom tool fixes that. It gives the agent one narrow, named action, with a schema, validation and a permission check that you control. The agent cannot do more than the tool allows.
That narrowness is the point. A tool called list-bookings that returns ten rows is much safer than handing an agent database access and hoping it writes the right query.

Step 1: name the permission and the ability

Every Lara Dashboard MCP call is checked twice. First, the token must carry an ability string, such as mcp:posts.read. Second, the user behind the token must hold the matching Spatie permission, such as post.view. You can see both checks in the InteractsWithMcpAuthorization trait in app/Mcp/Tools/Concerns.
So decide both names up front. For a read-only bookings tool we use booking.view as the permission and mcp:bookings.read as the ability. Follow the core pattern: mcp:, then the area, then .read or .write.
If the permission does not exist yet, add it from your module with the permission groups filter, the same way the module scaffold does:
Hook::addFilter(PermissionFilterHook::PERMISSION_GROUPS, function (array $groups): array { $groups[] = ['group_name' => 'booking', 'permissions' => ['booking.view', 'booking.create']]; return $groups; });
The permission then shows up on the role screen, and you grant it to the roles that should use the tool. Our guide to role-based access with Spatie covers how to split those roles.

Step 2: write the tool class inside your module

Module classes live under modules/Bookings/app/ with the Modules\Bookings namespace. Put the tool at modules/Bookings/app/Mcp/Tools/ListBookingsTool.php. A trimmed version looks like this:
#[Name('list-bookings')]
#[Description('List upcoming bookings with an optional status filter. Returns at most 50 rows.')]
#[McpToolMeta(ability: 'mcp:bookings.read', permission: 'booking.view', group: 'Bookings')]
class ListBookingsTool extends Tool { use InteractsWithMcpAuthorization; ... }
The Name and Description attributes come from laravel/mcp. The description is what the agent reads when it decides whether to call your tool, so write it like a short instruction, not marketing copy. McpToolMeta is Lara Dashboard's own attribute in app/Mcp/Attributes. It tells the registry which ability, permission and group to show for the tool.
The handle() method does the work. Copy the shape of a core tool such as ListTermsTool:
if ($response = $this->authorizeMcpAbility('mcp:bookings.read', 'booking.view')) { return $response; }
$validated = $request->validate(['status' => ['nullable', 'in:pending,confirmed,cancelled'], 'per_page' => ['nullable', 'integer', 'min:1', 'max:50']]);
return Response::json(['data' => $bookings->map(fn ($b) => ['id' => $b->id, 'starts_at' => $b->starts_at, 'status' => $b->status])->all()]);
Then describe the inputs in schema(), so the client knows what it can send:
return ['status' => $schema->string()->description('Optional status filter.')->enum(['pending', 'confirmed', 'cancelled']), 'per_page' => $schema->integer()->default(10)];
Three habits from the core tools are worth keeping. Call the authorization check on the first line. Validate every input, even ones the schema already limits. And return only the fields the agent needs, not whole models with every column and relation.

Step 3: register the tool and its ability with two filters

McpRegistryService::toolClasses() passes its list of core tool classes through the McpFilterHook::TOOLS filter. McpTokenService::resolveAbilitiesForUser() passes its ability-to-permission map through McpFilterHook::ABILITY_PERMISSION_MAP. Your module hooks into both from its service provider's boot() method:
Hook::addFilter(McpFilterHook::TOOLS, function (array $tools): array { $tools[] = ListBookingsTool::class; return $tools; });
Hook::addFilter(McpFilterHook::ABILITY_PERMISSION_MAP, function (array $map): array { $map['mcp:bookings.read'] = 'booking.view'; return $map; });
The first filter puts the tool on the server. The second means that any user with booking.view gets mcp:bookings.read on their next token. Forget the second filter and the tool is listed but every call fails with a missing-ability error.
Because both filters live in your module, they only run while the module is enabled. Disable the module and the tool is gone from the server and from Settings → MCP. We explain the hook system itself in Laravel hooks and filters in Lara Dashboard.

Step 4: issue a fresh token, because abilities are set at creation

This is the step people miss. McpTokenService::issueToken() works out the user's abilities when the token is created and stores them on the Sanctum token. A token you made last week does not have mcp:bookings.read, even if the user has the permission today.
Agent tokens. Create a new one under Settings → MCP → Agent tokens, update your client config, and revoke the old one.
OAuth connections. Claude.ai and other OAuth clients are handled for you. According to docs/mcp.md, every refresh recalculates abilities from the user's current permissions, and access tokens last 60 minutes. Our Claude MCP OAuth guide walks through that connection.
To confirm, ask the agent to call list-mcp-tools. It returns only the tools the current token and user can actually use, so your new tool should appear there with its group and ability.

Step 5: test the tool like the core does

The core has feature tests for its MCP tools in tests/Feature/Mcp. Copy that pattern for your module. Create a user, give them the permission, issue a token, then call the tool through the server class:
LaraDashboardServer::actingAs($user, 'sanctum')->tool(ListBookingsTool::class, ['status' => 'confirmed'])->assertOk()->assertSee($booking->id);
Write at least three tests. One where the call works. One where the user lacks booking.view, which should return an error. And one for bad input, such as an unknown status. tests/Feature/Mcp/McpRegistryTest.php also shows how to test the registration itself, using static methods so the filter can be removed again with Hook::removeFilter() after each test.
Run these in CI with the rest of your suite. Our post on staging, tests and CI for Laravel CMS work covers the setup.

Gotchas we found reading the code

The attribute does not enforce anything. McpToolMeta is read by the registry for display and by list-mcp-tools for filtering. The actual block happens only when your handle() calls authorizeMcpAbility(). Skip that call and any token can run the tool.
A typo fails silently. The registry drops any class that does not exist or does not extend Tool. If your tool never shows up, check the namespace and the module's autoload before anything else.
Clients may still list tools a user cannot call. The core tools do not hide themselves per user. A call without the right ability returns an error message instead. Current laravel/mcp docs describe a shouldRegister() method for hiding tools, but check that it exists in the version your install uses before you rely on it.
The package is moving fast. Lara Dashboard pins laravel/mcp to ^0.6.0. As of November 2026, Packagist lists v1.3.0 as the latest release. The official Laravel MCP docs follow the latest version, which documents features such as read-only and destructive annotations. Build against the version in your composer.lock, and use the core tools as your reference.
Keep tool names unique. Core names are short, such as list-posts. Avoid them, and consider a module prefix if your tool name is generic.

When not to write a custom MCP tool

A custom tool is the right fit when an agent needs repeated, structured access to your own data on a live site. It is not always the right fit.
  • If you want help writing code in your editor, a coding-agent tool such as Laravel Boost is the better fit, not a tool on your production site.
  • If a core tool already does the job, such as create-post or list-media, use it instead of wrapping it.
  • If another app, not an AI agent, needs the data, build a normal Sanctum API endpoint.
  • If the action is destructive or hard to undo, such as refunds or bulk deletes, keep a human in the admin screen.
  • If you cannot name one permission that should guard the tool, the tool is probably too broad. Split it.
  • If the data is sensitive, such as personal or payment details, return the minimum fields or do not expose it at all.
We compare these options in more depth in Laravel MCP vs Boost vs a CMS MCP server.
There is also a cost. Every tool you add is code you maintain, a schema the agent must read, and one more thing to review when laravel/mcp changes. Five sharp tools are better than thirty vague ones.

Self-check before you ship the tool

  1. The tool has one clear job, and its description says when to use it.
  2. handle() calls authorizeMcpAbility() on its first line.
  3. Every input is validated, and list results have a maximum page size.
  4. The response returns only the fields the agent needs.
  5. The ability is mapped to a permission with ABILITY_PERMISSION_MAP.
  6. Both filters live in the module service provider, not in core files.
  7. The tool name does not clash with a core tool.
  8. You issued a fresh agent token and saw the tool in list-mcp-tools.
  9. Tests cover success, missing permission and bad input.
  10. Write tools are idempotent where possible, or create drafts a human approves.

Verdict

Adding custom MCP tools in Laravel is a small job when the platform gives you the hooks. In Lara Dashboard it is one class, two filters, a fresh token and a test, all inside a module you can switch off. The double check on token ability and user permission means your agent never gets more access than the person who connected it.
The work that matters is the design: narrow tools, tight validation and small responses. If that is how you want AI agents to touch your client sites, try Lara Dashboard, scaffold a module, and give your agent its first tool.

FAQ

What is a custom MCP tool in Laravel?

It is a PHP class that an MCP client, such as Claude, can call by name with JSON input. In Lara Dashboard it extends the Tool class from the laravel/mcp package and is registered with the McpFilterHook::TOOLS filter.

Do I need to edit core files to add an MCP tool?

No. You register the tool and its ability from your module's service provider using two filter hooks. Core files stay untouched, so upgrades stay simple.

Why does my new tool return a missing-ability error?

Either the ability is not mapped with ABILITY_PERMISSION_MAP, the user lacks the permission, or the agent token was created before the mapping existed. Issue a fresh agent token after adding the mapping.

Can a custom tool write data, not just read it?

Yes. Use a separate write ability and permission, validate every field, and prefer actions that are easy to review or undo, such as creating drafts. Keep destructive actions in the admin.

How do I test an MCP tool?

Use the server's test helper: call LaraDashboardServer::actingAs($user, 'sanctum')->tool(YourTool::class, [...]) in a Pest feature test, then assert on the response. The core tests in tests/Feature/Mcp show the full setup.

Will my tool show up when the module is disabled?

No. The filters are registered when the module's service provider boots, and disabled modules do not boot, so the tool is removed from the server.
Laravel CMS Open source Module Architecture laravel access control

Try Lara Dashboard for Free

Explore every feature live — no sign-up required.

Launch Live Demo