How to embed the StackQL MCP server in a Rust application
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
- Add the crate to
Cargo.toml:
stackql-mcp = "0.1"
- Build the server and list tools. The builder defaults to
Mode::ReadOnly:
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.
- Choose a mode.
.mode()defaults toMode::ReadOnly. SetMode::Safe,Mode::DeleteSafe, orMode::FullAccessonly 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 withinclude_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 - install, API, and feature flags
- How to use StackQL with AI agents - the MCP model and safety modes
- StackQL MCP Architecture - transports, modes, and audit internals
- What is Agentic Infrastructure? - the pattern this enables