This page adds the crate to a Laravel for Rust application and connects an AI assistant to its MCP route.
Requirements
- Laravel for Rust 0.2 with a database (PostgreSQL, MySQL or SQLite).
- Session sign-in for your users: the approval page uses it.
APP_URLset to your public HTTPS address. Outside thelocalandtestingenvironments the crate refuses to start without HTTPS, because every URL it advertises is built fromAPP_URL.
Install
[dependencies]
laravel-rust-mcp-oauth = "0.1"
The crate enables the PostgreSQL driver by default, because Laravel for Rust 0.2 doesn't compile its database layer without one (laravel/rust-framework#419). With MySQL or SQLite, pick your driver instead:
[dependencies]
laravel-rust-mcp-oauth = { version = "0.1", default-features = false, features = ["mysql"] } # or "sqlite"
Register the service provider with your user provider, the same one your session guard uses:
use laravel_rust_mcp_oauth::McpOAuthServiceProvider;
Application::configure()
.with_providers(|providers| {
providers.add(McpOAuthServiceProvider::<model::Users<User>>::new());
})
.with_auth(|auth, _| {
auth.session::<model::Users<User>>();
})
Create the tables:
laravel migrate
The provider adds the discovery documents, the OAuth endpoints under /oauth, the approval page, the mcp-oauth:prune and mcp-oauth:clients commands and a daily prune.
Protect the MCP route
Put McpAuthenticate on any route handler or on a route serving rmcp. The middleware is designed for Laravel's first-party MCP server, which is unreleased, and compatibility is still unverified. The following example shows the intended usage once the server ships.
use laravel_rust_mcp_oauth::McpAuthenticate;
servers
.web("/mcp", app_server)
.middleware(McpAuthenticate::<User>::new().allow_guest_methods(["server/discover"]));
With your own handler:
route.middleware(McpAuthenticate::<User>::new()).group(|route| {
route.any("/mcp", mcp_endpoint);
});
A request without a token now gets this answer, which is how assistants find your sign-in:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://app.example/.well-known/oauth-protected-resource/mcp", scope="mcp offline_access"
Cache-Control: no-store
The challenge offers the first configured scope (mcp by default) and offline_access. If you set require_scopes on a route, the challenge offers those scopes and offline_access.
Connect an assistant
Add https://app.example/mcp as a custom connector or remote MCP server in your assistant. It opens your approval page in the browser. Log in if you aren't already, click Connect, and the assistant is connected.
To check from the command line, use the MCP Inspector:
npx @modelcontextprotocol/inspectorNext
- Protecting routes: scopes, guest methods and using the grant in tools.
- The approval page: your own design, names and wording.
- Configuration: every setting.