# Restify for Laravel for Rust

JSON:API endpoints and MCP tools from your [Laravel for Rust](https://laravel.com/rust) models.

Describe a model's API once, in a repository. Restify serves it to people over HTTP and to AI agents over the Model Context Protocol, with the same authorization and validation for both.

> Version 0.2, on `laravel-rust` 0.2.

## Example

```rust
use laravel::model::model;
use laravel::prelude::*;
use laravel::time::Timestamp;
use laravel::validation::Sometimes;
use restify::{Action, Field, McpTools, Repository, Restify, Schema};

#[model]
pub struct Post {
    pub id: i64,
    pub title: String,
    pub body: Option<String>,
    #[default]
    pub votes: i64,
    pub created_at: Timestamp,
    pub updated_at: Timestamp,
}

/// What a client sends to create a post. Only these fields can be written.
#[laravel::input(fills = Post)]
#[validate(anyone)]
#[derive(Schema)]
pub struct NewPost {
    /// The post's headline.
    #[validate(required, max_length(255))]
    pub title: String,
    /// The post's text.
    pub body: Option<String>,
}

/// What a client sends to change a post. A field left out keeps its value.
#[laravel::input(fills = Post)]
#[validate(anyone)]
#[derive(Schema)]
pub struct EditPost {
    /// The post's headline.
    #[validate(required, max_length(255))]
    pub title: Sometimes<String>,
    /// The post's text. Send `null` to clear it.
    pub body: Sometimes<Option<String>>,
}

pub struct PostRepository;

impl Repository for PostRepository {
    type Model = Post;
    type Store = NewPost;
    type Update = EditPost;

    /// AI agents may list posts and read one, through the MCP server.
    const MCP: Option<McpTools> = Some(McpTools::INDEX.show(true));

    /// What a post shows, and the columns clients may search, filter and sort by.
    fn fields() -> Vec<Field<Post>> {
        vec![
            Field::new(Post::title).searchable().sortable(),
            Field::new(Post::body).searchable().hide_from_index(),
            Field::new(Post::votes).matchable().sortable(),
            Field::new(Post::created_at),
        ]
    }

    /// Anyone may read posts. Nobody may write them.
    async fn authorize(action: Action, _request: &Request, _post: Option<&Post>) -> laravel::Result<()> {
        match action {
            Action::AllowRestify | Action::Index | Action::Show => Ok(()),
            _ => Err(laravel::abort(403)),
        }
    }
}

pub fn configure() -> laravel::foundation::ApplicationBuilder {
    Application::configure().with_providers(|providers| {
        providers.add(Restify::new().repository::<PostRepository>());
    })
}
```

That application serves:

| Request | Does |
| --- | --- |
| `GET /api/restify/posts` | Lists a page: `?search=`, `?votes=10`, `?-votes=10`, `?sort=-votes`, `?page=`, `?perPage=` |
| `GET /api/restify/posts/{id}` | Shows one |
| `POST /api/restify/posts` | Creates one |
| `PUT` or `POST /api/restify/posts/{id}` | Changes one, checking every field's rules |
| `PATCH /api/restify/posts/{id}` | Changes the fields it sends |
| `DELETE /api/restify/posts/{id}` | Deletes one |
| `POST /mcp/restify` | An MCP server where AI agents list posts and read one, as `const MCP` allows |

## What you get

- **One definition, two audiences.** The MCP tools run the same code as the endpoints.
- **Closed by default.** A repository refuses every request until its `authorize` method allows it. A response shows only the fields the repository lists, clients can only write the fields of the input structs, and only search, filter and sort by the fields marked for it.
- **Typed.** A field is one of the model's typed columns, so a wrong column is a compile error and a wrong value is a `422`.
- **Made for AI agents.** `#[derive(Schema)]` turns an input struct, its doc comments and its validation rules into the schema an agent reads. Errors come back as results an agent can correct.
- **The same JSON as Restify for PHP**: `data` with `id`, `type` and `attributes`, and `meta` and `links` for a page.
- **Pay only for what you use.** The MCP server and the OpenAPI document are Cargo features, `mcp` and `openapi`, both on by default. Turn them off and their code and dependencies leave your binary. See [Choosing features](installation.md#choosing-features).

## Documentation

The guides ship inside the crate, in [`docs/`](documentation.md), so they match the installed version:
[installation](installation.md), [repositories](repositories.md), [fields](fields.md), [filtering](filtering.md),
[relationships](relationships.md), [writing](writing.md), [files and images](files.md),
[actions and getters](actions.md), [authorization](authorization.md), [Sanctum tokens](sanctum.md),
[authentication](authentication.md), [soft deletes](soft-deletes.md), [bulk operations](bulk.md),
[MCP](mcp.md), [OpenAPI](openapi.md), [reference](reference.md), [testing](testing.md),
[troubleshooting](troubleshooting.md) and [migrating from Restify for PHP](migrating-from-php.md).

For AI coding agents, run `laravel restify:install` in an application once `Restify::new()` is registered in its providers: the command comes with the provider. It writes two skills to `.agents/skills`, and the same files to `.claude/skills` for Claude Code's slash commands: one that tells an agent how to work with Restify and where the guides are, and one that moves a Restify for PHP application to Rust. To start a migration from the PHP application, `php artisan restify:rust-install` from the Composer package `binarcode/laravel-restify-rust-migrate` writes that second skill there.
