@managebetcodes/mcp 0.0.0-stage → 1.0.1
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 +58 -2
- package/dist/catalog.d.ts +31 -0
- package/dist/catalog.js +746 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +42 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -0
- package/dist/server.d.ts +35 -0
- package/dist/server.js +634 -0
- package/package.json +51 -4
package/dist/server.js
ADDED
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { MCP_PROMPTS, MCP_RESOURCES, MCP_TOOLS, } from "./catalog.js";
|
|
3
|
+
export const VERSION = "1.0.1";
|
|
4
|
+
export const BASE_URL = "https://api.managebetcodes.app/v1";
|
|
5
|
+
export const SERVER_NAME = "managebetcodes-mcp";
|
|
6
|
+
const SUPPORTED_PROTOCOLS = new Set([
|
|
7
|
+
"2025-11-25",
|
|
8
|
+
"2025-06-18",
|
|
9
|
+
"2025-03-26",
|
|
10
|
+
"2024-11-05",
|
|
11
|
+
]);
|
|
12
|
+
const DEFAULT_PROTOCOL = "2025-06-18";
|
|
13
|
+
const RETRY_STATUSES = new Set([429, 502, 503, 504]);
|
|
14
|
+
const NON_RETRYABLE_CODES = new Set([
|
|
15
|
+
"api_quota_exhausted",
|
|
16
|
+
"plan_quota_exhausted",
|
|
17
|
+
"search_quota_exhausted",
|
|
18
|
+
]);
|
|
19
|
+
const MAX_BACKOFF_MS = 30_000;
|
|
20
|
+
function sleep(ms) {
|
|
21
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
22
|
+
}
|
|
23
|
+
function retryAfterSeconds(value) {
|
|
24
|
+
if (!value)
|
|
25
|
+
return undefined;
|
|
26
|
+
const cap = MAX_BACKOFF_MS / 1000;
|
|
27
|
+
const seconds = Number(value);
|
|
28
|
+
if (Number.isFinite(seconds)) {
|
|
29
|
+
return Math.min(Math.max(0, seconds), cap);
|
|
30
|
+
}
|
|
31
|
+
const when = Date.parse(value);
|
|
32
|
+
if (Number.isNaN(when))
|
|
33
|
+
return undefined;
|
|
34
|
+
return Math.min(Math.max(0, (when - Date.now()) / 1000), cap);
|
|
35
|
+
}
|
|
36
|
+
function compact(value) {
|
|
37
|
+
return Object.fromEntries(Object.entries(value).filter(([, item]) => item !== undefined));
|
|
38
|
+
}
|
|
39
|
+
function okToolResult(summary, structured, imageB64) {
|
|
40
|
+
const content = [];
|
|
41
|
+
if (imageB64) {
|
|
42
|
+
const raw = imageB64.includes(",") ? imageB64.split(",")[1] : imageB64;
|
|
43
|
+
content.push({
|
|
44
|
+
type: "image",
|
|
45
|
+
data: raw,
|
|
46
|
+
mimeType: "image/png",
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
content.push({
|
|
50
|
+
type: "text",
|
|
51
|
+
text: `${summary}\n\n\`\`\`json\n${JSON.stringify(structured, null, 2)}\n\`\`\``,
|
|
52
|
+
});
|
|
53
|
+
return {
|
|
54
|
+
isError: false,
|
|
55
|
+
content,
|
|
56
|
+
structuredContent: structured,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function errorToolResult(code, message, details = {}, nextAction, requestId) {
|
|
60
|
+
const envelope = {
|
|
61
|
+
error: compact({
|
|
62
|
+
code,
|
|
63
|
+
message,
|
|
64
|
+
next_action: nextAction,
|
|
65
|
+
request_id: requestId,
|
|
66
|
+
docs: "https://managebetcodes.app/docs/errors",
|
|
67
|
+
details,
|
|
68
|
+
}),
|
|
69
|
+
};
|
|
70
|
+
return {
|
|
71
|
+
isError: true,
|
|
72
|
+
content: [
|
|
73
|
+
{
|
|
74
|
+
type: "text",
|
|
75
|
+
text: `Error (${code}): ${message}\n\n\`\`\`json\n${JSON.stringify(envelope, null, 2)}\n\`\`\``,
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
structuredContent: envelope,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export class McpServer {
|
|
82
|
+
baseUrl;
|
|
83
|
+
apiKey;
|
|
84
|
+
timeoutMs;
|
|
85
|
+
constructor(options = {}) {
|
|
86
|
+
this.apiKey = (options.apiKey ?? process.env["MBC_API_KEY"] ?? "").trim();
|
|
87
|
+
this.baseUrl = (options.baseUrl ?? process.env["MBC_BASE_URL"] ?? BASE_URL).replace(/\/+$/, "");
|
|
88
|
+
this.timeoutMs = options.timeoutMs ?? 65_000;
|
|
89
|
+
}
|
|
90
|
+
async apiRequest(method, path, options = {}) {
|
|
91
|
+
if (options.authRequired !== false && !this.apiKey) {
|
|
92
|
+
const err = new Error("Missing MBC_API_KEY. Create a key at https://managebetcodes.app/dashboard/api and set MBC_API_KEY.");
|
|
93
|
+
err.code = "invalid_key";
|
|
94
|
+
err.status = 401;
|
|
95
|
+
throw err;
|
|
96
|
+
}
|
|
97
|
+
const url = new URL(this.baseUrl + path);
|
|
98
|
+
for (const [k, v] of Object.entries(options.query ?? {})) {
|
|
99
|
+
if (v === undefined || v === null || v === "")
|
|
100
|
+
continue;
|
|
101
|
+
url.searchParams.set(k, String(v));
|
|
102
|
+
}
|
|
103
|
+
const headers = {
|
|
104
|
+
Accept: options.rawBytes ? "image/png" : "application/json",
|
|
105
|
+
"User-Agent": `managebetcodes-mcp/${VERSION}`,
|
|
106
|
+
};
|
|
107
|
+
if (this.apiKey) {
|
|
108
|
+
headers["X-API-Key"] = this.apiKey;
|
|
109
|
+
}
|
|
110
|
+
let bodyStr;
|
|
111
|
+
if (options.body !== undefined) {
|
|
112
|
+
bodyStr = JSON.stringify(options.body);
|
|
113
|
+
headers["Content-Type"] = "application/json";
|
|
114
|
+
}
|
|
115
|
+
if (method === "POST") {
|
|
116
|
+
headers["Idempotency-Key"] = `mcp_${randomUUID()}`;
|
|
117
|
+
}
|
|
118
|
+
for (let attempt = 0;; attempt += 1) {
|
|
119
|
+
const controller = new AbortController();
|
|
120
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
121
|
+
let response;
|
|
122
|
+
try {
|
|
123
|
+
response = await fetch(url, {
|
|
124
|
+
method,
|
|
125
|
+
headers,
|
|
126
|
+
body: bodyStr,
|
|
127
|
+
signal: controller.signal,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
finally {
|
|
131
|
+
clearTimeout(timer);
|
|
132
|
+
}
|
|
133
|
+
if (RETRY_STATUSES.has(response.status) && attempt < 3) {
|
|
134
|
+
let peek = null;
|
|
135
|
+
if (response.status === 429) {
|
|
136
|
+
try {
|
|
137
|
+
peek = await response.clone().json();
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
peek = null;
|
|
141
|
+
}
|
|
142
|
+
const code = peek?.error?.code;
|
|
143
|
+
if (typeof code === "string" && NON_RETRYABLE_CODES.has(code)) {
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const retryAfter = retryAfterSeconds(response.headers.get("retry-after"));
|
|
148
|
+
const delayMs = retryAfter === undefined
|
|
149
|
+
? Math.min(500 * 2 ** attempt, MAX_BACKOFF_MS)
|
|
150
|
+
: retryAfter * 1000;
|
|
151
|
+
await response.body?.cancel();
|
|
152
|
+
await sleep(delayMs);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (response.ok) {
|
|
156
|
+
if (options.rawBytes) {
|
|
157
|
+
const buf = new Uint8Array(await response.arrayBuffer());
|
|
158
|
+
return {
|
|
159
|
+
bytes: buf,
|
|
160
|
+
base64: Buffer.from(buf).toString("base64"),
|
|
161
|
+
creditsCharged: Number(response.headers.get("x-mbc-credits-charged") ?? 2),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
const text = await response.text();
|
|
165
|
+
return text ? JSON.parse(text) : {};
|
|
166
|
+
}
|
|
167
|
+
let parsed = null;
|
|
168
|
+
try {
|
|
169
|
+
parsed = (await response.json());
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
parsed = null;
|
|
173
|
+
}
|
|
174
|
+
const errObj = parsed?.["error"] ?? {};
|
|
175
|
+
const err = new Error(typeof errObj.message === "string"
|
|
176
|
+
? errObj.message
|
|
177
|
+
: `HTTP ${response.status}`);
|
|
178
|
+
err.status = response.status;
|
|
179
|
+
err.code =
|
|
180
|
+
typeof errObj.code === "string"
|
|
181
|
+
? errObj.code
|
|
182
|
+
: `http_${response.status}`;
|
|
183
|
+
err.nextAction =
|
|
184
|
+
typeof errObj.next_action === "string" ? errObj.next_action : undefined;
|
|
185
|
+
err.requestId =
|
|
186
|
+
typeof errObj.request_id === "string" ? errObj.request_id : undefined;
|
|
187
|
+
err.payload =
|
|
188
|
+
typeof errObj.details === "object" && errObj.details
|
|
189
|
+
? errObj.details
|
|
190
|
+
: {};
|
|
191
|
+
throw err;
|
|
192
|
+
}
|
|
193
|
+
throw new Error("Exhausted retries");
|
|
194
|
+
}
|
|
195
|
+
async callTool(name, args) {
|
|
196
|
+
try {
|
|
197
|
+
if (name === "mbc_list_bookmakers") {
|
|
198
|
+
const data = await this.apiRequest("GET", "/bookmakers", {
|
|
199
|
+
authRequired: false,
|
|
200
|
+
});
|
|
201
|
+
return okToolResult(`Loaded ${data.count ?? 0} supported bookmakers.`, data);
|
|
202
|
+
}
|
|
203
|
+
if (name === "mbc_list_markets") {
|
|
204
|
+
const data = await this.apiRequest("GET", "/markets", {
|
|
205
|
+
authRequired: false,
|
|
206
|
+
});
|
|
207
|
+
return okToolResult(`Loaded ${data.count ?? 0} canonical betting markets.`, data);
|
|
208
|
+
}
|
|
209
|
+
if (name === "mbc_get_usage") {
|
|
210
|
+
const data = await this.apiRequest("GET", "/usage");
|
|
211
|
+
return okToolResult(`Plan: ${data?.plan?.name ?? "Unknown"} (${data?.charge_order ?? "api_first"}) · API credits remaining: ${data?.api_credits?.total_remaining ?? 0}.`, data);
|
|
212
|
+
}
|
|
213
|
+
if (name === "mbc_view_slip") {
|
|
214
|
+
const data = await this.apiRequest("GET", "/view", {
|
|
215
|
+
query: { code: args["code"], bookmaker: args["bookmaker"] },
|
|
216
|
+
});
|
|
217
|
+
const gamesCount = Array.isArray(data?.selections)
|
|
218
|
+
? data.selections.length
|
|
219
|
+
: 0;
|
|
220
|
+
return okToolResult(`Slip ${data?.code} on ${data?.bookmaker?.name ?? args["bookmaker"]}: ${gamesCount} games · Total odds: ${data?.total_odds ?? "N/A"}.`, data);
|
|
221
|
+
}
|
|
222
|
+
if (name === "mbc_view_slip_image") {
|
|
223
|
+
const res = await this.apiRequest("GET", "/view/image", {
|
|
224
|
+
query: { code: args["code"], bookmaker: args["bookmaker"] },
|
|
225
|
+
rawBytes: true,
|
|
226
|
+
});
|
|
227
|
+
const structured = {
|
|
228
|
+
code: String(args["code"] ?? ""),
|
|
229
|
+
bookmaker: String(args["bookmaker"] ?? ""),
|
|
230
|
+
mime_type: "image/png",
|
|
231
|
+
credits_charged: res.creditsCharged,
|
|
232
|
+
};
|
|
233
|
+
return okToolResult(`Rendered PNG slip for ${structured.code} on ${structured.bookmaker} (${res.creditsCharged} credits).`, structured, res.base64);
|
|
234
|
+
}
|
|
235
|
+
if (name === "mbc_convert_code") {
|
|
236
|
+
const data = await this.apiRequest("POST", "/convert", {
|
|
237
|
+
body: {
|
|
238
|
+
code: args["code"],
|
|
239
|
+
from: args["from_bookmaker"] ?? args["from"],
|
|
240
|
+
to: args["to_bookmakers"] ?? args["to"],
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
const summary = data?.target
|
|
244
|
+
? `Converted ${data?.source?.code} -> ${data?.target?.code} (${data?.target?.name}) · ${data?.counts?.converted}/${data?.counts?.original} games.`
|
|
245
|
+
: `Batch converted ${data?.source?.code}: ${Array.isArray(data?.results) ? data.results.filter((r) => r.success).length : 0} target codes created.`;
|
|
246
|
+
return okToolResult(summary, data);
|
|
247
|
+
}
|
|
248
|
+
if (name === "mbc_convert_to_all") {
|
|
249
|
+
const data = await this.apiRequest("POST", "/convert/all", {
|
|
250
|
+
body: compact({
|
|
251
|
+
code: args["code"],
|
|
252
|
+
from: args["from_bookmaker"] ?? args["from"],
|
|
253
|
+
to: args["targets"] ?? args["to"],
|
|
254
|
+
include_card: args["include_card"],
|
|
255
|
+
}),
|
|
256
|
+
});
|
|
257
|
+
return okToolResult(`Converted ${args["code"]} to ${data?.successful_pairs ?? 0}/${data?.pairs ?? 0} bookmakers (${data?.credits_charged ?? 0} credits).`, data, typeof data?.card === "string" ? data.card : undefined);
|
|
258
|
+
}
|
|
259
|
+
if (name === "mbc_bulk_convert") {
|
|
260
|
+
const rawCodes = Array.isArray(args["codes"]) ? args["codes"] : [];
|
|
261
|
+
const codes = rawCodes.map((c) => ({
|
|
262
|
+
code: c?.code,
|
|
263
|
+
from: c?.from_bookmaker ?? c?.from ?? c?.source,
|
|
264
|
+
}));
|
|
265
|
+
const data = await this.apiRequest("POST", "/convert/bulk", {
|
|
266
|
+
body: compact({
|
|
267
|
+
codes,
|
|
268
|
+
to: args["targets"] ?? args["to"],
|
|
269
|
+
include_card: args["include_card"],
|
|
270
|
+
}),
|
|
271
|
+
});
|
|
272
|
+
return okToolResult(`Bulk converted ${codes.length} slips: ${data?.successful_pairs ?? 0}/${data?.pairs ?? 0} target codes created.`, data, typeof data?.card === "string" ? data.card : undefined);
|
|
273
|
+
}
|
|
274
|
+
if (name === "mbc_split_code") {
|
|
275
|
+
const data = await this.apiRequest("POST", "/split", {
|
|
276
|
+
body: compact({
|
|
277
|
+
code: args["code"],
|
|
278
|
+
bookmaker: args["bookmaker"],
|
|
279
|
+
count: args["count"] ?? 2,
|
|
280
|
+
pattern: args["method"] ?? args["pattern"] ?? "equal",
|
|
281
|
+
order: args["order"],
|
|
282
|
+
target_odds: args["target_odds"],
|
|
283
|
+
counts: args["counts"],
|
|
284
|
+
}),
|
|
285
|
+
});
|
|
286
|
+
return okToolResult(`Split ${data?.source_code} into ${data?.created_count}/${data?.count} codes (${data?.mode_label ?? data?.mode}): ${(data?.new_codes ?? []).join(", ")}.`, data);
|
|
287
|
+
}
|
|
288
|
+
if (name === "mbc_merge_codes") {
|
|
289
|
+
const data = await this.apiRequest("POST", "/merge", {
|
|
290
|
+
body: compact({
|
|
291
|
+
pairs: args["pairs"],
|
|
292
|
+
target: args["target"],
|
|
293
|
+
}),
|
|
294
|
+
});
|
|
295
|
+
return okToolResult(`Merged ${data?.codes_combined} codes into ${data?.target_code} (${data?.selections_count} games).`, data);
|
|
296
|
+
}
|
|
297
|
+
if (name === "mbc_search_events") {
|
|
298
|
+
const data = await this.apiRequest("POST", "/events/search", {
|
|
299
|
+
body: compact({
|
|
300
|
+
bookmaker: args["bookmaker"],
|
|
301
|
+
queries: args["queries"],
|
|
302
|
+
sport: args["sport"] ?? "football",
|
|
303
|
+
include_markets: args["include_markets"] ?? true,
|
|
304
|
+
market_filter: args["market_filter"],
|
|
305
|
+
detail: "agent",
|
|
306
|
+
}),
|
|
307
|
+
});
|
|
308
|
+
return okToolResult(`Searched ${Array.isArray(args["queries"]) ? args["queries"].length : 0} queries on ${args["bookmaker"]}.`, data);
|
|
309
|
+
}
|
|
310
|
+
if (name === "mbc_get_event_markets") {
|
|
311
|
+
const eventId = encodeURIComponent(String(args["event_id"] ?? ""));
|
|
312
|
+
const data = await this.apiRequest("GET", `/events/${eventId}/markets`, {
|
|
313
|
+
query: { bookmaker: args["bookmaker"] },
|
|
314
|
+
});
|
|
315
|
+
return okToolResult(`Loaded ${Array.isArray(data?.markets) ? data.markets.length : 0} markets for ${args["event_id"]}.`, data);
|
|
316
|
+
}
|
|
317
|
+
if (name === "mbc_resolve_picks") {
|
|
318
|
+
const data = await this.apiRequest("POST", "/resolve", {
|
|
319
|
+
body: compact({
|
|
320
|
+
bookmaker: args["bookmaker"],
|
|
321
|
+
picks: args["picks"],
|
|
322
|
+
strict: args["strict"] ?? true,
|
|
323
|
+
accept_odds_change: args["accept_odds_change"] ?? false,
|
|
324
|
+
detail: "agent",
|
|
325
|
+
}),
|
|
326
|
+
});
|
|
327
|
+
return okToolResult(`Resolved plan ${data?.plan_id} (${data?.status}).`, data);
|
|
328
|
+
}
|
|
329
|
+
if (name === "mbc_book_code") {
|
|
330
|
+
const data = await this.apiRequest("POST", "/book", {
|
|
331
|
+
body: compact({
|
|
332
|
+
bookmaker: args["bookmaker"],
|
|
333
|
+
plan_id: args["plan_id"],
|
|
334
|
+
picks: args["picks"],
|
|
335
|
+
to: args["to"],
|
|
336
|
+
strict: args["strict"],
|
|
337
|
+
accept_odds_change: args["accept_odds_change"],
|
|
338
|
+
}),
|
|
339
|
+
});
|
|
340
|
+
return okToolResult(`Booked code ${data?.code ?? "fan-out"} (verified: ${data?.verified ?? true}).`, data);
|
|
341
|
+
}
|
|
342
|
+
if (name === "mbc_manage_tracking") {
|
|
343
|
+
const action = String(args["action"] ?? "");
|
|
344
|
+
if (action === "start") {
|
|
345
|
+
const data = await this.apiRequest("POST", "/track", {
|
|
346
|
+
body: compact({
|
|
347
|
+
code: args["code"],
|
|
348
|
+
bookmaker: args["bookmaker"] ?? "sportybet",
|
|
349
|
+
mode: args["mode"] ?? "ticket",
|
|
350
|
+
label: args["label"],
|
|
351
|
+
webhook_url: args["webhook_url"],
|
|
352
|
+
}),
|
|
353
|
+
});
|
|
354
|
+
return okToolResult(`Started tracking ${data?.id}.`, data);
|
|
355
|
+
}
|
|
356
|
+
if (action === "get") {
|
|
357
|
+
const id = encodeURIComponent(String(args["track_id"] ?? ""));
|
|
358
|
+
const data = await this.apiRequest("GET", `/track/${id}`);
|
|
359
|
+
return okToolResult(`Track ${data?.id} (${data?.status}).`, data);
|
|
360
|
+
}
|
|
361
|
+
if (action === "stop") {
|
|
362
|
+
const id = encodeURIComponent(String(args["track_id"] ?? ""));
|
|
363
|
+
const data = await this.apiRequest("DELETE", `/track/${id}`);
|
|
364
|
+
return okToolResult(`Stopped track ${id}.`, data);
|
|
365
|
+
}
|
|
366
|
+
if (action === "list") {
|
|
367
|
+
const data = await this.apiRequest("GET", "/tracks", {
|
|
368
|
+
query: { status: args["status"] },
|
|
369
|
+
});
|
|
370
|
+
return okToolResult(`Listed ${data?.count ?? 0} tracks.`, data);
|
|
371
|
+
}
|
|
372
|
+
return errorToolResult("invalid_request", `Unknown tracking action: ${action}`);
|
|
373
|
+
}
|
|
374
|
+
if (name === "mbc_manage_jobs_and_history") {
|
|
375
|
+
const action = String(args["action"] ?? "");
|
|
376
|
+
if (action === "create_job") {
|
|
377
|
+
const data = await this.apiRequest("POST", "/jobs", {
|
|
378
|
+
body: args["job_payload"] ?? {},
|
|
379
|
+
});
|
|
380
|
+
return okToolResult(`Created job ${data?.id}.`, data);
|
|
381
|
+
}
|
|
382
|
+
if (action === "get_job") {
|
|
383
|
+
const id = encodeURIComponent(String(args["job_id"] ?? ""));
|
|
384
|
+
const data = await this.apiRequest("GET", `/jobs/${id}`);
|
|
385
|
+
return okToolResult(`Job ${data?.id} (${data?.status}).`, data);
|
|
386
|
+
}
|
|
387
|
+
if (action === "list_jobs") {
|
|
388
|
+
const data = await this.apiRequest("GET", "/jobs", {
|
|
389
|
+
query: {
|
|
390
|
+
limit: args["limit"],
|
|
391
|
+
offset: args["offset"],
|
|
392
|
+
status: args["status"],
|
|
393
|
+
},
|
|
394
|
+
});
|
|
395
|
+
return okToolResult(`Listed ${(data?.items ?? []).length} jobs.`, data);
|
|
396
|
+
}
|
|
397
|
+
if (action === "cancel_job") {
|
|
398
|
+
const id = encodeURIComponent(String(args["job_id"] ?? ""));
|
|
399
|
+
const data = await this.apiRequest("DELETE", `/jobs/${id}`);
|
|
400
|
+
return okToolResult(`Cancelled job ${id}.`, data);
|
|
401
|
+
}
|
|
402
|
+
if (action === "list_history") {
|
|
403
|
+
const data = await this.apiRequest("GET", "/history", {
|
|
404
|
+
query: {
|
|
405
|
+
limit: args["limit"],
|
|
406
|
+
offset: args["offset"],
|
|
407
|
+
bookmaker: args["bookmaker"],
|
|
408
|
+
target: args["target"],
|
|
409
|
+
status: args["status"],
|
|
410
|
+
},
|
|
411
|
+
});
|
|
412
|
+
return okToolResult(`Listed ${(data?.items ?? []).length} history rows.`, data);
|
|
413
|
+
}
|
|
414
|
+
if (action === "get_history") {
|
|
415
|
+
const id = encodeURIComponent(String(args["row_id"] ?? ""));
|
|
416
|
+
const data = await this.apiRequest("GET", `/history/${id}`);
|
|
417
|
+
return okToolResult(`Loaded history row ${data?.id}.`, data);
|
|
418
|
+
}
|
|
419
|
+
if (action === "list_books") {
|
|
420
|
+
const data = await this.apiRequest("GET", "/books", {
|
|
421
|
+
query: { limit: args["limit"], offset: args["offset"] },
|
|
422
|
+
});
|
|
423
|
+
return okToolResult(`Listed ${(data?.items ?? []).length} books.`, data);
|
|
424
|
+
}
|
|
425
|
+
if (action === "get_book") {
|
|
426
|
+
const id = encodeURIComponent(String(args["row_id"] ?? ""));
|
|
427
|
+
const data = await this.apiRequest("GET", `/books/${id}`);
|
|
428
|
+
return okToolResult(`Loaded book ${data?.id}.`, data);
|
|
429
|
+
}
|
|
430
|
+
return errorToolResult("invalid_request", `Unknown action: ${action}`);
|
|
431
|
+
}
|
|
432
|
+
return errorToolResult("invalid_request", `Unknown tool: ${name}`);
|
|
433
|
+
}
|
|
434
|
+
catch (err) {
|
|
435
|
+
const httpErr = err;
|
|
436
|
+
return errorToolResult(httpErr.code ?? "internal_error", httpErr.message ?? String(err), httpErr.payload ?? {}, httpErr.nextAction, httpErr.requestId);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
async readResource(uri) {
|
|
440
|
+
let payload;
|
|
441
|
+
if (uri === "mbc://catalog/bookmakers") {
|
|
442
|
+
payload = await this.apiRequest("GET", "/bookmakers", { authRequired: false });
|
|
443
|
+
}
|
|
444
|
+
else if (uri === "mbc://catalog/markets") {
|
|
445
|
+
payload = await this.apiRequest("GET", "/markets", { authRequired: false });
|
|
446
|
+
}
|
|
447
|
+
else if (uri === "mbc://catalog/capabilities") {
|
|
448
|
+
payload = await this.apiRequest("GET", "/capabilities", { authRequired: false });
|
|
449
|
+
}
|
|
450
|
+
else if (uri === "mbc://account/usage") {
|
|
451
|
+
payload = await this.apiRequest("GET", "/usage");
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
throw new Error(`Unknown resource URI: ${uri}`);
|
|
455
|
+
}
|
|
456
|
+
return {
|
|
457
|
+
contents: [
|
|
458
|
+
{
|
|
459
|
+
uri,
|
|
460
|
+
mimeType: "application/json",
|
|
461
|
+
text: JSON.stringify(payload, null, 2),
|
|
462
|
+
},
|
|
463
|
+
],
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
getPrompt(name, args) {
|
|
467
|
+
if (name === "convert-to-all-bookmakers") {
|
|
468
|
+
const code = args["code"] ?? "<CODE>";
|
|
469
|
+
const from = args["from_bookmaker"] ?? "sportybet";
|
|
470
|
+
const targets = args["targets"]
|
|
471
|
+
? ` to targets [${args["targets"]}]`
|
|
472
|
+
: " to all supported bookmakers";
|
|
473
|
+
return {
|
|
474
|
+
description: `Convert ${code} from ${from}${targets}`,
|
|
475
|
+
messages: [
|
|
476
|
+
{
|
|
477
|
+
role: "user",
|
|
478
|
+
content: {
|
|
479
|
+
type: "text",
|
|
480
|
+
text: `Call \`mbc_convert_to_all\` with \`code='${code}'\` and \`from_bookmaker='${from}'\`${targets}. Present a table of Bookmaker, Converted Code, Games Converted, Target Odds, and Status. Use 'games' (never 'legs').`,
|
|
481
|
+
},
|
|
482
|
+
},
|
|
483
|
+
],
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
if (name === "bulk-convert-codes") {
|
|
487
|
+
return {
|
|
488
|
+
description: "Bulk convert multiple booking codes",
|
|
489
|
+
messages: [
|
|
490
|
+
{
|
|
491
|
+
role: "user",
|
|
492
|
+
content: {
|
|
493
|
+
type: "text",
|
|
494
|
+
text: `Parse these source codes: ${args["codes_list"] ?? ""}. Call \`mbc_bulk_convert\` and show the converted codes grouped by each source slip.`,
|
|
495
|
+
},
|
|
496
|
+
},
|
|
497
|
+
],
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
if (name === "split-slip") {
|
|
501
|
+
return {
|
|
502
|
+
description: "Split a booking code into smaller codes",
|
|
503
|
+
messages: [
|
|
504
|
+
{
|
|
505
|
+
role: "user",
|
|
506
|
+
content: {
|
|
507
|
+
type: "text",
|
|
508
|
+
text: `Split booking code \`${args["code"] ?? ""}\` on \`${args["bookmaker"] ?? "sportybet"}\` using \`mbc_split_code\` with method='${args["method"] ?? "equal"}', order='${args["order"] ?? "mixed"}', and value='${args["value"] ?? "2"}'.`,
|
|
509
|
+
},
|
|
510
|
+
},
|
|
511
|
+
],
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
if (name === "build-slip-from-text") {
|
|
515
|
+
return {
|
|
516
|
+
description: "Resolve games and book a verified slip",
|
|
517
|
+
messages: [
|
|
518
|
+
{
|
|
519
|
+
role: "user",
|
|
520
|
+
content: {
|
|
521
|
+
type: "text",
|
|
522
|
+
text: `Build a new booking code on \`${args["bookmaker"] ?? "sportybet"}\` for: ${args["picks_text"] ?? ""}. First call \`mbc_resolve_picks\`, confirm any ambiguous games, and then call \`mbc_book_code\` with the plan_id.`,
|
|
523
|
+
},
|
|
524
|
+
},
|
|
525
|
+
],
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
throw new Error(`Unknown prompt: ${name}`);
|
|
529
|
+
}
|
|
530
|
+
async handleMessage(msg) {
|
|
531
|
+
if (!msg || msg.jsonrpc !== "2.0" || typeof msg.method !== "string") {
|
|
532
|
+
return {
|
|
533
|
+
jsonrpc: "2.0",
|
|
534
|
+
id: msg?.id ?? null,
|
|
535
|
+
error: { code: -32600, message: "Invalid JSON-RPC 2.0 request." },
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
if (msg.id === undefined || msg.method.startsWith("notifications/")) {
|
|
539
|
+
return null;
|
|
540
|
+
}
|
|
541
|
+
const params = (msg.params ?? {});
|
|
542
|
+
if (msg.method === "initialize") {
|
|
543
|
+
const requested = String(params["protocolVersion"] ?? DEFAULT_PROTOCOL);
|
|
544
|
+
const negotiated = SUPPORTED_PROTOCOLS.has(requested)
|
|
545
|
+
? requested
|
|
546
|
+
: DEFAULT_PROTOCOL;
|
|
547
|
+
return {
|
|
548
|
+
jsonrpc: "2.0",
|
|
549
|
+
id: msg.id,
|
|
550
|
+
result: {
|
|
551
|
+
protocolVersion: negotiated,
|
|
552
|
+
capabilities: {
|
|
553
|
+
tools: { listChanged: false },
|
|
554
|
+
resources: { subscribe: false, listChanged: false },
|
|
555
|
+
prompts: { listChanged: false },
|
|
556
|
+
},
|
|
557
|
+
serverInfo: {
|
|
558
|
+
name: SERVER_NAME,
|
|
559
|
+
version: VERSION,
|
|
560
|
+
},
|
|
561
|
+
},
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
if (msg.method === "ping") {
|
|
565
|
+
return { jsonrpc: "2.0", id: msg.id, result: {} };
|
|
566
|
+
}
|
|
567
|
+
if (msg.method === "tools/list") {
|
|
568
|
+
return {
|
|
569
|
+
jsonrpc: "2.0",
|
|
570
|
+
id: msg.id,
|
|
571
|
+
result: { tools: MCP_TOOLS },
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
if (msg.method === "resources/list") {
|
|
575
|
+
return {
|
|
576
|
+
jsonrpc: "2.0",
|
|
577
|
+
id: msg.id,
|
|
578
|
+
result: { resources: MCP_RESOURCES },
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
if (msg.method === "prompts/list") {
|
|
582
|
+
return {
|
|
583
|
+
jsonrpc: "2.0",
|
|
584
|
+
id: msg.id,
|
|
585
|
+
result: { prompts: MCP_PROMPTS },
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
if (msg.method === "tools/call") {
|
|
589
|
+
const name = String(params["name"] ?? "");
|
|
590
|
+
const args = typeof params["arguments"] === "object" && params["arguments"]
|
|
591
|
+
? params["arguments"]
|
|
592
|
+
: {};
|
|
593
|
+
const result = await this.callTool(name, args);
|
|
594
|
+
return { jsonrpc: "2.0", id: msg.id, result };
|
|
595
|
+
}
|
|
596
|
+
if (msg.method === "resources/read") {
|
|
597
|
+
try {
|
|
598
|
+
const result = await this.readResource(String(params["uri"] ?? ""));
|
|
599
|
+
return { jsonrpc: "2.0", id: msg.id, result };
|
|
600
|
+
}
|
|
601
|
+
catch (err) {
|
|
602
|
+
return {
|
|
603
|
+
jsonrpc: "2.0",
|
|
604
|
+
id: msg.id,
|
|
605
|
+
error: {
|
|
606
|
+
code: -32001,
|
|
607
|
+
message: err instanceof Error ? err.message : String(err),
|
|
608
|
+
},
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
if (msg.method === "prompts/get") {
|
|
613
|
+
try {
|
|
614
|
+
const result = this.getPrompt(String(params["name"] ?? ""), params["arguments"] ?? {});
|
|
615
|
+
return { jsonrpc: "2.0", id: msg.id, result };
|
|
616
|
+
}
|
|
617
|
+
catch (err) {
|
|
618
|
+
return {
|
|
619
|
+
jsonrpc: "2.0",
|
|
620
|
+
id: msg.id,
|
|
621
|
+
error: {
|
|
622
|
+
code: -32602,
|
|
623
|
+
message: err instanceof Error ? err.message : String(err),
|
|
624
|
+
},
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return {
|
|
629
|
+
jsonrpc: "2.0",
|
|
630
|
+
id: msg.id,
|
|
631
|
+
error: { code: -32601, message: `Method not found: '${msg.method}'.` },
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@managebetcodes/mcp",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Official Model Context Protocol (MCP) server for Manage Bet Codes API v1",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Manage Bet Codes",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/kayceepeece/managebetcodes-mcp.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://managebetcodes.app/mcp",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"bin": {
|
|
22
|
+
"mbc-mcp": "dist/cli.js"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"README.md"
|
|
27
|
+
],
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=18"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc -p tsconfig.json",
|
|
37
|
+
"test": "npm run build && node --test test/mcp-server.test.mjs",
|
|
38
|
+
"prepack": "npm run build"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"mcp",
|
|
42
|
+
"model-context-protocol",
|
|
43
|
+
"betting",
|
|
44
|
+
"bet-codes",
|
|
45
|
+
"sportybet",
|
|
46
|
+
"1xbet",
|
|
47
|
+
"bet9ja",
|
|
48
|
+
"managebetcodes"
|
|
49
|
+
],
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"typescript": "^5.4.0"
|
|
52
|
+
}
|
|
53
|
+
}
|