@latens/mcp-server 0.0.0-stage → 1.4.0
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.
- package/LICENSE +21 -0
- package/README.md +132 -2
- package/package.json +37 -4
- package/src/server.mjs +683 -0
- package/src/sizing.mjs +198 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LATENS Contributors
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,133 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@latens/mcp-server`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Read-only **Model Context Protocol (`MCP`)** JSON-RPC 2.0 stdio server for autonomous AI agents querying paginated GPU compute supply, genuine 15-minute GPU price history, deterministic LLM VRAM sizing, multi-offer TCO ranking, and on-chain `$LTNS` wallet standing on **LATENS** ([https://latens.trade](https://latens.trade)).
|
|
4
|
+
|
|
5
|
+
## Exposed Read-Only Tools
|
|
6
|
+
|
|
7
|
+
| Tool Name | Purpose | Read-Only |
|
|
8
|
+
| :- | :- | :- |
|
|
9
|
+
| `latens_list_gpu_offers` | Paginated search across 8 clouds with `model`, `min_vram_gb`, `max_price_per_gpu_usd`, `provider`, `category`, `sort` (`price_asc` default), `offset`, `limit`, and `all: true` (auto-paginates up to 500 offers) | `true` |
|
|
10
|
+
| `latens_list_gpu_models` | List indexed GPU models with min/median hourly pricing and active provider counts | `true` |
|
|
11
|
+
| `latens_get_price_history` | Query genuine recorded 15-minute, hourly, or daily min/median/max GPU price series across `24h`, `7d`, and `30d` windows (zero backfill or interpolation) | `true` |
|
|
12
|
+
| `latens_calculate_compute_cost` | Deterministic compute + persistent storage + outbound egress rental cost calculator | `true` |
|
|
13
|
+
| `latens_estimate_llm_vram` | Deterministic local LLM VRAM estimator and multi-GPU hardware sizing for inference, LoRA, and full fine-tuning | `true` |
|
|
14
|
+
| `latens_compare_offer_tco` | Rank live GPU offers by full TCO (compute + storage + egress) with optional `$LTNS` Private Desk fee schedules | `true` |
|
|
15
|
+
| `latens_get_provider_sync_status` | Verify synchronization freshness, adapter health across 8 GPU networks, `totalOffers` vs `filteredOffers` counts, and caller `$LTNS` tier | `true` |
|
|
16
|
+
| `latens_get_token_standing` | Inspect live on-chain `$LTNS` balance on Robinhood Chain (`4663`), active tier, OTC fee bps, and API RPM | `true` |
|
|
17
|
+
|
|
18
|
+
## Example: Querying Real GPU Price History
|
|
19
|
+
|
|
20
|
+
Tool call (`latens_get_price_history`):
|
|
21
|
+
|
|
22
|
+
```json
|
|
23
|
+
{
|
|
24
|
+
"name": "latens_get_price_history",
|
|
25
|
+
"arguments": {
|
|
26
|
+
"model": "H100",
|
|
27
|
+
"window": "24h",
|
|
28
|
+
"interval": "15m"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Example response shape:
|
|
34
|
+
|
|
35
|
+
```json
|
|
36
|
+
{
|
|
37
|
+
"object": "gpu_price_history",
|
|
38
|
+
"read_only": true,
|
|
39
|
+
"model": "H100",
|
|
40
|
+
"window": "24h",
|
|
41
|
+
"interval": "15m",
|
|
42
|
+
"category": "all",
|
|
43
|
+
"first_recorded_at": "2026-12-01T21:15:00Z",
|
|
44
|
+
"coverage_hours": 0.25,
|
|
45
|
+
"points": [
|
|
46
|
+
{
|
|
47
|
+
"t": "2026-12-01T21:15:00Z",
|
|
48
|
+
"min": 1.7344,
|
|
49
|
+
"median": 2.39,
|
|
50
|
+
"max": 3.29,
|
|
51
|
+
"offers": 13,
|
|
52
|
+
"providers": 4
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Example: Full Index Pagination (`all: true`)
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"name": "latens_list_gpu_offers",
|
|
63
|
+
"arguments": {
|
|
64
|
+
"sort": "price_asc",
|
|
65
|
+
"all": true
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
When `available_only` is `true` (default), paging through all offers yields the exact count reported in `status.summary.filteredOffers` (while `status.summary.totalOffers` also includes currently unavailable catalog listings).
|
|
71
|
+
|
|
72
|
+
## $LTNS Throughput Scaling (`X-Latens-Wallet`)
|
|
73
|
+
|
|
74
|
+
Set `LATENS_WALLET` to an EVM wallet holding `$LTNS` (`0x6c29b040f7d139c93d22f8d6e29b1528e3ca4bf9` on Robinhood Chain `4663`) to automatically attach `X-Latens-Wallet` on every Agent API request and scale rate limits from `60` up to `600` read RPM (`900` RPM on `/calculate`). No private key is ever used or requested.
|
|
75
|
+
|
|
76
|
+
## Security Invariants
|
|
77
|
+
|
|
78
|
+
1. **Zero Wallet Custody**: The MCP server never holds private keys or signs transactions. Read-only discovery tools carry `readOnlyHint: true`, while `x402` execution tools (`latens_x402_request_quote`, `latens_x402_settle_execution`) enforce explicit `max_spend_usd` caps and require a caller-broadcast Robinhood Chain (`4663`) `tx_hash`.
|
|
79
|
+
2. **Confidentiality Isolation**: This server communicates exclusively with `/api/v1/agent/*` and `/api/token/standing` and has zero access to encrypted Private Compute Desk RFQs or browser sessions.
|
|
80
|
+
3. **Zero Runtime Dependencies**: Pure Node.js ESM with no third-party dependencies.
|
|
81
|
+
|
|
82
|
+
## MCP Client Configuration (Primary: `npx`)
|
|
83
|
+
|
|
84
|
+
```json
|
|
85
|
+
{
|
|
86
|
+
"mcpServers": {
|
|
87
|
+
"latens": {
|
|
88
|
+
"command": "npx",
|
|
89
|
+
"args": ["-y", "@latens/mcp-server"],
|
|
90
|
+
"env": {
|
|
91
|
+
"LATENS_API_URL": "https://latens.trade",
|
|
92
|
+
"LATENS_WALLET": "0x..."
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Local Setup via `git clone`
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
git clone https://github.com/LatensRbh/latens-mcp.git
|
|
103
|
+
cd latens-mcp
|
|
104
|
+
npm test
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
```json
|
|
108
|
+
{
|
|
109
|
+
"mcpServers": {
|
|
110
|
+
"latens": {
|
|
111
|
+
"command": "node",
|
|
112
|
+
"args": ["/path/to/latens-mcp/src/server.mjs"],
|
|
113
|
+
"env": {
|
|
114
|
+
"LATENS_API_URL": "https://latens.trade",
|
|
115
|
+
"LATENS_WALLET": "0xYourRobinhoodChainWalletAddress"
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Links
|
|
123
|
+
|
|
124
|
+
- Live Platform: [https://latens.trade](https://latens.trade)
|
|
125
|
+
- OpenAPI 3.1 Specification: [https://latens.trade/api/v1/openapi.json](https://latens.trade/api/v1/openapi.json)
|
|
126
|
+
- Utility & Tier Inspector: [https://latens.trade/utility](https://latens.trade/utility)
|
|
127
|
+
- Changelog & Atom Feed: [https://latens.trade/changelog](https://latens.trade/changelog) · [https://latens.trade/feed.xml](https://latens.trade/feed.xml)
|
|
128
|
+
- Documentation: [https://latens.trade/docs](https://latens.trade/docs)
|
|
129
|
+
- X (Twitter): [@LatensCompute](https://x.com/LatensCompute)
|
|
130
|
+
|
|
131
|
+
## License
|
|
132
|
+
|
|
133
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@latens/mcp-server",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"description": "Model Context Protocol (MCP) stdio server for autonomous AI agents querying GPU supply across 8 clouds, 15-minute GPU price history, LLM VRAM sizing, multi-offer TCO comparison, $LTNS standing, and HTTP 402 (x402) payment-gated GPU execution on Robinhood Chain (4663).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"bin": {
|
|
10
|
+
"latens-mcp": "src/server.mjs"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"src",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"start": "node ./src/server.mjs",
|
|
19
|
+
"test": "node --test tests/mcp.test.mjs"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"mcp",
|
|
23
|
+
"model-context-protocol",
|
|
24
|
+
"gpu",
|
|
25
|
+
"ai-agents",
|
|
26
|
+
"ltns",
|
|
27
|
+
"compute",
|
|
28
|
+
"robinhood-chain"
|
|
29
|
+
],
|
|
30
|
+
"homepage": "https://latens.trade/docs",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/LatensRbh/latens-mcp.git"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=20.0.0"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/server.mjs
ADDED
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import readline from "node:readline";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { estimateLlmVramRequirements, rankOffersByTco } from "./sizing.mjs";
|
|
6
|
+
|
|
7
|
+
const BASE_URL = (process.env.LATENS_API_URL || "https://latens.trade").replace(/\/+$/, "");
|
|
8
|
+
const DEFAULT_WALLET = (process.env.LATENS_WALLET || "").trim();
|
|
9
|
+
const MAX_ALL_OFFERS_CAP = 500;
|
|
10
|
+
|
|
11
|
+
function buildHeaders(extra = {}, walletOverride = "") {
|
|
12
|
+
const headers = { Accept: "application/json", ...extra };
|
|
13
|
+
const candidate = (walletOverride || DEFAULT_WALLET).trim();
|
|
14
|
+
if (/^0x[a-fA-F0-9]{40}$/.test(candidate)) {
|
|
15
|
+
headers["X-Latens-Wallet"] = candidate;
|
|
16
|
+
}
|
|
17
|
+
return headers;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const TOOLS = [
|
|
21
|
+
{
|
|
22
|
+
name: "latens_list_gpu_offers",
|
|
23
|
+
description:
|
|
24
|
+
"Read-only paginated discovery of normalized GPU offers across Vast.ai, Runpod, CUDO Compute, Vultr Cloud GPU, Akash Network, Lambda Cloud, Crusoe Cloud, and Scaleway. Sorted by price_asc by default. Set all=true to auto-paginate up to 500 matching offers.",
|
|
25
|
+
annotations: {
|
|
26
|
+
readOnlyHint: true,
|
|
27
|
+
destructiveHint: false,
|
|
28
|
+
openWorldHint: true,
|
|
29
|
+
},
|
|
30
|
+
inputSchema: {
|
|
31
|
+
type: "object",
|
|
32
|
+
properties: {
|
|
33
|
+
model: {
|
|
34
|
+
type: "string",
|
|
35
|
+
description: "Canonical GPU model filter (e.g. H100 SXM, A100 SXM, RTX 4090, MI300X)",
|
|
36
|
+
},
|
|
37
|
+
gpu_model: {
|
|
38
|
+
type: "string",
|
|
39
|
+
description: "Alias for model",
|
|
40
|
+
},
|
|
41
|
+
provider: {
|
|
42
|
+
type: "string",
|
|
43
|
+
enum: ["all", "vast", "runpod", "cudo", "vultr", "akash", "lambda", "crusoe", "scaleway"],
|
|
44
|
+
},
|
|
45
|
+
category: {
|
|
46
|
+
type: "string",
|
|
47
|
+
enum: [
|
|
48
|
+
"all",
|
|
49
|
+
"all_dedicated",
|
|
50
|
+
"dedicated_instance",
|
|
51
|
+
"secure_pod",
|
|
52
|
+
"community_pod",
|
|
53
|
+
"cloud_vm",
|
|
54
|
+
"bare_metal",
|
|
55
|
+
"decentralized_lease",
|
|
56
|
+
"interruptible_spot",
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
min_vram_gb: { type: "number" },
|
|
60
|
+
max_price_per_gpu_usd: { type: "number" },
|
|
61
|
+
max_hourly_usd: { type: "number", description: "Alias for max_price_per_gpu_usd" },
|
|
62
|
+
gpu_count: { type: "integer", minimum: 1 },
|
|
63
|
+
region: { type: "string" },
|
|
64
|
+
available_only: {
|
|
65
|
+
type: "boolean",
|
|
66
|
+
default: true,
|
|
67
|
+
description: "When true (default), returns currently available offers matching status.summary.filteredOffers.",
|
|
68
|
+
},
|
|
69
|
+
sort: {
|
|
70
|
+
type: "string",
|
|
71
|
+
enum: ["price_asc", "price_desc", "vram_desc"],
|
|
72
|
+
default: "price_asc",
|
|
73
|
+
description: "Deterministic sort order. Default is price_asc.",
|
|
74
|
+
},
|
|
75
|
+
offset: {
|
|
76
|
+
type: "integer",
|
|
77
|
+
minimum: 0,
|
|
78
|
+
default: 0,
|
|
79
|
+
description: "Zero-based pagination offset.",
|
|
80
|
+
},
|
|
81
|
+
limit: { type: "integer", minimum: 1, maximum: 100, default: 15 },
|
|
82
|
+
all: {
|
|
83
|
+
type: "boolean",
|
|
84
|
+
default: false,
|
|
85
|
+
description: "When true, automatically paginates through all matching pages up to 500 offers.",
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: "latens_list_gpu_models",
|
|
92
|
+
description:
|
|
93
|
+
"Read-only summary of indexed GPU hardware models, VRAM capacities, and minimum/median USD hourly rates.",
|
|
94
|
+
annotations: {
|
|
95
|
+
readOnlyHint: true,
|
|
96
|
+
destructiveHint: false,
|
|
97
|
+
openWorldHint: true,
|
|
98
|
+
},
|
|
99
|
+
inputSchema: {
|
|
100
|
+
type: "object",
|
|
101
|
+
properties: {},
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
name: "latens_get_price_history",
|
|
106
|
+
description:
|
|
107
|
+
"Read-only historical GPU pricing time series (min, median, max USD/GPU/hr, offer count, provider count) recorded in 15-minute UTC buckets with hourly and daily rollups. Never backfills or interpolates missing intervals.",
|
|
108
|
+
annotations: {
|
|
109
|
+
readOnlyHint: true,
|
|
110
|
+
destructiveHint: false,
|
|
111
|
+
openWorldHint: true,
|
|
112
|
+
},
|
|
113
|
+
inputSchema: {
|
|
114
|
+
type: "object",
|
|
115
|
+
required: ["model"],
|
|
116
|
+
properties: {
|
|
117
|
+
model: {
|
|
118
|
+
type: "string",
|
|
119
|
+
description: "GPU model name or family (e.g. H100, H100 SXM, A100 SXM, RTX 4090)",
|
|
120
|
+
},
|
|
121
|
+
window: {
|
|
122
|
+
type: "string",
|
|
123
|
+
enum: ["24h", "7d", "30d"],
|
|
124
|
+
default: "24h",
|
|
125
|
+
description: "Historical lookback window.",
|
|
126
|
+
},
|
|
127
|
+
interval: {
|
|
128
|
+
type: "string",
|
|
129
|
+
enum: ["15m", "1h", "1d"],
|
|
130
|
+
default: "15m",
|
|
131
|
+
description: "Time bucket aggregation interval.",
|
|
132
|
+
},
|
|
133
|
+
category: {
|
|
134
|
+
type: "string",
|
|
135
|
+
enum: [
|
|
136
|
+
"all",
|
|
137
|
+
"dedicated_instance",
|
|
138
|
+
"secure_pod",
|
|
139
|
+
"community_pod",
|
|
140
|
+
"cloud_vm",
|
|
141
|
+
"bare_metal",
|
|
142
|
+
"decentralized_lease",
|
|
143
|
+
"interruptible_spot",
|
|
144
|
+
],
|
|
145
|
+
default: "all",
|
|
146
|
+
description: "Optional product category filter (defaults to all).",
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
name: "latens_calculate_compute_cost",
|
|
153
|
+
description:
|
|
154
|
+
"Deterministic GPU rental total cost calculation combining hourly compute, persistent storage amortization, and outbound network egress.",
|
|
155
|
+
annotations: {
|
|
156
|
+
readOnlyHint: true,
|
|
157
|
+
destructiveHint: false,
|
|
158
|
+
openWorldHint: true,
|
|
159
|
+
},
|
|
160
|
+
inputSchema: {
|
|
161
|
+
type: "object",
|
|
162
|
+
required: ["duration_hours"],
|
|
163
|
+
properties: {
|
|
164
|
+
offer_id: { type: "string", description: "Normalized offer ID from latens_list_gpu_offers" },
|
|
165
|
+
gpu_model: { type: "string" },
|
|
166
|
+
hourly_price_per_gpu_usd: { type: "number", minimum: 0 },
|
|
167
|
+
hourly_rate_usd: { type: "number", minimum: 0, description: "Alias for hourly_price_per_gpu_usd" },
|
|
168
|
+
duration_hours: { type: "number", exclusiveMinimum: 0 },
|
|
169
|
+
gpu_count: { type: "integer", minimum: 1, default: 1 },
|
|
170
|
+
instance_count: { type: "integer", minimum: 1, description: "Alias for gpu_count" },
|
|
171
|
+
storage_gb: { type: "number", minimum: 0, default: 0 },
|
|
172
|
+
storage_cost_per_gb_month_usd: { type: "number", minimum: 0 },
|
|
173
|
+
egress_gb: { type: "number", minimum: 0, default: 0 },
|
|
174
|
+
egress_cost_per_gb_usd: { type: "number", minimum: 0 },
|
|
175
|
+
additional_flat_fees_usd: { type: "number", minimum: 0, default: 0 },
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
name: "latens_estimate_llm_vram",
|
|
181
|
+
description:
|
|
182
|
+
"Deterministic local LLM VRAM and multi-GPU hardware sizing estimator for inference, LoRA fine-tuning, and full fine-tuning across fp32, fp16, bf16, fp8, int8, q8_0, q4_k_m, and int4 precisions.",
|
|
183
|
+
annotations: {
|
|
184
|
+
readOnlyHint: true,
|
|
185
|
+
destructiveHint: false,
|
|
186
|
+
openWorldHint: false,
|
|
187
|
+
},
|
|
188
|
+
inputSchema: {
|
|
189
|
+
type: "object",
|
|
190
|
+
required: ["parameter_count_billions"],
|
|
191
|
+
properties: {
|
|
192
|
+
parameter_count_billions: {
|
|
193
|
+
type: "number",
|
|
194
|
+
exclusiveMinimum: 0,
|
|
195
|
+
description: "Model parameter count in billions (e.g. 8, 14, 32, 70, 405)",
|
|
196
|
+
},
|
|
197
|
+
quantization: {
|
|
198
|
+
type: "string",
|
|
199
|
+
enum: ["fp32", "fp16", "bf16", "fp8", "int8", "q8_0", "q4_k_m", "int4"],
|
|
200
|
+
default: "bf16",
|
|
201
|
+
},
|
|
202
|
+
mode: {
|
|
203
|
+
type: "string",
|
|
204
|
+
enum: ["inference", "lora_finetune", "full_finetune"],
|
|
205
|
+
default: "inference",
|
|
206
|
+
},
|
|
207
|
+
context_length: { type: "integer", minimum: 512, default: 8192 },
|
|
208
|
+
batch_size: { type: "integer", minimum: 1, default: 1 },
|
|
209
|
+
kv_cache_quantization: {
|
|
210
|
+
type: "string",
|
|
211
|
+
enum: ["fp16", "fp8", "int8"],
|
|
212
|
+
default: "fp16",
|
|
213
|
+
},
|
|
214
|
+
headroom_pct: { type: "number", minimum: 5, maximum: 50, default: 15 },
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
name: "latens_compare_offer_tco",
|
|
220
|
+
description:
|
|
221
|
+
"Fetch live normalized GPU offers and rank them deterministically by total cost of ownership (compute + persistent storage + outbound egress) with optional $LTNS OTC Desk fee schedule.",
|
|
222
|
+
annotations: {
|
|
223
|
+
readOnlyHint: true,
|
|
224
|
+
destructiveHint: false,
|
|
225
|
+
openWorldHint: true,
|
|
226
|
+
},
|
|
227
|
+
inputSchema: {
|
|
228
|
+
type: "object",
|
|
229
|
+
required: ["duration_hours"],
|
|
230
|
+
properties: {
|
|
231
|
+
model: { type: "string", description: "Optional canonical GPU model filter" },
|
|
232
|
+
min_total_vram_gb: { type: "number", minimum: 0 },
|
|
233
|
+
duration_hours: { type: "number", exclusiveMinimum: 0 },
|
|
234
|
+
storage_gb: { type: "number", minimum: 0, default: 0 },
|
|
235
|
+
egress_gb: { type: "number", minimum: 0, default: 0 },
|
|
236
|
+
confirmed_only: { type: "boolean", default: true },
|
|
237
|
+
holder_tier: {
|
|
238
|
+
type: "string",
|
|
239
|
+
enum: ["open", "verified", "priority", "institutional"],
|
|
240
|
+
default: "open",
|
|
241
|
+
},
|
|
242
|
+
settlement_asset: {
|
|
243
|
+
type: "string",
|
|
244
|
+
enum: ["USDG", "LTNS", "ETH", "USD"],
|
|
245
|
+
default: "USDG",
|
|
246
|
+
},
|
|
247
|
+
limit: { type: "integer", minimum: 1, maximum: 50, default: 10 },
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
name: "latens_get_provider_sync_status",
|
|
253
|
+
description:
|
|
254
|
+
"Read-only operational status, synchronization freshness across all 8 GPU compute networks, inventory counts (totalOffers vs filteredOffers), and $LTNS caller tier metadata.",
|
|
255
|
+
annotations: {
|
|
256
|
+
readOnlyHint: true,
|
|
257
|
+
destructiveHint: false,
|
|
258
|
+
openWorldHint: true,
|
|
259
|
+
},
|
|
260
|
+
inputSchema: {
|
|
261
|
+
type: "object",
|
|
262
|
+
properties: {},
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
name: "latens_get_token_standing",
|
|
267
|
+
description:
|
|
268
|
+
"Verify live on-chain $LTNS balance on Robinhood Chain (4663) for any EVM wallet and inspect its active holder tier, OTC Desk fee bps, AI Broker quota, and Agent API RPM.",
|
|
269
|
+
annotations: {
|
|
270
|
+
readOnlyHint: true,
|
|
271
|
+
destructiveHint: false,
|
|
272
|
+
openWorldHint: true,
|
|
273
|
+
},
|
|
274
|
+
inputSchema: {
|
|
275
|
+
type: "object",
|
|
276
|
+
properties: {
|
|
277
|
+
address: {
|
|
278
|
+
type: "string",
|
|
279
|
+
description: "42-character EVM address (0x...) on Robinhood Chain. Defaults to LATENS_WALLET if set.",
|
|
280
|
+
},
|
|
281
|
+
refresh: {
|
|
282
|
+
type: "boolean",
|
|
283
|
+
default: false,
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
name: "latens_x402_request_quote",
|
|
290
|
+
description:
|
|
291
|
+
"Request an HTTP 402 (x402) payment-gated GPU execution quote on Robinhood Chain (4663) in $LTNS, USDG, or native ETH with hard maxSpendUsd ceiling protection. Does not move funds on its own.",
|
|
292
|
+
annotations: {
|
|
293
|
+
readOnlyHint: false,
|
|
294
|
+
destructiveHint: false,
|
|
295
|
+
openWorldHint: true,
|
|
296
|
+
},
|
|
297
|
+
inputSchema: {
|
|
298
|
+
type: "object",
|
|
299
|
+
required: ["ssh_public_key"],
|
|
300
|
+
properties: {
|
|
301
|
+
wallet_address: {
|
|
302
|
+
type: "string",
|
|
303
|
+
description: "Agent EVM wallet address (0x...) on Robinhood Chain 4663.",
|
|
304
|
+
},
|
|
305
|
+
offer_id: { type: "string", description: "Optional Vast.ai offer ID (e.g. vast:12345)." },
|
|
306
|
+
gpu_model: { type: "string", description: "Desired GPU model if offer_id is omitted (e.g. RTX 4090, A100, H100)." },
|
|
307
|
+
gpu_count: { type: "integer", minimum: 1, default: 1 },
|
|
308
|
+
duration_hours: { type: "integer", minimum: 1, maximum: 168, default: 2 },
|
|
309
|
+
disk_gb: { type: "integer", minimum: 20, maximum: 500, default: 80 },
|
|
310
|
+
payment_asset: { type: "string", enum: ["LTNS", "USDG", "ETH"], default: "LTNS" },
|
|
311
|
+
ssh_public_key: { type: "string", description: "OpenSSH Ed25519 or RSA public key." },
|
|
312
|
+
max_spend_usd: { type: "number", default: 25 },
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
{
|
|
317
|
+
name: "latens_x402_settle_execution",
|
|
318
|
+
description:
|
|
319
|
+
"Submit a confirmed Robinhood Chain (4663) transaction hash via X-Payment header to verify settlement and provision the dedicated SSH GPU container.",
|
|
320
|
+
annotations: {
|
|
321
|
+
readOnlyHint: false,
|
|
322
|
+
destructiveHint: false,
|
|
323
|
+
openWorldHint: true,
|
|
324
|
+
},
|
|
325
|
+
inputSchema: {
|
|
326
|
+
type: "object",
|
|
327
|
+
required: ["quote_id", "tx_hash"],
|
|
328
|
+
properties: {
|
|
329
|
+
quote_id: { type: "string" },
|
|
330
|
+
tx_hash: { type: "string" },
|
|
331
|
+
wallet_address: { type: "string" },
|
|
332
|
+
},
|
|
333
|
+
},
|
|
334
|
+
},
|
|
335
|
+
{
|
|
336
|
+
name: "latens_x402_get_rental_status",
|
|
337
|
+
description:
|
|
338
|
+
"Poll live SSH host, port, and container status for an x402-provisioned GPU lease using its Bearer rentalAccessToken.",
|
|
339
|
+
annotations: {
|
|
340
|
+
readOnlyHint: true,
|
|
341
|
+
destructiveHint: false,
|
|
342
|
+
openWorldHint: true,
|
|
343
|
+
},
|
|
344
|
+
inputSchema: {
|
|
345
|
+
type: "object",
|
|
346
|
+
required: ["rental_id", "rental_access_token"],
|
|
347
|
+
properties: {
|
|
348
|
+
rental_id: { type: "string" },
|
|
349
|
+
rental_access_token: { type: "string" },
|
|
350
|
+
},
|
|
351
|
+
},
|
|
352
|
+
},
|
|
353
|
+
];
|
|
354
|
+
|
|
355
|
+
function buildOffersParams(args, overrides = {}) {
|
|
356
|
+
const params = new URLSearchParams();
|
|
357
|
+
const resolvedModel = args.model ?? args.gpu_model;
|
|
358
|
+
const resolvedMaxPrice = args.max_price_per_gpu_usd ?? args.max_hourly_usd;
|
|
359
|
+
if (resolvedModel) params.set("model", String(resolvedModel));
|
|
360
|
+
if (args.provider) params.set("provider", String(args.provider));
|
|
361
|
+
if (args.category) params.set("category", String(args.category));
|
|
362
|
+
if (args.min_vram_gb !== undefined) params.set("min_vram_gb", String(args.min_vram_gb));
|
|
363
|
+
if (resolvedMaxPrice !== undefined) params.set("max_price_per_gpu_usd", String(resolvedMaxPrice));
|
|
364
|
+
if (args.gpu_count !== undefined) params.set("gpu_count", String(args.gpu_count));
|
|
365
|
+
if (args.region) params.set("region", String(args.region));
|
|
366
|
+
if (args.available_only !== undefined) {
|
|
367
|
+
params.set("available_only", args.available_only ? "true" : "false");
|
|
368
|
+
}
|
|
369
|
+
if (args.sort) params.set("sort", String(args.sort));
|
|
370
|
+
|
|
371
|
+
const offset = overrides.offset !== undefined ? overrides.offset : args.offset;
|
|
372
|
+
const limit = overrides.limit !== undefined ? overrides.limit : args.limit;
|
|
373
|
+
if (offset !== undefined) params.set("offset", String(offset));
|
|
374
|
+
if (limit !== undefined) params.set("limit", String(limit));
|
|
375
|
+
return params;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export async function executeTool(name, args = {}, fetchImpl = fetch) {
|
|
379
|
+
if (name === "latens_estimate_llm_vram") {
|
|
380
|
+
return estimateLlmVramRequirements(args);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (name === "latens_compare_offer_tco") {
|
|
384
|
+
const params = new URLSearchParams();
|
|
385
|
+
if (args.model) params.set("model", String(args.model));
|
|
386
|
+
params.set("available_only", "true");
|
|
387
|
+
params.set("limit", "100");
|
|
388
|
+
const res = await fetchImpl(`${BASE_URL}/api/v1/agent/offers?${params.toString()}`, {
|
|
389
|
+
headers: buildHeaders(),
|
|
390
|
+
});
|
|
391
|
+
const raw = await res.json();
|
|
392
|
+
const offers = raw.data ?? raw.offers ?? [];
|
|
393
|
+
return rankOffersByTco(offers, args);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (name === "latens_list_gpu_offers") {
|
|
397
|
+
if (args.all === true) {
|
|
398
|
+
const cap = MAX_ALL_OFFERS_CAP;
|
|
399
|
+
const startOffset =
|
|
400
|
+
typeof args.offset === "number" && args.offset > 0
|
|
401
|
+
? Math.floor(args.offset)
|
|
402
|
+
: 0;
|
|
403
|
+
const collected = [];
|
|
404
|
+
let currentOffset = startOffset;
|
|
405
|
+
let pagesFetched = 0;
|
|
406
|
+
let lastMeta = null;
|
|
407
|
+
|
|
408
|
+
while (collected.length < cap) {
|
|
409
|
+
const remaining = cap - collected.length;
|
|
410
|
+
const pageLimit = Math.min(100, remaining);
|
|
411
|
+
const params = buildOffersParams(args, {
|
|
412
|
+
offset: currentOffset,
|
|
413
|
+
limit: pageLimit,
|
|
414
|
+
});
|
|
415
|
+
const res = await fetchImpl(`${BASE_URL}/api/v1/agent/offers?${params.toString()}`, {
|
|
416
|
+
headers: buildHeaders(),
|
|
417
|
+
});
|
|
418
|
+
const page = await res.json();
|
|
419
|
+
if (!res.ok) {
|
|
420
|
+
return page;
|
|
421
|
+
}
|
|
422
|
+
pagesFetched += 1;
|
|
423
|
+
lastMeta = page;
|
|
424
|
+
const items = page.data ?? page.offers ?? [];
|
|
425
|
+
collected.push(...items);
|
|
426
|
+
|
|
427
|
+
if (
|
|
428
|
+
page.next_offset === null ||
|
|
429
|
+
page.next_offset === undefined ||
|
|
430
|
+
items.length === 0
|
|
431
|
+
) {
|
|
432
|
+
currentOffset = null;
|
|
433
|
+
break;
|
|
434
|
+
}
|
|
435
|
+
currentOffset = page.next_offset;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const total = lastMeta?.total ?? collected.length;
|
|
439
|
+
const truncated = currentOffset !== null && startOffset + collected.length < total;
|
|
440
|
+
return {
|
|
441
|
+
object: lastMeta?.object ?? "gpu_offer_list",
|
|
442
|
+
read_only: true,
|
|
443
|
+
execution_permitted: false,
|
|
444
|
+
generated_at: lastMeta?.generated_at ?? new Date().toISOString(),
|
|
445
|
+
last_synced_at: lastMeta?.last_synced_at ?? null,
|
|
446
|
+
has_stale_data: Boolean(lastMeta?.has_stale_data),
|
|
447
|
+
sort: lastMeta?.sort ?? args.sort ?? "price_asc",
|
|
448
|
+
offset: startOffset,
|
|
449
|
+
count: collected.length,
|
|
450
|
+
total,
|
|
451
|
+
next_offset: truncated ? currentOffset : null,
|
|
452
|
+
pages_fetched: pagesFetched,
|
|
453
|
+
truncated,
|
|
454
|
+
data: collected,
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const params = buildOffersParams(args);
|
|
459
|
+
const qs = params.toString();
|
|
460
|
+
const res = await fetchImpl(`${BASE_URL}/api/v1/agent/offers${qs ? `?${qs}` : ""}`, {
|
|
461
|
+
headers: buildHeaders(),
|
|
462
|
+
});
|
|
463
|
+
return res.json();
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if (name === "latens_list_gpu_models") {
|
|
467
|
+
const res = await fetchImpl(`${BASE_URL}/api/v1/agent/models`, {
|
|
468
|
+
headers: buildHeaders(),
|
|
469
|
+
});
|
|
470
|
+
return res.json();
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (name === "latens_get_price_history") {
|
|
474
|
+
const model = String(args.model || "").trim();
|
|
475
|
+
if (!model) {
|
|
476
|
+
throw new Error("model is required for latens_get_price_history");
|
|
477
|
+
}
|
|
478
|
+
const params = new URLSearchParams();
|
|
479
|
+
params.set("model", model);
|
|
480
|
+
if (args.window) params.set("window", String(args.window));
|
|
481
|
+
if (args.interval) params.set("interval", String(args.interval));
|
|
482
|
+
if (args.category) params.set("category", String(args.category));
|
|
483
|
+
|
|
484
|
+
const res = await fetchImpl(
|
|
485
|
+
`${BASE_URL}/api/v1/agent/models/history?${params.toString()}`,
|
|
486
|
+
{
|
|
487
|
+
headers: buildHeaders(),
|
|
488
|
+
}
|
|
489
|
+
);
|
|
490
|
+
return res.json();
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
if (name === "latens_calculate_compute_cost") {
|
|
494
|
+
const payload = {
|
|
495
|
+
offer_id: args.offer_id,
|
|
496
|
+
gpu_model: args.gpu_model,
|
|
497
|
+
gpu_count: args.gpu_count ?? args.instance_count ?? 1,
|
|
498
|
+
duration_hours: args.duration_hours,
|
|
499
|
+
hourly_price_per_gpu_usd: args.hourly_price_per_gpu_usd ?? args.hourly_rate_usd,
|
|
500
|
+
storage_gb: args.storage_gb ?? 0,
|
|
501
|
+
storage_cost_per_gb_month_usd: args.storage_cost_per_gb_month_usd,
|
|
502
|
+
egress_gb: args.egress_gb ?? 0,
|
|
503
|
+
egress_cost_per_gb_usd: args.egress_cost_per_gb_usd,
|
|
504
|
+
additional_flat_fees_usd: args.additional_flat_fees_usd ?? 0,
|
|
505
|
+
};
|
|
506
|
+
const res = await fetchImpl(`${BASE_URL}/api/v1/agent/calculate`, {
|
|
507
|
+
method: "POST",
|
|
508
|
+
headers: buildHeaders({ "Content-Type": "application/json" }),
|
|
509
|
+
body: JSON.stringify(payload),
|
|
510
|
+
});
|
|
511
|
+
return res.json();
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (name === "latens_get_provider_sync_status") {
|
|
515
|
+
const res = await fetchImpl(`${BASE_URL}/api/v1/agent/status`, {
|
|
516
|
+
headers: buildHeaders(),
|
|
517
|
+
});
|
|
518
|
+
return res.json();
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
if (name === "latens_get_token_standing") {
|
|
522
|
+
const params = new URLSearchParams();
|
|
523
|
+
const target = String(args.address || DEFAULT_WALLET || "").trim();
|
|
524
|
+
if (target) params.set("address", target);
|
|
525
|
+
if (args.refresh) params.set("refresh", "1");
|
|
526
|
+
const qs = params.toString();
|
|
527
|
+
const res = await fetchImpl(`${BASE_URL}/api/token/standing${qs ? `?${qs}` : ""}`, {
|
|
528
|
+
headers: buildHeaders({}, target),
|
|
529
|
+
});
|
|
530
|
+
return res.json();
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
if (name === "latens_x402_request_quote") {
|
|
534
|
+
const wallet = String(args.wallet_address || DEFAULT_WALLET || "").trim();
|
|
535
|
+
const payload = {
|
|
536
|
+
walletAddress: wallet || undefined,
|
|
537
|
+
offerId: args.offer_id,
|
|
538
|
+
gpuModel: args.gpu_model,
|
|
539
|
+
gpuCount: args.gpu_count ?? 1,
|
|
540
|
+
durationHours: args.duration_hours ?? 2,
|
|
541
|
+
diskGb: args.disk_gb ?? 80,
|
|
542
|
+
paymentAsset: args.payment_asset ?? "LTNS",
|
|
543
|
+
sshPublicKey: args.ssh_public_key,
|
|
544
|
+
maxSpendUsd: args.max_spend_usd ?? 25,
|
|
545
|
+
};
|
|
546
|
+
const res = await fetchImpl(`${BASE_URL}/api/v1/agent/execute`, {
|
|
547
|
+
method: "POST",
|
|
548
|
+
headers: buildHeaders({ "Content-Type": "application/json" }, wallet),
|
|
549
|
+
body: JSON.stringify(payload),
|
|
550
|
+
});
|
|
551
|
+
return res.json();
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
if (name === "latens_x402_settle_execution") {
|
|
555
|
+
const wallet = String(args.wallet_address || DEFAULT_WALLET || "").trim();
|
|
556
|
+
const xPayment = Buffer.from(
|
|
557
|
+
JSON.stringify({
|
|
558
|
+
quoteId: args.quote_id,
|
|
559
|
+
txHash: args.tx_hash,
|
|
560
|
+
payer: wallet || undefined,
|
|
561
|
+
}),
|
|
562
|
+
"utf8"
|
|
563
|
+
).toString("base64");
|
|
564
|
+
const res = await fetchImpl(`${BASE_URL}/api/v1/agent/execute`, {
|
|
565
|
+
method: "POST",
|
|
566
|
+
headers: buildHeaders(
|
|
567
|
+
{ "Content-Type": "application/json", "X-Payment": xPayment },
|
|
568
|
+
wallet
|
|
569
|
+
),
|
|
570
|
+
body: JSON.stringify({}),
|
|
571
|
+
});
|
|
572
|
+
return res.json();
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (name === "latens_x402_get_rental_status") {
|
|
576
|
+
const token = String(args.rental_access_token || "").replace(/^Bearer\s+/i, "").trim();
|
|
577
|
+
const res = await fetchImpl(
|
|
578
|
+
`${BASE_URL}/api/v1/agent/rentals/${encodeURIComponent(String(args.rental_id))}`,
|
|
579
|
+
{
|
|
580
|
+
headers: buildHeaders({ Authorization: `Bearer ${token}` }),
|
|
581
|
+
}
|
|
582
|
+
);
|
|
583
|
+
return res.json();
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
function sendJsonRpc(message) {
|
|
590
|
+
process.stdout.write(`${JSON.stringify(message)}\n`);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
export function startStdioServer() {
|
|
594
|
+
const rl = readline.createInterface({
|
|
595
|
+
input: process.stdin,
|
|
596
|
+
terminal: false,
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
rl.on("line", async (line) => {
|
|
600
|
+
const trimmed = line.trim();
|
|
601
|
+
if (!trimmed) return;
|
|
602
|
+
|
|
603
|
+
let req;
|
|
604
|
+
try {
|
|
605
|
+
req = JSON.parse(trimmed);
|
|
606
|
+
} catch {
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
if (req.method === "initialize") {
|
|
611
|
+
sendJsonRpc({
|
|
612
|
+
jsonrpc: "2.0",
|
|
613
|
+
id: req.id,
|
|
614
|
+
result: {
|
|
615
|
+
protocolVersion: "2024-11-05",
|
|
616
|
+
capabilities: { tools: {} },
|
|
617
|
+
serverInfo: { name: "latens-mcp", version: "1.4.0" },
|
|
618
|
+
},
|
|
619
|
+
});
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
if (req.method === "tools/list") {
|
|
624
|
+
sendJsonRpc({
|
|
625
|
+
jsonrpc: "2.0",
|
|
626
|
+
id: req.id,
|
|
627
|
+
result: { tools: TOOLS },
|
|
628
|
+
});
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
if (req.method === "tools/call") {
|
|
633
|
+
try {
|
|
634
|
+
const data = await executeTool(req.params?.name, req.params?.arguments);
|
|
635
|
+
sendJsonRpc({
|
|
636
|
+
jsonrpc: "2.0",
|
|
637
|
+
id: req.id,
|
|
638
|
+
result: {
|
|
639
|
+
content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
|
|
640
|
+
},
|
|
641
|
+
});
|
|
642
|
+
} catch (err) {
|
|
643
|
+
sendJsonRpc({
|
|
644
|
+
jsonrpc: "2.0",
|
|
645
|
+
id: req.id,
|
|
646
|
+
result: {
|
|
647
|
+
isError: true,
|
|
648
|
+
content: [
|
|
649
|
+
{
|
|
650
|
+
type: "text",
|
|
651
|
+
text: err instanceof Error ? err.message : "Tool execution failed",
|
|
652
|
+
},
|
|
653
|
+
],
|
|
654
|
+
},
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
if (req.id !== undefined) {
|
|
661
|
+
sendJsonRpc({
|
|
662
|
+
jsonrpc: "2.0",
|
|
663
|
+
id: req.id,
|
|
664
|
+
result: {},
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
function isDirectCliExecution() {
|
|
671
|
+
if (!process.argv[1]) return false;
|
|
672
|
+
try {
|
|
673
|
+
const currentFile = fs.realpathSync(fileURLToPath(import.meta.url));
|
|
674
|
+
const entryFile = fs.realpathSync(process.argv[1]);
|
|
675
|
+
return currentFile === entryFile;
|
|
676
|
+
} catch {
|
|
677
|
+
return process.argv[1].endsWith("server.mjs");
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
if (isDirectCliExecution()) {
|
|
682
|
+
startStdioServer();
|
|
683
|
+
}
|
package/src/sizing.mjs
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
const HOURS_PER_MONTH = 730;
|
|
2
|
+
|
|
3
|
+
const BYTES_PER_PARAM = {
|
|
4
|
+
fp32: 4.0,
|
|
5
|
+
fp16: 2.0,
|
|
6
|
+
bf16: 2.0,
|
|
7
|
+
fp8: 1.0,
|
|
8
|
+
int8: 1.0,
|
|
9
|
+
q8_0: 1.06,
|
|
10
|
+
q4_k_m: 0.6,
|
|
11
|
+
int4: 0.55,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const KV_BYTES_PER_ELEMENT = {
|
|
15
|
+
fp16: 2.0,
|
|
16
|
+
fp8: 1.0,
|
|
17
|
+
int8: 1.0,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const TIER_DESK_FEE_BPS = {
|
|
21
|
+
open: 100,
|
|
22
|
+
verified: 75,
|
|
23
|
+
priority: 50,
|
|
24
|
+
institutional: 0,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const HARDWARE_TEMPLATES = [
|
|
28
|
+
{ gpuModel: "RTX 3090", vramPerGpuGb: 24, validCounts: [1, 2, 4] },
|
|
29
|
+
{ gpuModel: "RTX 4090", vramPerGpuGb: 24, validCounts: [1, 2, 4, 8] },
|
|
30
|
+
{ gpuModel: "L40S", vramPerGpuGb: 48, validCounts: [1, 2, 4, 8] },
|
|
31
|
+
{ gpuModel: "A100 SXM", vramPerGpuGb: 80, validCounts: [1, 2, 4, 8] },
|
|
32
|
+
{ gpuModel: "H100 SXM", vramPerGpuGb: 80, validCounts: [1, 2, 4, 8] },
|
|
33
|
+
{ gpuModel: "H200", vramPerGpuGb: 141, validCounts: [1, 2, 4, 8] },
|
|
34
|
+
{ gpuModel: "B200", vramPerGpuGb: 192, validCounts: [1, 2, 4, 8] },
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
export function roundUsd(value, decimals = 4) {
|
|
38
|
+
const factor = 10 ** decimals;
|
|
39
|
+
return Math.round((value + Number.EPSILON) * factor) / factor;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function estimateLlmVramRequirements(input) {
|
|
43
|
+
const paramsB = Number(input?.parameter_count_billions ?? input?.parameterCountBillions);
|
|
44
|
+
if (!Number.isFinite(paramsB) || paramsB <= 0) {
|
|
45
|
+
throw new RangeError("parameter_count_billions must be greater than zero.");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const quantization = String(input?.quantization || "bf16").toLowerCase();
|
|
49
|
+
const bytesPerParam = BYTES_PER_PARAM[quantization];
|
|
50
|
+
if (!bytesPerParam) {
|
|
51
|
+
throw new TypeError(`Unsupported quantization: ${quantization}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const mode = String(input?.mode || "inference").toLowerCase();
|
|
55
|
+
const contextLength = Math.max(512, Math.floor(Number(input?.context_length ?? input?.contextLength ?? 8192)));
|
|
56
|
+
const batchSize = Math.max(1, Math.floor(Number(input?.batch_size ?? input?.batchSize ?? 1)));
|
|
57
|
+
const kvQuant = String(input?.kv_cache_quantization ?? input?.kvCacheQuantization ?? "fp16").toLowerCase();
|
|
58
|
+
const kvBytes = KV_BYTES_PER_ELEMENT[kvQuant] ?? 2.0;
|
|
59
|
+
const headroomPct = Math.min(50, Math.max(5, Number(input?.headroom_pct ?? input?.headroomPct ?? 15)));
|
|
60
|
+
|
|
61
|
+
const weightsGb = (paramsB * 1e9 * bytesPerParam) / 1024 ** 3;
|
|
62
|
+
const estimatedLayers = Math.max(24, Math.round(14 * Math.pow(paramsB, 0.42)));
|
|
63
|
+
const kvHiddenDim = Math.max(512, Math.round(320 * Math.pow(paramsB, 0.34)));
|
|
64
|
+
const kvCacheGb = (2 * estimatedLayers * kvHiddenDim * contextLength * batchSize * kvBytes) / 1024 ** 3;
|
|
65
|
+
|
|
66
|
+
let trainingOverheadGb = 0;
|
|
67
|
+
if (mode === "lora_finetune") {
|
|
68
|
+
const adapterStatesGb = (paramsB * 1e9 * 0.025 * 16) / 1024 ** 3;
|
|
69
|
+
const activationsGb = paramsB * 0.14 * Math.sqrt(contextLength / 4096) * Math.sqrt(batchSize);
|
|
70
|
+
trainingOverheadGb = adapterStatesGb + activationsGb;
|
|
71
|
+
} else if (mode === "full_finetune") {
|
|
72
|
+
const optimizerAndGradsGb = (paramsB * 1e9 * 14) / 1024 ** 3;
|
|
73
|
+
const activationsGb = paramsB * 0.25 * Math.sqrt(contextLength / 4096) * Math.sqrt(batchSize);
|
|
74
|
+
trainingOverheadGb = optimizerAndGradsGb + activationsGb;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const cudaContextGb = 1.5;
|
|
78
|
+
const rawTotalGb = weightsGb + kvCacheGb + trainingOverheadGb + cudaContextGb;
|
|
79
|
+
const runtimeOverheadGb = cudaContextGb + rawTotalGb * (headroomPct / 100);
|
|
80
|
+
const recommendedVramGb = roundUsd(weightsGb + kvCacheGb + trainingOverheadGb + runtimeOverheadGb, 2);
|
|
81
|
+
|
|
82
|
+
const recommendedConfigs = [];
|
|
83
|
+
for (const tpl of HARDWARE_TEMPLATES) {
|
|
84
|
+
const gpuCount = tpl.validCounts.find((c) => c * tpl.vramPerGpuGb >= recommendedVramGb);
|
|
85
|
+
if (!gpuCount) continue;
|
|
86
|
+
const totalVramGb = gpuCount * tpl.vramPerGpuGb;
|
|
87
|
+
recommendedConfigs.push({
|
|
88
|
+
gpu_model: tpl.gpuModel,
|
|
89
|
+
gpu_count: gpuCount,
|
|
90
|
+
vram_per_gpu_gb: tpl.vramPerGpuGb,
|
|
91
|
+
total_vram_gb: totalVramGb,
|
|
92
|
+
estimated_utilization_pct: roundUsd((recommendedVramGb / totalVramGb) * 100, 1),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
recommendedConfigs.sort((a, b) =>
|
|
97
|
+
a.gpu_count !== b.gpu_count ? a.gpu_count - b.gpu_count : a.total_vram_gb - b.total_vram_gb
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
deterministic: true,
|
|
102
|
+
parameter_count_billions: paramsB,
|
|
103
|
+
quantization,
|
|
104
|
+
mode,
|
|
105
|
+
context_length: contextLength,
|
|
106
|
+
batch_size: batchSize,
|
|
107
|
+
kv_cache_quantization: kvQuant,
|
|
108
|
+
weights_gb: roundUsd(weightsGb, 2),
|
|
109
|
+
kv_cache_gb: roundUsd(kvCacheGb, 2),
|
|
110
|
+
training_overhead_gb: roundUsd(trainingOverheadGb, 2),
|
|
111
|
+
runtime_overhead_gb: roundUsd(runtimeOverheadGb, 2),
|
|
112
|
+
minimum_vram_gb: roundUsd(rawTotalGb, 2),
|
|
113
|
+
recommended_vram_gb: recommendedVramGb,
|
|
114
|
+
recommended_configs: recommendedConfigs,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function rankOffersByTco(offers, options = {}) {
|
|
119
|
+
const durationHours = Number(options.duration_hours ?? options.durationHours);
|
|
120
|
+
if (!Number.isFinite(durationHours) || durationHours <= 0) {
|
|
121
|
+
throw new RangeError("duration_hours must be greater than zero.");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const minTotalVramGb = Math.max(0, Number(options.min_total_vram_gb ?? options.minTotalVramGb ?? 0));
|
|
125
|
+
const storageGb = Math.max(0, Number(options.storage_gb ?? options.storageGb ?? 0));
|
|
126
|
+
const egressGb = Math.max(0, Number(options.egress_gb ?? options.egressGb ?? 0));
|
|
127
|
+
const confirmedOnly = Boolean(options.confirmed_only ?? options.confirmedOnly ?? false);
|
|
128
|
+
const availableOnly = options.available_only !== undefined ? Boolean(options.available_only) : true;
|
|
129
|
+
const holderTier = String(options.holder_tier ?? options.holderTier ?? "open").toLowerCase();
|
|
130
|
+
const settlementAsset = String(options.settlement_asset ?? options.settlementAsset ?? "USDG").toUpperCase();
|
|
131
|
+
const limit = Math.min(50, Math.max(1, Math.floor(Number(options.limit ?? 10))));
|
|
132
|
+
|
|
133
|
+
const baseFeeBps = TIER_DESK_FEE_BPS[holderTier] ?? 100;
|
|
134
|
+
const effectiveDeskFeeBps = Math.max(0, baseFeeBps - (settlementAsset === "LTNS" ? 15 : 0));
|
|
135
|
+
|
|
136
|
+
const eligible = (Array.isArray(offers) ? offers : []).filter((o) => {
|
|
137
|
+
if (!o || typeof o !== "object") return false;
|
|
138
|
+
if (availableOnly && o.availability_status === "unavailable") return false;
|
|
139
|
+
if (confirmedOnly && o.price_precision !== "confirmed_offer") return false;
|
|
140
|
+
if (Number(o.total_vram_gb || 0) < minTotalVramGb) return false;
|
|
141
|
+
return Number(o.hourly_price_usd || 0) > 0;
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const ranked = eligible.map((o) => {
|
|
145
|
+
const computeUsd = Number(o.hourly_price_usd) * durationHours;
|
|
146
|
+
const storageRate = Number(o.storage_cost_per_gb_month_usd ?? 0.1);
|
|
147
|
+
const storageUsd = storageGb * (storageRate / HOURS_PER_MONTH) * durationHours;
|
|
148
|
+
const egressRate = Number(o.egress_cost_per_gb_usd ?? 0);
|
|
149
|
+
const egressUsd = egressGb * egressRate;
|
|
150
|
+
const totalUsd = computeUsd + storageUsd + egressUsd;
|
|
151
|
+
const deskFeeUsd = totalUsd * (effectiveDeskFeeBps / 10_000);
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
offer_id: o.id,
|
|
155
|
+
provider: o.provider,
|
|
156
|
+
gpu_model: o.gpu_model,
|
|
157
|
+
gpu_count: o.gpu_count,
|
|
158
|
+
total_vram_gb: o.total_vram_gb,
|
|
159
|
+
region: o.region,
|
|
160
|
+
price_precision: o.price_precision,
|
|
161
|
+
product_category: o.product_category,
|
|
162
|
+
hourly_price_usd: roundUsd(Number(o.hourly_price_usd), 4),
|
|
163
|
+
compute_subtotal_usd: roundUsd(computeUsd, 4),
|
|
164
|
+
storage_subtotal_usd: roundUsd(storageUsd, 4),
|
|
165
|
+
egress_subtotal_usd: roundUsd(egressUsd, 4),
|
|
166
|
+
total_estimated_usd: roundUsd(totalUsd, 4),
|
|
167
|
+
effective_hourly_usd: roundUsd(totalUsd / durationHours, 4),
|
|
168
|
+
desk_fee_bps: effectiveDeskFeeBps,
|
|
169
|
+
estimated_desk_fee_usd: roundUsd(deskFeeUsd, 4),
|
|
170
|
+
total_with_desk_fee_usd: roundUsd(totalUsd + deskFeeUsd, 4),
|
|
171
|
+
source_url: o.source_url,
|
|
172
|
+
};
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
ranked.sort((a, b) => a.total_estimated_usd - b.total_estimated_usd);
|
|
176
|
+
|
|
177
|
+
const cheapest = ranked[0]?.total_estimated_usd ?? 0;
|
|
178
|
+
const mostExpensive = ranked[ranked.length - 1]?.total_estimated_usd ?? 0;
|
|
179
|
+
const spreadMultiplier =
|
|
180
|
+
cheapest > 0 && mostExpensive > 0 ? roundUsd(mostExpensive / cheapest, 2) : 1;
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
deterministic: true,
|
|
184
|
+
duration_hours: durationHours,
|
|
185
|
+
evaluated_count: Array.isArray(offers) ? offers.length : 0,
|
|
186
|
+
eligible_count: ranked.length,
|
|
187
|
+
holder_tier: holderTier,
|
|
188
|
+
settlement_asset: settlementAsset,
|
|
189
|
+
effective_desk_fee_bps: effectiveDeskFeeBps,
|
|
190
|
+
cheapest_total_usd: cheapest,
|
|
191
|
+
most_expensive_total_usd: mostExpensive,
|
|
192
|
+
spread_multiplier: spreadMultiplier,
|
|
193
|
+
ranked: ranked.slice(0, limit).map((item) => ({
|
|
194
|
+
...item,
|
|
195
|
+
savings_vs_most_expensive_usd: roundUsd(Math.max(0, mostExpensive - item.total_estimated_usd), 4),
|
|
196
|
+
})),
|
|
197
|
+
};
|
|
198
|
+
}
|