How to Use MCP Tools in Laravel AI Apps with LarAgent
We are in the era of Software 3.0, where your prompts are essentially the programs that control large language models (LLMs).
But here’s the developer pain point: as your AI agent grows smarter, it needs more tools—access to databases, external APIs, and memory servers. Managing all these external capabilities often means endless custom boilerplate code and complex maintenance.
The elegant solution for Laravel developers is the combination of the LarAgent package and the Model Context Protocol (MCP). Additionally, adding Laravel MCP package to this toolset gives you real superpowers 💪
This article is your step-by-step guide to ditching the boilerplate. We'll show you how to leverage MCP to dynamically inject external capabilities—tools and data—into your LarAgent, giving your agents superpowers without ever writing another manual tool implementation.
Extensibility Problem
If you're using LarAgent, you’re already building robust AI agents using familiar Laravel conventions, much like creating Eloquent models. You define your agent's behaviour and register tools, which are custom PHP methods that enable the agent to perform actions, such as calling an external API.
For simple needs, you can easily add tools right within your class using simple attributes. But what happens when you need to connect your agent to a dozen different microservices or external APIs? You suddenly find yourself writing and maintaining a significant amount of integration code just to keep everything organised.
This is where the Model Context Protocol (MCP) shines! MCP enables your agents to connect to external MCP servers and dynamically discover and use their offered tools and data sources, extending capabilities instantly and reducing your workload.
Initial Configuration: Mapping External Services
Before your agent can chat with an MCP server, you need to tell LarAgent where to find it. This is done in your application's configuration file, specifically under the mcp_servers key in config/laragent.php.
LarAgent supports two main ways to connect to an MCP server, called transport types:
- HTTP Transport: This is what you use for remote or cloud-based MCP servers accessible via standard HTTP endpoints. You simply provide the server’s base URL and any necessary API tokens.
- STDIO Transport: This transport is super handy for command-line MCP servers, letting you integrate local tools or even npm packages (like a memory server) right into your agent setup.
Here is an example registration of STDIO MCP server:
'mcp_servers' => [
'mcp_server_memory' => [
'type' => \Redberry\MCPClient\Enums\Transporters::STDIO,
'command' => [
'npx',
'-y',
'@modelcontextprotocol/server-memory',
],
'timeout' => 30,
'cwd' => base_path(),
],
],Integration step: Activating Dynamic Tools in Your Agent Class
Once configured globally, you need to link those MCP servers to a specific LarAgent class so it knows which external tools it can access.
You have two easy options for registration:
- Using a Property: The quickest way is defining the
$mcpServersproperty right in your Agent class, listing the names of the servers you configured
use LarAgent\Agent;
class MyAgent extends Agent
{
protected $mcpServers = [
'github',
'mcp_server_memory',
];
}- Using a Method: If you need more dynamic control—maybe only registering a server under certain conditions—you can implement the
registerMcpServers()method and return the list of server names:
use LarAgent\Agent;
class MyAgent extends Agent
{
public function registerMcpServers()
{
return [
'github',
'mcp_server_memory',
];
}
}This registration kicks off the dynamic magic. When your agent initializes, LarAgent connects to the specified MCP servers and goes through a process: Discovery, Registration, and Execution.
- Discovery: LarAgent fetches the schema (the parameters and descriptions) of all available tools from the external MCP server.
- Registration: These external tools are automatically registered with your agent, appearing alongside any custom tools you might have defined.
- Execution: When the LLM decides it needs a capability (like checking a specific GitHub issue), LarAgent handles the connection, invocation, and returns the result back to the LLM
Fine-Grained Control: Tools, Resources, and Selective Filtering
Unlocking dynamic tools is great, but granting an AI agent access to everything might be risky or inefficient. And LarAgent on a mission to make Laravel the most productive stack for AI Development.
This is why we are aiming to reliable and production-ready solutions, where filtering and resource management are crucial.
Controlling What You Fetch
By default, LarAgent only fetches tools (callable functions) from MCP servers, since they are intended to be used so. But what if you need data sources?
You can fetch specific types using suffixes when registering the server:
- Append :tools to explicitly request callable functions.
- Append :resources to request data sources
protected $mcpServers = [
'mcp_server_memory:tools',
'mcp_everything:resources',
];Filtering is your best friend for security and performance.
You can use only and except filters to precisely control which specific tools or resources are registered.
For instance, if you connect to a server that manages database entities, you absolutely must use the except filter to block risky operations like delete_entities to keep your data safe:
public function registerMcpServers()
{
return [
'mcp_server_memory:tools|except:delete_entities,delete_observations',
];
}We went even further and gave the property in the agent class that can be used to achieve more control and customisation over your MCP tools, including changing the descriptions or fetching resources manually. Check details in LarAgent documentation: Working with Resources & Manual tool access
Performance and Security
With great dynamic power comes great responsibility 😄 To ensure your MCP-enabled agents run smoothly in production, keep these best practices in mind:
- Mind the Server Count: The process of discovery, fetching, and registering tools is intensive. Using more than three MCP servers per Agent can dramatically decrease performance and response time. Be strategic and register only the servers strictly necessary for that agent's job
- Handle Resources Efficiently: Load resources in the
instructions()method only when context is truly needed. Remember that resource size impacts the total token usage for your LLM, so don't inject the entire internet 😆 - Secure Credentials: Use environment variables for all tokens and sensitive credentials
Conclusion
We started by acknowledging the shift to a world where our software needs to be dynamically programmable. We solved the complexity of scaling agent capabilities by leveraging the Model Context Protocol (MCP) within LarAgent.
By mastering configuration, strategic registration, and intelligent filtering using except to block dangerous operations, you can now build robust, secure, and highly extensible AI agents that dynamically find and use the tools they need. This capability frees you from writing custom boilerplate for every single integration, letting you focus on the actual intelligence of your application.
And Laravel MCP package gives you ability to scaffold your own MCP servers with ease.
Now that you have the framework to dynamically integrate unlimited external services, what massive, multi-tool agent will you build next in Laravel?
Go forth and build those powerful agents! The Laravel AI ecosystem is growing fast, and tools like LarAgent and Laravel MCP are putting you at the cutting edge of modern software development.