Overview
The Model Context Protocol (MCP) integration enables your Agents to dynamically discover and use tools and resources provided by external MCP servers. This powerful feature allows you to extend your agent’s capabilities without writing custom tool implementations.MCP support is provided through the
redberry/mcp-client-laravel
package,
which is included as a dependency.Configuration
Configure MCP servers in yourconfig/laragent.php
file under the mcp_servers
key. LarAgent supports two transport types: HTTP and STDIO.
HTTP Transport
Use HTTP transport for MCP servers accessible via HTTP endpoints:config/laragent.php
STDIO Transport
Use STDIO transport for command-line MCP servers:config/laragent.php
You can configure multiple MCP servers simultaneously by adding more entries
to the
mcp_servers
array.Registering MCP Servers in Agents
Once configured, register MCP servers in your agent class using either a property or a method.Using Property
The simplest approach is to define the$mcpServers
property:
Using Method
For more control and dynamic nature, implement theregisterMcpServers()
method:
Filtering Tools and Resources
By default, LarAgent fetches only tools from MCP servers. You can explicitly control what to fetch using filters.Fetch Specific Types
Use the:tools
or :resources
suffix to specify what to fetch:
Include/Exclude Specific Items
Useonly
and except
filters to control which tools or resources are registered:
When using filters, separate multiple items with commas and ensure there are
no spaces after the commas.
Working with Resources
Resources are data sources provided by MCP servers. While tools are automatically registered, resources need to be explicitly requested via"mcp_server_name:resources"
.
Alternatively, resources and tools can be accessed manually through the mcpClient
property.
Manual Resource Access
Access resources directly in your agent methods:Resource Response Structure
ThereadResource()
method returns an array with the following structure:
Array containing resource content objects.
Registering Resources as Tools
You can make resources available as tools by explicitly requesting them:Manual Tool Access
You can run MCP tools manually viamcpClient
property and callTool
method:
How MCP Tools Work
MCP tools are dynamically constructed based on metadata from the MCP server:1
Discovery
LarAgent connects to the configured MCP server and retrieves available tools and their schemas.
2
Registration
Tools are automatically registered with your agent, including their
parameters, descriptions, and required fields.
3
Execution
When the LLM decides to use an MCP tool, LarAgent handles the invocation and returns results to the conversation.
MCP tools integrate seamlessly with your agent’s existing tool system,
appearing alongside custom tools.
Initialization -> fetching -> registration of tools: The process is pretty
intensive, using more than 3 mcp servers per Agent can dramatically decrease
the performance / response time
Complete Example
Here’s a comprehensive example combining multiple MCP servers with filtering:Best Practices
Choose the right transport type
Choose the right transport type
- Use HTTP transport for cloud-based or remote MCP servers
- Use STDIO transport for local command-line tools and npm packages
- Consider timeout values based on expected response times
Filter tools strategically
Filter tools strategically
Use
except
to exclude dangerous or unnecessary operations (like delete
functions). Use only
when you need a specific subset of tools for focused
agents. Start with all tools and refine based on actual usage patternsSecure your credentials
Secure your credentials
Always use environment variables for API tokens and sensitive credentials;
Never hardcode tokens directly in configuration files; Review MCP server
documentation for required authentication
Handle resources efficiently
Handle resources efficiently
- Load resources in
instructions()
when context is needed - Cache resource data when appropriate to avoid repeated fetches
- Consider resource size impact on token usage
Troubleshooting
Ensure MCP server commands are available in your environment. For STDIO
transport with npm packages, verify Node.js and npm/npx are installed and
accessible.
- Verify the
base_url
is correct and accessible - Check authentication tokens are valid
- Ensure firewall rules allow outbound connections
- Confirm the command exists (
npx
,node
, etc.) - Verify the working directory (
cwd
) has proper permissions - Check the command array syntax is correct
- Verify the MCP server name matches your configuration exactly
-
Check filter syntax if using
only
orexcept
- Ensure the MCP server actually provides tools (not just resources)
- Review logs for connection or parsing errors
-
Increase the
timeout
value in your MCP server configuration - For STDIO servers, ensure the command starts quickly
- Consider network latency for HTTP servers