Embedding in Rust
Embedding in Rust
Section titled “Embedding in Rust”pgvis is not just a standalone server — it’s a library you can embed in any Rust application.
Add the Dependency
Section titled “Add the Dependency”[dependencies]pgvis-lib = "0.1"tokio = { version = "1", features = ["full"] }Build a REST + MCP Router
Section titled “Build a REST + MCP Router”use pgvis_lib::Builder;
#[tokio::main]async fn main() -> anyhow::Result<()> { // Build an axum Router with REST + MCP let router = Builder::new("postgres://localhost/mydb") .schemas(vec!["public"]) .with_mcp_http() .build() .await?;
// Serve with axum let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?; axum::serve(listener, router).await?; Ok(())}Build an MCP-only Server (stdio)
Section titled “Build an MCP-only Server (stdio)”use pgvis_lib::Builder;
#[tokio::main]async fn main() -> anyhow::Result<()> { let mcp = Builder::new("postgres://localhost/mydb") .schemas(vec!["public"]) .build_mcp_server() .await?;
pgvis_lib::pgvis_mcp::serve_stdio(mcp).await?; Ok(())}Configuration
Section titled “Configuration”use pgvis_lib::Builder;use pgvis_core::config::Config;
let config = Config { schemas: vec!["public".into(), "api".into()], max_rows: Some(1000), ..Default::default()};
let router = Builder::new("postgres://localhost/mydb") .config(config) .build() .await?;Architecture
Section titled “Architecture”The embed API (pgvis-lib) is the same code path the CLI uses. There is no “server-only” logic — the Builder assembles:
- A database backend (connection pool + introspection)
- A schema cache (tables, relationships, routines)
- Configuration
- An axum
Routeror MCPServer
All wired together with the same I/O-free core engine.