| Crates.io | kodegen_mcp_client |
| lib.rs | kodegen_mcp_client |
| version | 0.10.9 |
| created_at | 2025-10-28 20:22:06.606975+00 |
| updated_at | 2026-01-02 15:02:49.700964+00 |
| description | KODEGEN.ᴀɪ: Database query and schema exploration MCP tools for AI agents. |
| homepage | https://kodegen.ai |
| repository | https://github.com/cyrup-ai/kodegen-mcp-client |
| max_upload_size | |
| id | 1905586 |
| size | 4,080,513 |
A Rust client library for interacting with MCP (Model Context Protocol) servers, specifically designed for KODEGEN.ᴀɪ database query and schema exploration tools.
✨ Type-Safe MCP Client
call_tool_typed<T>()camelCase and snake_case field names🚀 Async & Efficient
🔌 Multiple Transports
🛡️ Robust Error Handling
Add to your Cargo.toml:
[dependencies]
kodegen_mcp_client = "0.1"
Requirements:
use kodegen_mcp_client::{create_streamable_client, tools, X_SESSION_PWD, X_SESSION_GITROOT};
use reqwest::header::{HeaderMap, HeaderValue};
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Build session context headers
let mut headers = HeaderMap::new();
let cwd = std::env::current_dir()?;
headers.insert(X_SESSION_PWD, HeaderValue::from_str(&cwd.to_string_lossy())?);
// Add git root if available
if let Some(git_root) = find_git_root(&cwd) {
headers.insert(X_SESSION_GITROOT, HeaderValue::from_str(&git_root.to_string_lossy())?);
}
// Create client connection with session headers
let (client, _conn) = create_streamable_client("http://localhost:8000/mcp", headers).await?;
// List available tools
let tools = client.list_tools().await?;
println!("Available tools: {}", tools.len());
// Call a tool with JSON arguments
let result = client.call_tool(
tools::LIST_SCHEMAS,
json!({}),
).await?;
println!("Result: {:?}", result);
Ok(())
}
fn find_git_root(start: &std::path::Path) -> Option<std::path::PathBuf> {
let mut current = start.to_path_buf();
loop {
if current.join(".git").exists() {
return Some(current);
}
if !current.pop() {
return None;
}
}
}
Use strongly-typed responses instead of manual JSON parsing:
use kodegen_mcp_client::responses::StartSearchResponse;
use kodegen_mcp_client::tools;
// Type-safe tool call with automatic deserialization
let response: StartSearchResponse = client
.call_tool_typed(tools::START_SEARCH, json!({
"path": "/project",
"pattern": "*.rs",
"searchType": "files"
}))
.await?;
// Use the typed response
println!("Search session ID: {}", response.session_id);
Client handles are cheap to clone and can be shared across tasks:
use reqwest::header::HeaderMap;
let (client, _conn) = create_streamable_client(url, HeaderMap::new()).await?;
// Clone the client for concurrent operations
let client2 = client.clone();
tokio::spawn(async move {
client2.list_tools().await
});
// Original client still works
client.call_tool(tools::READ_FILE, args).await?;
use reqwest::header::HeaderMap;
use tokio::time::Duration;
let (client, _conn) = create_streamable_client(url, HeaderMap::new()).await?;
// Set custom timeout (default is 10 minutes)
let client = client.with_timeout(Duration::from_secs(60));
use kodegen_mcp_client::responses::{GitHubIssuesResponse, GitHubIssue};
let response: GitHubIssuesResponse = client
.call_tool_typed(tools::LIST_ISSUES, json!({
"owner": "myorg",
"repo": "myrepo",
"state": "open"
}))
.await?;
for issue in response.issues {
println!("#{}: {}", issue.number, issue.title);
}
use kodegen_mcp_client::tools;
// Execute SQL query
let result = client.call_tool(tools::EXECUTE_SQL, json!({
"query": "SELECT * FROM users WHERE active = true",
"database": "production"
})).await?;
// Get table schema
let schema = client.call_tool(tools::GET_TABLE_SCHEMA, json!({
"table_name": "users",
"schema": "public"
})).await?;
The library uses a two-struct pattern for resource management:
KodegenClient: Cheap-to-clone handle for performing MCP operations
Arc internally for efficient cloningKodegenConnection: Non-clonable lifecycle manager
close() for graceful shutdownuse reqwest::header::HeaderMap;
let (client, _conn) = create_streamable_client(url, HeaderMap::new()).await?;
// client: Clone freely
// _conn: Hold until shutdown desired (auto-cleanup on drop)
The library provides type-safe access to 100+ KODEGEN.ᴀɪ tools across multiple categories:
All tool names are available as constants in the tools module.
The library provides comprehensive error types with context:
use kodegen_mcp_client::ClientError;
match client.call_tool(tools::READ_FILE, args).await {
Ok(result) => println!("Success: {:?}", result),
Err(ClientError::Timeout(msg)) => eprintln!("Timeout: {}", msg),
Err(ClientError::ParseError(msg)) => eprintln!("Parse error: {}", msg),
Err(ClientError::Connection(msg)) => eprintln!("Connection failed: {}", msg),
Err(e) => eprintln!("Other error: {}", e),
}
Error variants:
Protocol: MCP protocol errors
Timeout: Operation timeouts (includes duration)
ParseError: Response deserialization failures
Connection: Transport connection failures
ServiceError, InitError, Io, JoinError: Lower-level errors
use kodegen_mcp_client::create_streamable_client;
use reqwest::header::HeaderMap;
let (client, conn) = create_streamable_client("http://localhost:8000/mcp", HeaderMap::new()).await?;
use kodegen_mcp_client::transports::http::create_http_client;
let (client, conn) = create_http_client("http://localhost:8000/mcp").await?;
cargo build # Debug build
cargo build --release # Release build
cargo test # Run all tests
cargo test --lib # Library tests only
cargo clippy # Run linter
cargo fmt # Format code
cargo doc --open # Build and open docs
Dual-licensed under Apache 2.0 OR MIT terms.
See LICENSE.md for details.
Copyright © 2025 David Maple / KODEGEN.ᴀɪ