sabira-khan / eloquent-mcp
Expose chosen Eloquent models to Claude Code and other MCP clients as a safe, read-only MCP server.
Requires
- php: ^8.2
- illuminate/database: ^12.41.1|^13.0
- illuminate/events: ^12.41.1|^13.0
- illuminate/log: ^12.41.1|^13.0
- illuminate/support: ^12.41.1|^13.0
- laravel/mcp: ^1.2
Requires (Dev)
- laravel/pint: ^1.30
- orchestra/testbench: ^10.0|^11.0
- phpunit/phpunit: ^11.5|^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Ask Claude about your app's data through your own Eloquent models, scopes and casts. Read-only, deny by default, and safe enough to point at a read replica.
Eloquent MCP turns selected models into a Model Context Protocol server built on the official laravel/mcp package. Agents can then answer questions like "how many orders were refunded this week?" or "show me this customer's last five orders" against your real database, without raw SQL and without seeing data you have not exposed.
composer require sabira-khan/eloquent-mcp php artisan eloquent-mcp:install claude mcp add eloquent -- php artisan mcp:start eloquent
How it compares
Laravel already has good MCP servers for coding agents. Eloquent MCP is for a different job: answering questions about your data with the same rules your app uses.
| Eloquent MCP | Laravel Boost | laravel-agent-mcp | |
|---|---|---|---|
| Built for | Questions about your data ("revenue by status this month") | Coding agents writing code in your app | Coding agents reading schema, logs, queue, cache and routes |
| How data is read | Through the Eloquent models and columns you list | Read-only SQL on any table | SELECT-only SQL with redaction |
$hidden, casts, soft deletes and global scopes |
Applied on every read | Not applied (raw SQL) | Not applied (raw SQL) |
| Columns visible by default | Only the primary key | All | All, minus redacted ones |
| Writes stopped by | No write path, plus a read-only transaction | A read-only transaction | A read-only connection and a SELECT-only validator |
Use Boost or laravel-agent-mcp to help an agent write code. Use Eloquent MCP when the question is about your data.
Why
Giving an agent database credentials means it can read every table and run any query. Eloquent MCP goes through your models instead:
| Safeguard | How it works |
|---|---|
| Opt in per model | Only models listed in the config are visible |
| Deny columns by default | A model without a columns list exposes only its primary key |
| Hidden stays hidden | $hidden attributes are never returned, filtered on or aggregated, even if you list them |
| Encrypted stays encrypted | Columns with an encrypted or AsEncrypted* cast are left out unless you list them |
| Scopes still apply | Queries start from newQuery(), so soft deletes and global scopes such as tenancy keep working |
| Your own definitions | Local scopes you list, such as paid or active, are available to the agent, so it uses your business rules instead of reinventing them |
| No raw SQL | Every query is built with the query builder, there is no write path, and all tools carry the MCP readOnlyHint |
| Database-enforced reads | Reads run in a transaction that is always rolled back, marked read-only on MySQL, MariaDB and PostgreSQL, with query_only on SQLite |
| Checked arguments | Arguments are validated against each tool's schema, and wrong types get a clear error |
| Bounded cost | Rows are capped, long strings are truncated, and max_execution_ms stops slow queries |
| One connection | Relations must read from the configured connection, so a replica or SELECT-only user cannot be bypassed |
| Exact decimals | Decimal aggregates come back as strings at the column's precision, never floats |
| Audit trail | Every call dispatches a ToolCalled event and can be logged to a channel |
Installation
composer require sabira-khan/eloquent-mcp php artisan eloquent-mcp:install
The install command finds the models in app/Models and writes config/eloquent-mcp.php with, for each model:
- its columns, leaving out
$hiddenattributes, encrypted casts, columns outside$visible, and names that look sensitive, such aspassword,token,secretorapi_key; - its relations to the other models it found;
- its local scopes.
It prints a table of what it exposed and what it left out. Review the file before you connect a client: it is a starting point, not a decision. Use --path and --namespace for models elsewhere, and --force to overwrite an existing config.
To write the config by hand instead, publish the empty one with php artisan vendor:publish --tag=eloquent-mcp-config.
Requires PHP 8.2+ and Laravel 12 or 13.
Configuration
List the models to expose in config/eloquent-mcp.php:
'models' => [ App\Models\Customer::class => [ 'description' => 'People who have placed at least one order.', 'columns' => ['id', 'name', 'country', 'created_at'], 'relations' => ['orders'], ], App\Models\Order::class => [ 'description' => 'One row per checkout.', 'columns' => ['id', 'customer_id', 'status', 'total', 'currency', 'created_at'], 'relations' => ['customer', 'items'], 'scopes' => [ 'paid' => 'Paid orders that were not refunded.', 'placedBetween', ], ], App\Models\OrderItem::class => [ 'columns' => ['id', 'order_id', 'sku', 'quantity', 'price'], ], ],
| Option | Default | Purpose |
|---|---|---|
handle |
eloquent |
Name used with mcp:start |
enabled |
true |
Register the local server |
columns_default |
deny |
deny exposes only the primary key of a model without a columns list. allow exposes every column that is not hidden or encrypted |
connection |
model default | Database connection to read from, e.g. a read replica. Relations must use the same connection |
max_rows |
50 |
Maximum rows or groups per request |
max_string_length |
1000 |
Longer strings are truncated |
max_execution_ms |
none | Time limit per query in milliseconds. MySQL uses max_execution_time, MariaDB max_statement_time, PostgreSQL SET LOCAL statement_timeout. SQLite has no limit |
log_channel |
none | Log channel that gets one line per tool call |
Per model, columns lists the readable columns, relations lists relations the agent may load, scopes lists query scopes the agent may use, description tells the agent what the model means, and as renames the model.
Scopes
Your query scopes already say what "paid", "active" or "overdue" means in your app. List them and the agent uses your definition instead of guessing one with where filters:
class Order extends Model { public function scopePaid(Builder $query): void { $query->where('status', 'paid')->whereNull('refunded_at'); } #[Scope] protected function placedBetween(Builder $query, string $from, string $to): void { $query->whereBetween('created_at', [$from, $to]); } }
{
"model": "Order",
"function": "sum",
"column": "total",
"scopes": [
{ "name": "paid" },
{ "name": "placedBetween", "arguments": ["2026-11-01", "2026-11-30"] }
]
}
Both scopeName() methods and the #[Scope] attribute work. Only the scopes you list are available, and describe-model shows each one with its arguments and your description. Arguments must be strings, numbers or booleans, and the agent gets a clear error when the count or type is wrong. Scopes run your code, so list only scopes that read.
Point connection at a database user that only has SELECT rights. See Limits.
Audit trail
Every tool call dispatches a SabiraKhan\EloquentMcp\Events\ToolCalled event with the tool name, model, arguments, number of rows or groups returned, duration in milliseconds and whether the call errored. Row data is never included, but arguments can contain filter values such as an email address. Listen for the event to store calls wherever you keep audit records, or set log_channel to write one log line per call:
'log_channel' => env('ELOQUENT_MCP_LOG_CHANNEL', 'daily'),
Limits
Be clear about what this package does not do:
- No user identity or policy checks. The server runs over stdio, so it does not know which person is asking. Laravel policies and gates are not applied, and anyone who can start the server can read everything you expose. Only expose data that every user of the MCP client may see.
- Read-only needs a
SELECT-only database user. The read-only transaction makes the database reject writes, but for a hard guarantee pointconnectionat a database user that only hasSELECTrights. - No row-level rules beyond scopes. Global scopes apply, but there is no per-row authorization.
- Arguments in the audit trail can contain personal data, such as an email address used as a filter.
- SQLite has no query time limit.
- Data is sent to the model provider. Anything a tool returns goes to the AI client and its provider.
Connecting a client
The server runs over stdio through Artisan:
php artisan mcp:start eloquent
Claude Code:
claude mcp add eloquent -- php artisan mcp:start eloquent
Any MCP client that supports stdio servers (Cursor, VS Code, Windsurf) can use the same command. To try it in the browser:
php artisan mcp:inspector eloquent
Tools
| Tool | What it does |
|---|---|
list-models |
Exposed models with their tables, readable columns and relations |
describe-model |
Column types, nullability, casts, relations and scopes of one model |
query-model |
Rows with where filters, scopes, filters on related rows, relation counts, sorting, a limit and optional relations |
aggregate-model |
count, sum, avg, min or max with filters, scopes and filters on related rows, optionally grouped by a column or by day, week, month or year |
Filters use =, !=, >, >=, <, <=, like, in, not_in, is_null and not_null:
{
"model": "Order",
"where": [
{ "column": "status", "operator": "in", "values": ["paid", "shipped"] },
{ "column": "created_at", "operator": ">=", "value": "2026-11-01" }
],
"order_by": "total",
"direction": "desc",
"limit": 10,
"with": ["customer"]
}
Filtering and counting by relation
has keeps rows by their related rows, like whereHas. "Customers with a paid order over 100":
{
"model": "Customer",
"has": [
{
"relation": "orders",
"where": [{ "column": "total", "operator": ">", "value": 100 }],
"scopes": [{ "name": "paid" }]
}
]
}
It means at least one by default. Set operator and count for other counts, such as { "relation": "orders", "operator": "<", "count": 1 } for customers with no orders. aggregate-model takes the same has, so "how many customers have never ordered" is one call.
with_count adds a count of related rows to each row, like withCount, and you can sort by it. "Top 5 customers by paid orders":
{
"model": "Customer",
"with_count": [{ "relation": "orders", "scopes": [{ "name": "paid" }] }],
"order_by": "orders_count",
"direction": "desc",
"limit": 5
}
Only relations listed in the model's relations option, pointing at another exposed model, can be used. Filters and scopes inside them are checked against the related model's own readable columns and exposed scopes, and its global scopes such as soft deletes still apply.
Grouping by period
To answer "revenue by month", group a date column by day, week, month or year:
{
"model": "Order",
"function": "sum",
"column": "total",
"group_by": "created_at",
"period": "month",
"scopes": [{ "name": "paid" }]
}
{ "groups": [{ "group": "2026-09", "value": "1250.500" }, { "group": "2026-10", "value": "1410.000" }] }
The most recent periods are returned, oldest first, up to max_rows, with has_more when there are older ones. Weeks are labelled with the Monday they start on. Periods are worked out in the database's time zone. This works on SQLite, MySQL, MariaDB and PostgreSQL.
Testing
composer install
composer test
composer lint
License
MIT