stackdeck-mcp-writes 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stackdeck_mcp_writes/__init__.py +62 -0
- stackdeck_mcp_writes/canonical.py +149 -0
- stackdeck_mcp_writes/errors.py +86 -0
- stackdeck_mcp_writes/guard.py +698 -0
- stackdeck_mcp_writes/py.typed +0 -0
- stackdeck_mcp_writes/redis_script.py +123 -0
- stackdeck_mcp_writes/stores.py +526 -0
- stackdeck_mcp_writes/timing.py +34 -0
- stackdeck_mcp_writes-0.1.0.dist-info/METADATA +103 -0
- stackdeck_mcp_writes-0.1.0.dist-info/RECORD +13 -0
- stackdeck_mcp_writes-0.1.0.dist-info/WHEEL +5 -0
- stackdeck_mcp_writes-0.1.0.dist-info/licenses/LICENSE +21 -0
- stackdeck_mcp_writes-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
from .canonical import MAX_SAFE_INTEGER
|
|
6
|
+
from .errors import WriteSafetyError
|
|
7
|
+
|
|
8
|
+
DEFAULT_SPENT_KEY_RETENTION_MS = 604_800_000
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def current_ms() -> int:
|
|
12
|
+
return time.time_ns() // 1_000_000
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def timestamp(value: int, field: str = "now_ms") -> int:
|
|
16
|
+
if type(value) is not int or not 0 <= value <= MAX_SAFE_INTEGER:
|
|
17
|
+
raise WriteSafetyError("validation", f"{field} must be a non-negative safe integer.")
|
|
18
|
+
return value
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def duration(value: int, field: str) -> int:
|
|
22
|
+
if type(value) is not int or not 0 < value <= MAX_SAFE_INTEGER:
|
|
23
|
+
raise WriteSafetyError("validation", f"{field} must be a positive safe integer.")
|
|
24
|
+
return value
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def deadline(now_ms: int, value: int, field: str) -> int:
|
|
28
|
+
return timestamp(timestamp(now_ms) + duration(value, field), field)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def retention(ttl_ms: int, spent_key_retention_ms: int = DEFAULT_SPENT_KEY_RETENTION_MS) -> int:
|
|
32
|
+
if duration(spent_key_retention_ms, "spent_key_retention_ms") < duration(ttl_ms, "ttl_ms"):
|
|
33
|
+
raise WriteSafetyError("validation", "spent_key_retention_ms must be greater than or equal to ttl_ms.")
|
|
34
|
+
return spent_key_retention_ms
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: stackdeck-mcp-writes
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Safe writes for MCP servers: idempotency keys, conflict checks, in-flight locks, dry-run previews, and write receipts. Zero dependencies.
|
|
5
|
+
Author: StackDeck
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://stackdeck.dev/mcp-writes
|
|
8
|
+
Project-URL: Documentation, https://github.com/stackdeckhq/mcp-writes/blob/main/SPEC.md
|
|
9
|
+
Project-URL: Source, https://github.com/stackdeckhq/mcp-writes
|
|
10
|
+
Project-URL: Issues, https://github.com/stackdeckhq/mcp-writes/issues
|
|
11
|
+
Keywords: mcp,fastmcp,model-context-protocol,idempotency,retries,receipts,write-safety,dry-run,ai-agents,stackdeck
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# stackdeck-mcp-writes
|
|
27
|
+
|
|
28
|
+
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.
|
|
29
|
+
|
|
30
|
+
[Docs and interactive retry demo](https://stackdeck.dev/mcp-writes) · [Specification](https://github.com/stackdeckhq/mcp-writes/blob/main/SPEC.md) · [Agent retry rules](https://github.com/stackdeckhq/mcp-writes/blob/main/docs/agents.md) · [Adoption guide](https://github.com/stackdeckhq/mcp-writes/blob/main/docs/SKILL.md) · [Source on GitHub](https://github.com/stackdeckhq/mcp-writes)
|
|
31
|
+
|
|
32
|
+
## Why this exists
|
|
33
|
+
|
|
34
|
+
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.
|
|
35
|
+
|
|
36
|
+
```text
|
|
37
|
+
Plain write handler: 2 charges ($50.00) handler ran again
|
|
38
|
+
With stackdeck-mcp-writes: 1 charge ($25.00) replay: true
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
pip install stackdeck-mcp-writes
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Quick start
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from stackdeck_mcp_writes import MemoryStore, WriteGuard, safe_write_tool
|
|
51
|
+
|
|
52
|
+
guard = WriteGuard(store=MemoryStore())
|
|
53
|
+
caller = {"workspace_id": "workspace_1"}
|
|
54
|
+
|
|
55
|
+
@safe_write_tool(guard, name="invoices.refund", resolve_scope=lambda _args, _ctx: caller["workspace_id"])
|
|
56
|
+
def refund_invoice(invoice_id: str, amount_cents: int) -> dict[str, object]:
|
|
57
|
+
return {"refund_id": f"re_{invoice_id}", "amount_cents": amount_cents}
|
|
58
|
+
|
|
59
|
+
args = dict(invoice_id="invoice_42", amount_cents=500, idempotency_key="refund-42")
|
|
60
|
+
assert refund_invoice(**args)["replay"] is False
|
|
61
|
+
assert refund_invoice(**args)["replay"] is True
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## FastMCP and official MCP server integration
|
|
65
|
+
|
|
66
|
+
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`:
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from fastmcp import Context, FastMCP
|
|
70
|
+
from stackdeck_mcp_writes import PostgresStore, WriteContext, WriteGuard, safe_write_tool
|
|
71
|
+
|
|
72
|
+
# Pass an autocommit psycopg/DB-API connection (or a query_fn callable).
|
|
73
|
+
store = PostgresStore(pg_conn)
|
|
74
|
+
store.init()
|
|
75
|
+
guard = WriteGuard(store=store)
|
|
76
|
+
mcp = FastMCP("billing")
|
|
77
|
+
|
|
78
|
+
@mcp.tool()
|
|
79
|
+
@safe_write_tool(
|
|
80
|
+
guard,
|
|
81
|
+
name="payments.refund",
|
|
82
|
+
resolve_scope=lambda _args, ctx: verified_actor(ctx).workspace_id,
|
|
83
|
+
authorize=lambda args, write: can_refund_payment(current_verified_actor(), write.scope, args["payment_intent_id"]),
|
|
84
|
+
)
|
|
85
|
+
async def refund_payment(payment_intent_id: str, amount_cents: int, ctx: Context, write: WriteContext) -> dict[str, object]:
|
|
86
|
+
refund = await stripe_refund(payment_intent_id, amount_cents, idempotency_key=write.provider_key)
|
|
87
|
+
return {"refund_id": refund["id"], "amount_cents": refund["amount"], "status": refund["status"]}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Important usage rules
|
|
91
|
+
|
|
92
|
+
- **Keyword-only direct calls:** Construct with `WriteGuard(store=store)` and call `guard.execute(..., input_value=...)` or `await guard.execute_async(..., input_value=...)`.
|
|
93
|
+
- **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).
|
|
94
|
+
- **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.
|
|
95
|
+
- **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.
|
|
96
|
+
- **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.
|
|
97
|
+
- **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).
|
|
98
|
+
|
|
99
|
+
See the [Python refund example](https://github.com/stackdeckhq/mcp-writes/blob/main/examples/refund-tool-py/demo.py), [SPEC.md](https://github.com/stackdeckhq/mcp-writes/blob/main/SPEC.md), and [agent retry rules](https://github.com/stackdeckhq/mcp-writes/blob/main/docs/agents.md).
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
MIT. See [LICENSE](https://github.com/stackdeckhq/mcp-writes/blob/main/LICENSE).
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
stackdeck_mcp_writes/__init__.py,sha256=BtkMPoF5g-FZFc_iD0TIcgoSbLoLAfx1LKdJXKFGvNo,1540
|
|
2
|
+
stackdeck_mcp_writes/canonical.py,sha256=n2g9tYZdrvIUB-YAZ1ho3xkuOm-DrWllrKmhVkw9Fb4,5190
|
|
3
|
+
stackdeck_mcp_writes/errors.py,sha256=4rt2fqnADS-dAa0JFeGwtcM_9KAyF9iNXVSUJdK3Hfc,2294
|
|
4
|
+
stackdeck_mcp_writes/guard.py,sha256=iGM4-vAeajJ0KkPBbhTuPBxnmTiIJzI947AUPUHq2Us,33312
|
|
5
|
+
stackdeck_mcp_writes/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
stackdeck_mcp_writes/redis_script.py,sha256=0lc1jbg5BoAcC64gzkGr5Bs0oKJEUkVlOXBU48hQ77w,4747
|
|
7
|
+
stackdeck_mcp_writes/stores.py,sha256=L9Ce_dCKPuiWO9wl6mTj1AirmjZ5ptMv1ItQ-arFDgU,28259
|
|
8
|
+
stackdeck_mcp_writes/timing.py,sha256=MPCt6cjBVOsEuKtemDkZ-Eqi0oKr1rlobO8S8vEXJQ0,1172
|
|
9
|
+
stackdeck_mcp_writes-0.1.0.dist-info/licenses/LICENSE,sha256=KFZPha3jIByEKJi_aDtHArSXpKTJRHWRqyct5pihGAg,1070
|
|
10
|
+
stackdeck_mcp_writes-0.1.0.dist-info/METADATA,sha256=4fEDGIR-iPX1DETFCCHLn-41l7fAPZ41qI-UN36n7AY,5976
|
|
11
|
+
stackdeck_mcp_writes-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
12
|
+
stackdeck_mcp_writes-0.1.0.dist-info/top_level.txt,sha256=c6lNpmVfg72jkZ8nY6fDWoF8jCwmt5vu6tGVB06xYNQ,21
|
|
13
|
+
stackdeck_mcp_writes-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TrueWear, LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
stackdeck_mcp_writes
|