# How to embed the StackQL MCP server in a Rust application

> Embed the StackQL MCP server inside a Rust binary using the sidecar feature or include_bytes! vendoring, so a Rust app can run cloud queries and provisioning over SQL with no runtime dependency.

Source: https://stackql.io/ai/how-tos/embed-stackql-mcp-server-in-rust

StackQL's MCP server can run inside a Rust binary with no `npx`, no separate install, and no runtime dependency. The `stackql-mcp` crate spawns the signed StackQL binary over stdio behind an `rmcp` client, so your Rust app gets a governed, self-describing SQL interface to every cloud and SaaS provider in the StackQL registry. This is the pattern behind **auditron**, a terminal compliance copilot shipped as one binary.

## Steps

1. **Add the crate** to `Cargo.toml`:

```toml
stackql-mcp = "0.1"
```

2. **Build the server and list tools.** The builder defaults to `Mode::ReadOnly`:

```rust
use stackql_mcp::{Mode, StackqlMcp};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let server = StackqlMcp::builder()
        .mode(Mode::ReadOnly)
        .auth(serde_json::json!({"github": {"type": "null_auth"}}))
        .start()
        .await?;
    let tools = server.list_all_tools().await?;
    println!("{} tools available", tools.len());
    server.shutdown().await?;
    Ok(())
}
```

The builder also exposes `.binary()`, `.bundle_path()`, `.approot()`, and `.command()`. MSRV is Rust 1.88.

3. **Choose a mode.** `.mode()` defaults to `Mode::ReadOnly`. Set `Mode::Safe`, `Mode::DeleteSafe`, or `Mode::FullAccess` only when the app needs to provision. Escalation is explicit.

## Ship a single binary with include_bytes!

Two feature flags control how the binary is sourced:

- `sidecar` (default) - download, verify, and cache the signed binary on first run.
- `vendored` - embed the binary with `include_bytes!` for a single self-contained executable.

The vendored binary adds roughly 80 MB to the build.

## Demo app

**auditron** is a terminal compliance copilot: a TUI that streams pass/fail results with the SQL shown and Claude explanations, shipped as an ~80 MB vendored single binary.

## Related concepts

- [Embedded MCP: Rust reference](/mcp/embedded/rust) - install, API, and feature flags
- [How to use StackQL with AI agents](/ai/how-tos/use-stackql-with-ai-agents) - the MCP model and safety modes
- [StackQL MCP Architecture](/ai/architecture/stackql-mcp-architecture) - transports, modes, and audit internals
- [What is Agentic Infrastructure?](/ai/canonical-definitions/what-is-agentic-infrastructure) - the pattern this enables
