forgingstack-ration 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.
- app/__init__.py +0 -0
- app/mcp_server.py +235 -0
- forgingstack_ration-0.1.0.dist-info/METADATA +96 -0
- forgingstack_ration-0.1.0.dist-info/RECORD +8 -0
- forgingstack_ration-0.1.0.dist-info/WHEEL +5 -0
- forgingstack_ration-0.1.0.dist-info/entry_points.txt +2 -0
- forgingstack_ration-0.1.0.dist-info/licenses/LICENSE +21 -0
- forgingstack_ration-0.1.0.dist-info/top_level.txt +1 -0
app/__init__.py
ADDED
|
File without changes
|
app/mcp_server.py
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"""MCP wrapper client for ForgingStack Ration, v0.1.0.
|
|
2
|
+
|
|
3
|
+
An agent-native service must be usable by agents, not just by humans
|
|
4
|
+
with curl. MCP is the standard interface agents already speak, so the
|
|
5
|
+
wrapper exposes the same live REST API as MCP tools, with no duplicated
|
|
6
|
+
decision logic: every tool call goes through the REST API, so metering
|
|
7
|
+
and receipts stay in one place. The service itself is private; this
|
|
8
|
+
file is only a client.
|
|
9
|
+
|
|
10
|
+
Protocol: JSON-RPC 2.0 over stdio, no new dependencies (stdlib only).
|
|
11
|
+
Supported methods: initialize, ping, tools/list, tools/call.
|
|
12
|
+
Notifications (no id, e.g. notifications/initialized) get no response.
|
|
13
|
+
|
|
14
|
+
Environment:
|
|
15
|
+
FORGINGSTACK_API_BASE default https://forgingstack-ration.jkbattles.workers.dev
|
|
16
|
+
FORGINGSTACK_API_KEY required; sign up via POST /v1/signup (key shown once)
|
|
17
|
+
|
|
18
|
+
Run: python -m app.mcp_server
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
import json
|
|
22
|
+
import os
|
|
23
|
+
import sys
|
|
24
|
+
import urllib.error
|
|
25
|
+
import urllib.request
|
|
26
|
+
|
|
27
|
+
BASE = os.environ.get(
|
|
28
|
+
"FORGINGSTACK_API_BASE",
|
|
29
|
+
"https://forgingstack-ration.jkbattles.workers.dev",
|
|
30
|
+
)
|
|
31
|
+
API_KEY = os.environ.get("FORGINGSTACK_API_KEY", "")
|
|
32
|
+
|
|
33
|
+
TOOLS = [
|
|
34
|
+
{
|
|
35
|
+
"name": "create_pool",
|
|
36
|
+
"description": "Create a token-bucket quota pool shared by many agents. "
|
|
37
|
+
"Costs 1 credit. Returns the pool with its starting availability.",
|
|
38
|
+
"inputSchema": {
|
|
39
|
+
"type": "object",
|
|
40
|
+
"properties": {
|
|
41
|
+
"name": {"type": "string"},
|
|
42
|
+
"capacity_units": {"type": "integer"},
|
|
43
|
+
"refill_units_per_minute": {"type": "number"},
|
|
44
|
+
"per_agent_cap_units": {"type": "integer"},
|
|
45
|
+
"burst_units": {"type": "integer"},
|
|
46
|
+
},
|
|
47
|
+
"required": [
|
|
48
|
+
"name", "capacity_units", "refill_units_per_minute",
|
|
49
|
+
"per_agent_cap_units", "burst_units",
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"name": "draw",
|
|
55
|
+
"description": "Draw units from a pool for one agent. Returns allowed "
|
|
56
|
+
"or denied, a reason, and a hash-chained receipt. Costs 1 credit "
|
|
57
|
+
"per draw call, allowed or denied; an idempotency key replay is free.",
|
|
58
|
+
"inputSchema": {
|
|
59
|
+
"type": "object",
|
|
60
|
+
"properties": {
|
|
61
|
+
"pool_id": {"type": "integer"},
|
|
62
|
+
"agent_id": {"type": "string"},
|
|
63
|
+
"units": {"type": "integer"},
|
|
64
|
+
"idempotency_key": {"type": "string"},
|
|
65
|
+
},
|
|
66
|
+
"required": ["pool_id", "agent_id", "units"],
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
"name": "get_pool",
|
|
71
|
+
"description": "Fetch one pool by id, with its current available units "
|
|
72
|
+
"(refill applied).",
|
|
73
|
+
"inputSchema": {
|
|
74
|
+
"type": "object",
|
|
75
|
+
"properties": {"pool_id": {"type": "integer"}},
|
|
76
|
+
"required": ["pool_id"],
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
"name": "list_pools",
|
|
81
|
+
"description": "List all pools owned by the configured API key.",
|
|
82
|
+
"inputSchema": {"type": "object", "properties": {}},
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
"name": "topup_pool",
|
|
86
|
+
"description": "Add units to a pool, clamped at capacity. Free — no "
|
|
87
|
+
"billing credit is charged.",
|
|
88
|
+
"inputSchema": {
|
|
89
|
+
"type": "object",
|
|
90
|
+
"properties": {
|
|
91
|
+
"pool_id": {"type": "integer"},
|
|
92
|
+
"units": {"type": "integer"},
|
|
93
|
+
},
|
|
94
|
+
"required": ["pool_id", "units"],
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"name": "delete_pool",
|
|
99
|
+
"description": "Delete a pool and its draws. No billing credit is "
|
|
100
|
+
"charged or refunded.",
|
|
101
|
+
"inputSchema": {
|
|
102
|
+
"type": "object",
|
|
103
|
+
"properties": {"pool_id": {"type": "integer"}},
|
|
104
|
+
"required": ["pool_id"],
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
"name": "get_credits",
|
|
109
|
+
"description": "Remaining credit balance for the configured API key.",
|
|
110
|
+
"inputSchema": {"type": "object", "properties": {}},
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
"name": "get_receipt",
|
|
114
|
+
"description": "Fetch a past draw receipt by id.",
|
|
115
|
+
"inputSchema": {
|
|
116
|
+
"type": "object",
|
|
117
|
+
"properties": {"receipt_id": {"type": "integer"}},
|
|
118
|
+
"required": ["receipt_id"],
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
]
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _http(method, path, body=None, idempotency_key=None):
|
|
125
|
+
req = urllib.request.Request(
|
|
126
|
+
BASE + path,
|
|
127
|
+
data=json.dumps(body).encode() if body is not None else None,
|
|
128
|
+
method=method,
|
|
129
|
+
)
|
|
130
|
+
req.add_header("X-API-Key", API_KEY)
|
|
131
|
+
req.add_header("Content-Type", "application/json")
|
|
132
|
+
# A User-Agent is required: the default Python urllib agent is
|
|
133
|
+
# rejected (Cloudflare 1010) through some egress paths.
|
|
134
|
+
req.add_header("User-Agent", "ForgingStack-Ration/0.1.0")
|
|
135
|
+
if idempotency_key:
|
|
136
|
+
req.add_header("Idempotency-Key", idempotency_key)
|
|
137
|
+
try:
|
|
138
|
+
with urllib.request.urlopen(req, timeout=30) as resp:
|
|
139
|
+
return resp.status, json.loads(resp.read().decode() or "{}")
|
|
140
|
+
except urllib.error.HTTPError as e:
|
|
141
|
+
try:
|
|
142
|
+
return e.code, json.loads(e.read().decode() or "{}")
|
|
143
|
+
except Exception:
|
|
144
|
+
return e.code, {"detail": "api error"}
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _text(payload, is_error=False):
|
|
148
|
+
return {
|
|
149
|
+
"content": [{"type": "text", "text": json.dumps(payload)}],
|
|
150
|
+
"isError": is_error,
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def handle(msg):
|
|
155
|
+
method = msg.get("method")
|
|
156
|
+
params = msg.get("params") or {}
|
|
157
|
+
if method == "initialize":
|
|
158
|
+
return {
|
|
159
|
+
"protocolVersion": "2024-11-05",
|
|
160
|
+
"capabilities": {"tools": {}},
|
|
161
|
+
"serverInfo": {"name": "forgingstack-ration", "version": "0.1.0"},
|
|
162
|
+
}
|
|
163
|
+
if method == "ping":
|
|
164
|
+
return {}
|
|
165
|
+
if method == "tools/list":
|
|
166
|
+
return {"tools": TOOLS}
|
|
167
|
+
if method == "tools/call":
|
|
168
|
+
name = params.get("name")
|
|
169
|
+
args = params.get("arguments") or {}
|
|
170
|
+
if name == "create_pool":
|
|
171
|
+
body = {k: args[k] for k in
|
|
172
|
+
("name", "capacity_units", "refill_units_per_minute",
|
|
173
|
+
"per_agent_cap_units", "burst_units")}
|
|
174
|
+
status, data = _http("POST", "/v1/pools", body)
|
|
175
|
+
return _text(data, is_error=status >= 400)
|
|
176
|
+
if name == "draw":
|
|
177
|
+
body = {"agent_id": args["agent_id"], "units": args["units"]}
|
|
178
|
+
status, data = _http(
|
|
179
|
+
"POST", f"/v1/pools/{args['pool_id']}/draw", body,
|
|
180
|
+
args.get("idempotency_key"),
|
|
181
|
+
)
|
|
182
|
+
return _text(data, is_error=status >= 400)
|
|
183
|
+
if name == "get_pool":
|
|
184
|
+
status, data = _http("GET", f"/v1/pools/{args['pool_id']}")
|
|
185
|
+
return _text(data, is_error=status >= 400)
|
|
186
|
+
if name == "list_pools":
|
|
187
|
+
status, data = _http("GET", "/v1/pools")
|
|
188
|
+
return _text(data, is_error=status >= 400)
|
|
189
|
+
if name == "topup_pool":
|
|
190
|
+
status, data = _http(
|
|
191
|
+
"POST", f"/v1/pools/{args['pool_id']}/topup",
|
|
192
|
+
{"units": args["units"]},
|
|
193
|
+
)
|
|
194
|
+
return _text(data, is_error=status >= 400)
|
|
195
|
+
if name == "delete_pool":
|
|
196
|
+
status, data = _http("DELETE", f"/v1/pools/{args['pool_id']}")
|
|
197
|
+
return _text(data, is_error=status >= 400)
|
|
198
|
+
if name == "get_credits":
|
|
199
|
+
status, data = _http("GET", "/v1/credits")
|
|
200
|
+
return _text(data, is_error=status >= 400)
|
|
201
|
+
if name == "get_receipt":
|
|
202
|
+
status, data = _http(
|
|
203
|
+
"GET", f"/v1/receipts/{args['receipt_id']}"
|
|
204
|
+
)
|
|
205
|
+
return _text(data, is_error=status >= 400)
|
|
206
|
+
raise ValueError(f"unknown tool: {name}")
|
|
207
|
+
raise ValueError(f"unknown method: {method}")
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def main():
|
|
211
|
+
for line in sys.stdin:
|
|
212
|
+
line = line.strip()
|
|
213
|
+
if not line:
|
|
214
|
+
continue
|
|
215
|
+
try:
|
|
216
|
+
msg = json.loads(line)
|
|
217
|
+
except json.JSONDecodeError:
|
|
218
|
+
continue
|
|
219
|
+
# Notifications carry no id and get no response.
|
|
220
|
+
if "id" not in msg:
|
|
221
|
+
continue
|
|
222
|
+
try:
|
|
223
|
+
result = handle(msg)
|
|
224
|
+
out = {"jsonrpc": "2.0", "id": msg["id"], "result": result}
|
|
225
|
+
except Exception as exc: # noqa: BLE001 - MCP error surface
|
|
226
|
+
out = {
|
|
227
|
+
"jsonrpc": "2.0", "id": msg["id"],
|
|
228
|
+
"error": {"code": -32603, "message": str(exc)},
|
|
229
|
+
}
|
|
230
|
+
sys.stdout.write(json.dumps(out) + "\n")
|
|
231
|
+
sys.stdout.flush()
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
if __name__ == "__main__":
|
|
235
|
+
main()
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: forgingstack-ration
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: One quota pool, many agents, no overruns.
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Dynamic: license-file
|
|
10
|
+
|
|
11
|
+
# ForgingStack Ration
|
|
12
|
+
|
|
13
|
+
One quota pool, many agents, no overruns.
|
|
14
|
+
|
|
15
|
+
mcp-name: io.github.jdkabattles/forgingstack-ration
|
|
16
|
+
|
|
17
|
+
This is the **public client** for Ration. The service itself — the
|
|
18
|
+
token bucket, the receipt chain, the key store, and billing — runs
|
|
19
|
+
on ForgingStack's private service and is **not** in this repo.
|
|
20
|
+
Copying this repo does not give you the service: you still need an
|
|
21
|
+
API key and credits, from signup below.
|
|
22
|
+
|
|
23
|
+
Live API: https://forgingstack-ration.jkbattles.workers.dev
|
|
24
|
+
|
|
25
|
+
## What it does
|
|
26
|
+
|
|
27
|
+
A principal creates a pool with a capacity, a refill rate, a
|
|
28
|
+
per-agent cap, and a burst limit. Agents draw units from it. The
|
|
29
|
+
pool enforces the limits and issues a hash-chained receipt for
|
|
30
|
+
every draw attempt — allowed **and** denied.
|
|
31
|
+
|
|
32
|
+
A draw is denied when:
|
|
33
|
+
|
|
34
|
+
- the units exceed the burst limit (`draw_exceeds_burst`)
|
|
35
|
+
- the pool does not have enough available units
|
|
36
|
+
(`pool_insufficient_units`)
|
|
37
|
+
- the agent's allowed draws inside the rolling 60-minute window
|
|
38
|
+
would exceed the per-agent cap (`per_agent_cap_exceeded`)
|
|
39
|
+
|
|
40
|
+
The per-agent cap window is a rolling 60 minutes — a documented
|
|
41
|
+
design choice, not a calendar window.
|
|
42
|
+
|
|
43
|
+
## Signup (v1)
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
curl -X POST https://forgingstack-ration.jkbattles.workers.dev/v1/signup \
|
|
47
|
+
-H "Content-Type: application/json" \
|
|
48
|
+
-d '{"email":"you@example.com"}'
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Returns an API key **once** and 100 starter credits. Save the key
|
|
52
|
+
immediately — it is stored only as a hash and cannot be shown again.
|
|
53
|
+
Click the verification link in the signup email before the key can
|
|
54
|
+
create pools or draw: until then, pool creation and draws return
|
|
55
|
+
403 `email_not_verified`.
|
|
56
|
+
|
|
57
|
+
## Buying more credits
|
|
58
|
+
|
|
59
|
+
1 credit per pool creation, 1 credit per draw call (allowed or
|
|
60
|
+
denied). Top-ups, pool reads, pool lists, and deletes are free.
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
curl -X POST https://forgingstack-ration.jkbattles.workers.dev/v1/billing/checkout \
|
|
64
|
+
-H "X-API-Key: YOUR_PRIMARY_KEY"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Returns the Stripe Payment Link: **$10 for 1,000 credits**. Pay on
|
|
68
|
+
Stripe's hosted page with the same email you signed up with.
|
|
69
|
+
Credits land once the payment is verified (within the hour).
|
|
70
|
+
|
|
71
|
+
## Use it (MCP, stdio)
|
|
72
|
+
|
|
73
|
+
Set the key, then run the MCP server in this repo:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
export FORGINGSTACK_API_KEY="your-key-here"
|
|
77
|
+
python -m app.mcp_server
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Tools: `create_pool`, `draw`, `get_pool`, `list_pools`,
|
|
81
|
+
`topup_pool`, `delete_pool`, `get_credits`, `get_receipt`.
|
|
82
|
+
See `SKILL.md` for when to use it and when not to.
|
|
83
|
+
|
|
84
|
+
## Price
|
|
85
|
+
|
|
86
|
+
1 credit per pool creation. 1 credit per draw call — a denied draw
|
|
87
|
+
still costs 1 credit, the draw was performed. An idempotent replay
|
|
88
|
+
(same `Idempotency-Key`) is free and returns the original receipt.
|
|
89
|
+
|
|
90
|
+
## Files in this repo
|
|
91
|
+
|
|
92
|
+
- `app/mcp_server.py` — MCP wrapper client (stdio, stdlib only)
|
|
93
|
+
- `SKILL.md` — when to use it, how to read allowed/denied
|
|
94
|
+
- `agent-card.json` — A2A-style discovery card
|
|
95
|
+
- `server.json` — MCP Registry metadata
|
|
96
|
+
- `pyproject.toml` — package metadata for the MCP client
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
app/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
app/mcp_server.py,sha256=htWmFKWGeWzrJjDgbonlpY0Ge4FZ4fp7xqt1JXzc2Mg,8318
|
|
3
|
+
forgingstack_ration-0.1.0.dist-info/licenses/LICENSE,sha256=kDoUXgBDydvygXjgDe1nE4S2ZeXHI2C_4tc_-THZ2S4,1073
|
|
4
|
+
forgingstack_ration-0.1.0.dist-info/METADATA,sha256=BagEmNExWj_QYDJv5kus8L_Pm2HFgIMxbv3JZrPNaks,3157
|
|
5
|
+
forgingstack_ration-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
6
|
+
forgingstack_ration-0.1.0.dist-info/entry_points.txt,sha256=tEVEsrAI9qg3mz3tWg2RAUqxZcSsfMKAz4Q8c4CohXg,60
|
|
7
|
+
forgingstack_ration-0.1.0.dist-info/top_level.txt,sha256=io9g7LCbfmTG1SFKgEOGXmCFB9uMP2H5lerm0HiHWQE,4
|
|
8
|
+
forgingstack_ration-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jonathan Battles
|
|
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
|
+
app
|