Skip to main content

stackdeck-mcp-writes

Safe writes for Model Context Protocol (MCP) and FastMCP servers in Python 3.10+. Idempotency keys, SHA-256 input binding, authorization, leases, dry-run previews, human approval hooks, and replayable write receipts with zero runtime dependencies.

Docs and interactive retry demo · Specification · Agent retry rules · Adoption guide · Source on GitHub

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.

Plain write handler:         2 charges ($50.00)  handler ran again
With stackdeck-mcp-writes:   1 charge  ($25.00)  replay: true

Install

pip install stackdeck-mcp-writes

Quick start

from stackdeck_mcp_writes import MemoryStore, WriteGuard, safe_write_tool

guard = WriteGuard(store=MemoryStore())
caller = {"workspace_id": "workspace_1"}

@safe_write_tool(guard, name="invoices.refund", resolve_scope=lambda _args, _ctx: caller["workspace_id"])
def refund_invoice(invoice_id: str, amount_cents: int) -> dict[str, object]:
    return {"refund_id": f"re_{invoice_id}", "amount_cents": amount_cents}

args = dict(invoice_id="invoice_42", amount_cents=500, idempotency_key="refund-42")
assert refund_invoice(**args)["replay"] is False
assert refund_invoice(**args)["replay"] is True

FastMCP and official MCP server integration

Pass an autocommit psycopg/DB-API connection (or a query_fn callable) to PostgresStore. Derive resolve_scope from a verified workspace identity (never from unverified request metadata such as ctx.client_id), authorize the verified actor against the target payment, and return a plain JSON dict:

from fastmcp import Context, FastMCP
from stackdeck_mcp_writes import PostgresStore, WriteContext, WriteGuard, safe_write_tool

# Pass an autocommit psycopg/DB-API connection (or a query_fn callable).
store = PostgresStore(pg_conn)
store.init()
guard = WriteGuard(store=store)
mcp = FastMCP("billing")

@mcp.tool()
@safe_write_tool(
    guard,
    name="payments.refund",
    resolve_scope=lambda _args, ctx: verified_actor(ctx).workspace_id,
    authorize=lambda args, write: can_refund_payment(current_verified_actor(), write.scope, args["payment_intent_id"]),
)
async def refund_payment(payment_intent_id: str, amount_cents: int, ctx: Context, write: WriteContext) -> dict[str, object]:
    refund = await stripe_refund(payment_intent_id, amount_cents, idempotency_key=write.provider_key)
    return {"refund_id": refund["id"], "amount_cents": refund["amount"], "status": refund["status"]}

Important usage rules

  • Keyword-only direct calls: Construct with WriteGuard(store=store) and call guard.execute(..., input_value=...) or await guard.execute_async(..., input_value=...).
  • Stores included: MemoryStore(), SqliteStore(path_or_memory) (store.init()), PostgresStore(pg_conn_or_query_fn) (store.init() with an autocommit DB-API connection or callable), and RedisStore(client, key_prefix=...) (redis-py client).
  • Async SQLite: Pass a database path or ":memory:" (not a sqlite3.Connection) to SqliteStore so worker threads can run safely, then call store.close() on shutdown.
  • Sync handlers with official mcp.server.fastmcp: Set async_tool=True on @safe_write_tool so the wrapper is async and offloads the handler and store calls to worker threads. FastMCP requires an idempotency_key string even on dry_run=True preview calls.
  • Plain JSON receipts and pre-effect errors: Return plain JSON (dict, list, str, int, float, bool, None), or pass both project_receipt and resolve_receipt. Raise PreEffectError only when a handler fails before attempting any side effect so the lock is released. Call await write.extend_lease(...) during slow writes.
  • Stress proof: Verified cross-language conformance with TypeScript and Rust, plus live PostgreSQL 18 and Redis 8 stress checks (99/99 in-flight duplicates blocked, 100/100 post-commit replays, 1 handler run, 0 leaked locks).

See the Python refund example, SPEC.md, and agent retry rules.

License

MIT. See LICENSE.

Metadata

Release files for stackdeck-mcp-writes 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stackdeck-mcp-writes 0.1.0
File Size Uploaded Provenance Actions
stackdeck_mcp_writes-0.1.0.tar.gz 35.7 kB With provenance Details for stackdeck_mcp_writes-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for stackdeck-mcp-writes 0.1.0
File Interpreter ABI Platform Actions
stackdeck_mcp_writes-0.1.0-py3-none-any.whl Python 3 none any Details for stackdeck_mcp_writes-0.1.0-py3-none-any.whl

Total release size: 59.3 kB

Release files / stackdeck_mcp_writes-0.1.0.tar.gz

Download URL stackdeck_mcp_writes-0.1.0.tar.gz
Size 35.7 kB
Tags Source
SHA-256 checksum
How to use checksums
593d418f7d1686b0eb0364c9aeecfc0b426c6f475276e4f7c34090484e379a5c
BLAKE2b-256 checksum
How to use checksums
b34181087737a2d799acd54f83d61ae1e970ef27bf80f010d0babdfe0916cd30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

Release files / stackdeck_mcp_writes-0.1.0-py3-none-any.whl

Download URL stackdeck_mcp_writes-0.1.0-py3-none-any.whl
Size 23.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd40e7ea40d14769485e26ffd11871b9221bef4f63187c9124298d2115a77db7
BLAKE2b-256 checksum
How to use checksums
a11f125a98dc06b989c546c95af82c39bbfa2f898a9ba11984c145c55c9ce23e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

Security for stackdeck-mcp-writes 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for stackdeck_mcp_writes-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page