# stackdeck-mcp-writes
Safe writes for Model Context Protocol (MCP) servers in Rust (1.80+) with zero dependencies. Canonical JSON, FIPS 180-4 SHA-256 input binding, authorization, leases, scoped provider idempotency keys, dry-run previews, and replayable JSON receipts.
[Docs and interactive retry demo](https://stackdeck.dev/mcp-writes) · [Specification](https://github.com/stackdeckhq/mcp-writes/blob/main/SPEC.md) · [Language support matrix](https://github.com/stackdeckhq/mcp-writes/blob/main/SPEC.md#1-language-support) · [Source on GitHub](https://github.com/stackdeckhq/mcp-writes)
## Why this exists
When an MCP tool call times out after your server charges a card or writes to a database, the agent retries. Without an idempotency guard, the write runs twice.
```text
Plain write handler: 2 charges ($50.00) handler ran again
With stackdeck-mcp-writes: 1 charge ($25.00) replay: true
```
## Install
```sh
cargo add stackdeck-mcp-writes
```
## Quick start
```rust
use stackdeck_mcp_writes::{ExecuteRequest, JsonValue, MemoryStore, WriteGuard, WriteOutcome, WriteSafetyError};
fn main() -> Result<(), WriteSafetyError> {
let guard = WriteGuard::new(MemoryStore::new());
let caller_workspace = "workspace_1";
let execute = || {
let request = ExecuteRequest {
scope: Some(caller_workspace),
idempotency_key: Some("refund-42"),
..ExecuteRequest::new("invoices.refund", JsonValue::Int(500))
};
guard.execute(request, |_, _| Ok::<_, WriteSafetyError>(JsonValue::String("refund_42".into())))
};
assert!(matches!(execute()?, WriteOutcome::Committed { replay: false, .. }));
assert!(matches!(execute()?, WriteOutcome::Committed { replay: true, .. }));
Ok(())
}
```
## What is included
- `WriteGuard` with `execute`, `execute_with_preview`, `execute_with_pre_effect`, and `execute_with_preview_and_pre_effect` (supporting borrowed handler error types via `HandlerError::BeforeEffect`).
- Canonical JSON (`canonical_json`, `canonical_hash`) and zero-dependency SHA-256 passing the shared conformance vectors with TypeScript and Python.
- `WriteStore` trait and in-memory `MemoryStore` with OS-random process-independent claim tokens.
- SQL schema constants (`SQLITE_SCHEMA_SQL`, `POSTGRES_SCHEMA_SQL`) and Redis Lua scripts plus key encoding helpers (`REDIS_CLAIM_LUA`, `REDIS_COMPLETE_LUA`, `REDIS_EXTEND_LEASE_LUA`, `REDIS_RELEASE_LUA`, `build_redis_store_key`) for building custom database adapters.
See the [language support matrix](https://github.com/stackdeckhq/mcp-writes/blob/main/SPEC.md#1-language-support) and [agent retry rules](https://github.com/stackdeckhq/mcp-writes/blob/main/docs/agents.md).
## License
MIT. See [LICENSE](https://github.com/stackdeckhq/mcp-writes/blob/main/LICENSE).