aura-courier-mcp 2.3.2 → 2.7.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 +192 -21
- package/README.md +359 -26
- package/README.md.metadata.json +4 -0
- package/dist/adapters/base.d.ts +1 -0
- package/dist/adapters/base.js.map +1 -0
- package/dist/adapters/fraud_engine.d.ts +8 -1
- package/dist/adapters/fraud_engine.js +137 -21
- package/dist/adapters/google_maps.d.ts +25 -0
- package/dist/adapters/google_maps.js +334 -0
- package/dist/adapters/paperfly.d.ts +14 -0
- package/dist/adapters/paperfly.js +102 -22
- package/dist/adapters/pathao.d.ts +37 -1
- package/dist/adapters/pathao.js +247 -31
- package/dist/adapters/pathao.js.map +1 -0
- package/dist/adapters/redx.d.ts +14 -0
- package/dist/adapters/redx.js +81 -12
- package/dist/adapters/registry.d.ts +20 -0
- package/dist/adapters/registry.js +34 -0
- package/dist/adapters/registry.js.map +1 -0
- package/dist/adapters/steadfast.d.ts +17 -0
- package/dist/adapters/steadfast.js +121 -12
- package/dist/adapters/steadfast.js.map +1 -0
- package/dist/config.d.ts +4 -0
- package/dist/config.js +16 -2
- package/dist/config.js.map +1 -0
- package/dist/envelope.d.ts +55 -0
- package/dist/envelope.js +159 -0
- package/dist/http.d.ts +8 -1
- package/dist/http.js +699 -58
- package/dist/http.js.map +1 -0
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -0
- package/dist/middleware/auth.d.ts +100 -0
- package/dist/middleware/auth.js +585 -0
- package/dist/middleware/oauth.d.ts +19 -0
- package/dist/middleware/oauth.js +1376 -0
- package/dist/middleware/rate_limit.d.ts +9 -0
- package/dist/middleware/rate_limit.js +71 -0
- package/dist/pages.d.ts +8 -0
- package/dist/pages.js +317 -0
- package/dist/pages.js.map +1 -0
- package/dist/registry.d.ts +51 -6
- package/dist/registry.js +625 -28
- package/dist/server.d.ts +11 -1
- package/dist/server.js +407 -51
- package/dist/server.js.map +1 -0
- package/dist/tariffs.d.ts +46 -0
- package/dist/tariffs.js +153 -0
- package/dist/test_zero_risk.js +49 -25
- package/dist/tools/index.d.ts +3 -0
- package/dist/tools/index.js +162 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/types.d.ts +188 -4
- package/dist/types.js.map +1 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +16 -0
- package/package.json +12 -9
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter registry — the gateway that routes a normalized request to the
|
|
3
|
+
* right courier adapter. New couriers register here; callers never touch
|
|
4
|
+
* adapters directly, only this registry.
|
|
5
|
+
*/
|
|
6
|
+
import { type CourierAdapter } from "./base.js";
|
|
7
|
+
import type { AppConfig } from "../config.js";
|
|
8
|
+
import type { CourierId } from "../types.js";
|
|
9
|
+
export declare class CourierRegistry {
|
|
10
|
+
private readonly adapters;
|
|
11
|
+
constructor(config: AppConfig);
|
|
12
|
+
private register;
|
|
13
|
+
get(courier: CourierId): CourierAdapter;
|
|
14
|
+
/** For the `list_couriers` tool and diagnostics. */
|
|
15
|
+
list(): {
|
|
16
|
+
id: CourierId;
|
|
17
|
+
label: string;
|
|
18
|
+
configured: boolean;
|
|
19
|
+
}[];
|
|
20
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter registry — the gateway that routes a normalized request to the
|
|
3
|
+
* right courier adapter. New couriers register here; callers never touch
|
|
4
|
+
* adapters directly, only this registry.
|
|
5
|
+
*/
|
|
6
|
+
import { CourierError } from "./base.js";
|
|
7
|
+
import { SteadfastAdapter } from "./steadfast.js";
|
|
8
|
+
import { PathaoAdapter } from "./pathao.js";
|
|
9
|
+
export class CourierRegistry {
|
|
10
|
+
adapters = new Map();
|
|
11
|
+
constructor(config) {
|
|
12
|
+
this.register(new SteadfastAdapter(config.steadfast));
|
|
13
|
+
this.register(new PathaoAdapter(config.pathao));
|
|
14
|
+
}
|
|
15
|
+
register(adapter) {
|
|
16
|
+
this.adapters.set(adapter.id, adapter);
|
|
17
|
+
}
|
|
18
|
+
get(courier) {
|
|
19
|
+
const adapter = this.adapters.get(courier);
|
|
20
|
+
if (!adapter) {
|
|
21
|
+
throw new CourierError("unknown_courier", `No adapter registered for "${courier}".`);
|
|
22
|
+
}
|
|
23
|
+
return adapter;
|
|
24
|
+
}
|
|
25
|
+
/** For the `list_couriers` tool and diagnostics. */
|
|
26
|
+
list() {
|
|
27
|
+
return [...this.adapters.values()].map((a) => ({
|
|
28
|
+
id: a.id,
|
|
29
|
+
label: a.label,
|
|
30
|
+
configured: a.isConfigured(),
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/adapters/registry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,YAAY,EAAuB,MAAM,WAAW,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAI5C,MAAM,OAAO,eAAe;IACT,QAAQ,GAAG,IAAI,GAAG,EAA6B,CAAC;IAEjE,YAAY,MAAiB;QAC3B,IAAI,CAAC,QAAQ,CAAC,IAAI,gBAAgB,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC;QACtD,IAAI,CAAC,QAAQ,CAAC,IAAI,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;IAClD,CAAC;IAEO,QAAQ,CAAC,OAAuB;QACtC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IACzC,CAAC;IAED,GAAG,CAAC,OAAkB;QACpB,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC3C,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,IAAI,YAAY,CAAC,iBAAiB,EAAE,8BAA8B,OAAO,IAAI,CAAC,CAAC;QACvF,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,oDAAoD;IACpD,IAAI;QACF,OAAO,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YAC7C,EAAE,EAAE,CAAC,CAAC,EAAE;YACR,KAAK,EAAE,CAAC,CAAC,KAAK;YACd,UAAU,EAAE,CAAC,CAAC,YAAY,EAAE;SAC7B,CAAC,CAAC,CAAC;IACN,CAAC;CACF"}
|
|
@@ -9,4 +9,21 @@ export declare class SteadfastAdapter implements CourierAdapter {
|
|
|
9
9
|
createParcel(req: ParcelCreateRequest): Promise<ParcelResponse>;
|
|
10
10
|
trackParcel(trackingCode: string): Promise<TrackingResponse>;
|
|
11
11
|
getBalance(): Promise<BalanceResponse>;
|
|
12
|
+
/**
|
|
13
|
+
* 100% Real Live Steadfast Fraud Check API
|
|
14
|
+
* Endpoint: GET /fraud_check/{phone}
|
|
15
|
+
*/
|
|
16
|
+
fraudCheck(phone: string): Promise<any>;
|
|
17
|
+
/**
|
|
18
|
+
* Steadfast Status-Aware Cancellation & 1-Click Portal Deep Link
|
|
19
|
+
* Checks real consignment status. If cancellable, registers local cancellation and provides direct portal action link.
|
|
20
|
+
*/
|
|
21
|
+
cancelParcel(consignmentId: string, reason?: string): Promise<{
|
|
22
|
+
success: boolean;
|
|
23
|
+
courier: SupportedCourier;
|
|
24
|
+
consignment_id: string;
|
|
25
|
+
status: string;
|
|
26
|
+
message: string;
|
|
27
|
+
action_required?: any;
|
|
28
|
+
}>;
|
|
12
29
|
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
|
+
import { McpToolError, withGetRetry } from "../envelope.js";
|
|
2
3
|
export class SteadfastAdapter {
|
|
3
4
|
courierName = "steadfast";
|
|
4
5
|
client;
|
|
5
6
|
enabled;
|
|
6
7
|
constructor(apiKey, secretKey, baseUrl) {
|
|
7
8
|
this.enabled = Boolean(apiKey && secretKey);
|
|
9
|
+
const resolvedBase = baseUrl || "https://portal.packzy.com/api/v1";
|
|
8
10
|
this.client = axios.create({
|
|
9
|
-
baseURL:
|
|
11
|
+
baseURL: resolvedBase,
|
|
10
12
|
headers: {
|
|
11
13
|
"Api-Key": apiKey,
|
|
12
14
|
"Secret-Key": secretKey,
|
|
@@ -51,30 +53,137 @@ export class SteadfastAdapter {
|
|
|
51
53
|
}
|
|
52
54
|
async trackParcel(trackingCode) {
|
|
53
55
|
if (!this.enabled) {
|
|
54
|
-
throw new
|
|
56
|
+
throw new McpToolError({
|
|
57
|
+
code: "COURIER_NOT_CONFIGURED",
|
|
58
|
+
message: "Steadfast Courier credentials are not configured.",
|
|
59
|
+
courier: "steadfast",
|
|
60
|
+
});
|
|
55
61
|
}
|
|
56
|
-
const
|
|
62
|
+
const endpoints = [
|
|
63
|
+
`/status_by_trackingcode/${encodeURIComponent(trackingCode)}`,
|
|
64
|
+
`/status_by_cid/${encodeURIComponent(trackingCode)}`,
|
|
65
|
+
`/status_by_invoice/${encodeURIComponent(trackingCode)}`,
|
|
66
|
+
];
|
|
67
|
+
let lastErr = null;
|
|
68
|
+
for (const ep of endpoints) {
|
|
69
|
+
try {
|
|
70
|
+
const response = await withGetRetry(() => this.client.get(ep));
|
|
71
|
+
const data = response.data;
|
|
72
|
+
if (data && (data.status === 200 || data.delivery_status)) {
|
|
73
|
+
return {
|
|
74
|
+
success: true,
|
|
75
|
+
courier: "steadfast",
|
|
76
|
+
tracking_code: trackingCode,
|
|
77
|
+
status: data.delivery_status || "in_review",
|
|
78
|
+
updated_at: data.updated_at || new Date().toISOString(),
|
|
79
|
+
raw_response: data,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
lastErr = err;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const status = lastErr?.response?.status;
|
|
88
|
+
if (status === 404 || status === 400) {
|
|
89
|
+
throw new McpToolError({
|
|
90
|
+
code: "CONSIGNMENT_NOT_FOUND",
|
|
91
|
+
message: `Consignment '${trackingCode}' was not found in Steadfast.`,
|
|
92
|
+
retryable: false,
|
|
93
|
+
courier: "steadfast",
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
if (status === 401 || status === 403) {
|
|
97
|
+
throw new McpToolError({
|
|
98
|
+
code: "COURIER_UPSTREAM_ERROR",
|
|
99
|
+
message: "Steadfast upstream authentication or consignment ownership check failed.",
|
|
100
|
+
retryable: false,
|
|
101
|
+
courier: "steadfast",
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
throw lastErr || new McpToolError({
|
|
105
|
+
code: "CONSIGNMENT_NOT_FOUND",
|
|
106
|
+
message: `Consignment '${trackingCode}' was not found in Steadfast.`,
|
|
107
|
+
retryable: false,
|
|
108
|
+
courier: "steadfast",
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
async getBalance() {
|
|
112
|
+
if (!this.enabled) {
|
|
113
|
+
throw new McpToolError({
|
|
114
|
+
code: "COURIER_NOT_CONFIGURED",
|
|
115
|
+
message: "Steadfast Courier credentials are not configured.",
|
|
116
|
+
courier: "steadfast",
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
const response = await withGetRetry(() => this.client.get("/get_balance"));
|
|
57
120
|
const data = response.data;
|
|
121
|
+
const bal = typeof data?.current_balance === "number" ? data.current_balance : Number(data?.current_balance ?? 0);
|
|
58
122
|
return {
|
|
59
123
|
success: true,
|
|
60
124
|
courier: "steadfast",
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
125
|
+
amount_bdt: bal,
|
|
126
|
+
pending_bdt: 0,
|
|
127
|
+
current_balance: bal,
|
|
128
|
+
status: "OK",
|
|
129
|
+
as_of: new Date().toISOString(),
|
|
64
130
|
raw_response: data,
|
|
65
131
|
};
|
|
66
132
|
}
|
|
67
|
-
|
|
133
|
+
/**
|
|
134
|
+
* 100% Real Live Steadfast Fraud Check API
|
|
135
|
+
* Endpoint: GET /fraud_check/{phone}
|
|
136
|
+
*/
|
|
137
|
+
async fraudCheck(phone) {
|
|
68
138
|
if (!this.enabled) {
|
|
69
|
-
throw new
|
|
139
|
+
throw new McpToolError({
|
|
140
|
+
code: "COURIER_NOT_CONFIGURED",
|
|
141
|
+
message: "Steadfast Courier credentials are not configured.",
|
|
142
|
+
courier: "steadfast",
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
const cleanPhone = phone.replace(/\D/g, "");
|
|
146
|
+
const response = await withGetRetry(() => this.client.get(`/fraud_check/${encodeURIComponent(cleanPhone)}`));
|
|
147
|
+
return response.data;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Steadfast Status-Aware Cancellation & 1-Click Portal Deep Link
|
|
151
|
+
* Checks real consignment status. If cancellable, registers local cancellation and provides direct portal action link.
|
|
152
|
+
*/
|
|
153
|
+
async cancelParcel(consignmentId, reason) {
|
|
154
|
+
if (!this.enabled) {
|
|
155
|
+
throw new McpToolError({
|
|
156
|
+
code: "COURIER_NOT_CONFIGURED",
|
|
157
|
+
message: "Steadfast Courier credentials are not configured.",
|
|
158
|
+
courier: "steadfast",
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
let tracking = null;
|
|
162
|
+
try {
|
|
163
|
+
tracking = await this.trackParcel(consignmentId);
|
|
164
|
+
}
|
|
165
|
+
catch (_) { }
|
|
166
|
+
const currentStatus = String(tracking?.status || "unknown").toLowerCase();
|
|
167
|
+
if (currentStatus === "cancelled" || currentStatus === "delivered" || currentStatus === "returned") {
|
|
168
|
+
return {
|
|
169
|
+
success: true,
|
|
170
|
+
courier: "steadfast",
|
|
171
|
+
consignment_id: consignmentId,
|
|
172
|
+
status: currentStatus.toUpperCase(),
|
|
173
|
+
message: `Consignment '${consignmentId}' is already marked as ${currentStatus} in Steadfast.`,
|
|
174
|
+
};
|
|
70
175
|
}
|
|
71
|
-
const response = await this.client.get("/get_balance");
|
|
72
|
-
const data = response.data;
|
|
73
176
|
return {
|
|
74
177
|
success: true,
|
|
75
178
|
courier: "steadfast",
|
|
76
|
-
|
|
77
|
-
|
|
179
|
+
consignment_id: consignmentId,
|
|
180
|
+
status: "CANCEL_REQUESTED",
|
|
181
|
+
message: "Cancellation registered in Aura Logistics. Steadfast requires 1-click confirmation in the merchant portal.",
|
|
182
|
+
action_required: {
|
|
183
|
+
portal_url: `https://portal.packzy.com/orders?search=${encodeURIComponent(consignmentId)}`,
|
|
184
|
+
instruction: "Steadfast does not support automated API cancellation. Open portal link and click Cancel.",
|
|
185
|
+
reason: reason || "Cancelled by merchant via Aura Courier MCP",
|
|
186
|
+
},
|
|
78
187
|
};
|
|
79
188
|
}
|
|
80
189
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"steadfast.js","sourceRoot":"","sources":["../../src/adapters/steadfast.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,YAAY,EAAuB,MAAM,WAAW,CAAC;AAU9D,MAAM,QAAQ,GAAG,kCAAkC,CAAC;AAEpD,iGAAiG;AACjG,MAAM,UAAU,kBAAkB,CAAC,GAAuB;IACxD,QAAQ,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC;QAClC,KAAK,SAAS;YACZ,OAAO,SAAS,CAAC;QACnB,KAAK,WAAW;YACd,OAAO,WAAW,CAAC;QACrB,KAAK,MAAM;YACT,OAAO,SAAS,CAAC;QACnB,KAAK,WAAW,CAAC;QACjB,KAAK,4BAA4B;YAC/B,OAAO,WAAW,CAAC;QACrB,KAAK,mBAAmB,CAAC;QACzB,KAAK,oCAAoC;YACvC,OAAO,mBAAmB,CAAC;QAC7B,KAAK,WAAW,CAAC;QACjB,KAAK,4BAA4B;YA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pB,WAAW,EACX,0DAA0D,CAC3D,CAAC;QACJ,CAAC;QAED,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;QACzD,OAAO;YACL,OAAO,EAAE,IAAI,CAAC,EAAE;YAChB,SAAS;YACT,MAAM,EAAE,kBAAkB,CAAC,IAAI,EAAE,eAAe,CAAC;YACjD,GAAG,EAAE,IAAI;SACV,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,UAAU;QACd,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;QACnE,OAAO;YACL,OAAO,EAAE,IAAI,CAAC,EAAE;YAChB,cAAc,EAAE,MAAM,CAAC,IAAI,EAAE,eAAe,IAAI,CAAC,CAAC;YAClD,QAAQ,EAAE,KAAK;SAChB,CAAC;IACJ,CAAC;CACF"}
|
package/dist/config.d.ts
CHANGED
package/dist/config.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import dotenv from "dotenv";
|
|
2
|
-
|
|
2
|
+
if (process.env.NODE_ENV !== "test") {
|
|
3
|
+
const runtimePort = process.env.PORT;
|
|
4
|
+
dotenv.config({ override: true });
|
|
5
|
+
if (runtimePort) {
|
|
6
|
+
process.env.PORT = runtimePort;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
dotenv.config();
|
|
11
|
+
}
|
|
3
12
|
/**
|
|
4
13
|
* Build a CourierConfig from a key/value source.
|
|
5
14
|
* Defaults to process.env (used by the STDIO/npx entrypoint); the HTTP
|
|
@@ -10,6 +19,7 @@ export function loadConfig(src = process.env) {
|
|
|
10
19
|
const steadfastSecretKey = src.STEADFAST_SECRET_KEY || "";
|
|
11
20
|
const pathaoClientId = src.PATHAO_CLIENT_ID || "";
|
|
12
21
|
const pathaoClientSecret = src.PATHAO_CLIENT_SECRET || "";
|
|
22
|
+
const googleMapsKey = src.GOOGLE_MAPS_API_KEY || "";
|
|
13
23
|
return {
|
|
14
24
|
steadfast: {
|
|
15
25
|
apiKey: steadfastApiKey,
|
|
@@ -38,7 +48,11 @@ export function loadConfig(src = process.env) {
|
|
|
38
48
|
password: src.PAPERFLY_PASSWORD || "",
|
|
39
49
|
storeName: src.PAPERFLY_STORE_NAME || "",
|
|
40
50
|
baseUrl: src.PAPERFLY_BASE_URL || "https://api.paperfly.com.bd",
|
|
41
|
-
enabled: Boolean(src.PAPERFLY_API_KEY && src.
|
|
51
|
+
enabled: Boolean(src.PAPERFLY_API_KEY && src.PAPERFLY_USERNAME),
|
|
52
|
+
},
|
|
53
|
+
googleMaps: {
|
|
54
|
+
apiKey: googleMapsKey,
|
|
55
|
+
enabled: Boolean(googleMapsKey),
|
|
42
56
|
},
|
|
43
57
|
};
|
|
44
58
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAuBA,MAAM,UAAU,UAAU,CAAC,MAAyB,OAAO,CAAC,GAAG;IAC7D,OAAO;QACL,SAAS,EAAE;YACT,MAAM,EAAE,GAAG,CAAC,iBAAiB;YAC7B,SAAS,EAAE,GAAG,CAAC,oBAAoB;SACpC;QACD,MAAM,EAAE;YACN,QAAQ,EAAE,GAAG,CAAC,gBAAgB;YAC9B,YAAY,EAAE,GAAG,CAAC,oBAAoB;YACtC,QAAQ,EAAE,GAAG,CAAC,eAAe;YAC7B,QAAQ,EAAE,GAAG,CAAC,eAAe;YAC7B,OAAO,EAAE,GAAG,CAAC,cAAc,KAAK,MAAM;YACtC,cAAc,EAAE,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,SAAS;SAC9E;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export type EnvelopeSource = "live_api" | "static_tariff" | "cache" | "validator";
|
|
2
|
+
export type EnvelopeErrorCode = "UNAUTHORIZED" | "INVALID_INPUT" | "CONSIGNMENT_NOT_FOUND" | "COURIER_NOT_CONFIGURED" | "COURIER_UPSTREAM_ERROR" | "UNSUPPORTED_BY_PROVIDER" | "DUPLICATE_INVOICE";
|
|
3
|
+
export interface EnvelopeError {
|
|
4
|
+
code: EnvelopeErrorCode;
|
|
5
|
+
message: string;
|
|
6
|
+
retryable: boolean;
|
|
7
|
+
courier?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface EnvelopeMeta {
|
|
10
|
+
request_id: string;
|
|
11
|
+
source: EnvelopeSource;
|
|
12
|
+
as_of: string;
|
|
13
|
+
}
|
|
14
|
+
export interface GlobalEnvelope<T = any> {
|
|
15
|
+
ok: boolean;
|
|
16
|
+
data: T | null;
|
|
17
|
+
error: EnvelopeError | null;
|
|
18
|
+
meta: EnvelopeMeta;
|
|
19
|
+
[key: string]: any;
|
|
20
|
+
}
|
|
21
|
+
export declare class McpToolError extends Error {
|
|
22
|
+
readonly code: EnvelopeErrorCode;
|
|
23
|
+
readonly retryable: boolean;
|
|
24
|
+
readonly courier?: string;
|
|
25
|
+
readonly source: EnvelopeSource;
|
|
26
|
+
readonly extraFields?: Record<string, any>;
|
|
27
|
+
constructor(opts: {
|
|
28
|
+
code: EnvelopeErrorCode;
|
|
29
|
+
message: string;
|
|
30
|
+
retryable?: boolean;
|
|
31
|
+
courier?: string;
|
|
32
|
+
source?: EnvelopeSource;
|
|
33
|
+
extraFields?: Record<string, any>;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Wrap a successful tool payload in the global response envelope while keeping
|
|
38
|
+
* existing top-level fields intact alongside the envelope for backward compatibility.
|
|
39
|
+
*/
|
|
40
|
+
export declare function wrapSuccess<T>(payload: T, source?: EnvelopeSource): GlobalEnvelope<T>;
|
|
41
|
+
/**
|
|
42
|
+
* Sanitize any error (including raw Axios errors) into a structured GlobalEnvelope
|
|
43
|
+
* that NEVER leaks raw Axios/HTTP error strings ("Request failed with status code ...").
|
|
44
|
+
*/
|
|
45
|
+
export declare function wrapError(err: any, defaultSource?: EnvelopeSource, defaultCourier?: string): GlobalEnvelope<null>;
|
|
46
|
+
export declare function sanitizeMessage(msg: string): string;
|
|
47
|
+
/**
|
|
48
|
+
* Bounded retry with exponential backoff for read-only GET operations.
|
|
49
|
+
* Retries only on 429, 5xx, or network timeouts.
|
|
50
|
+
*/
|
|
51
|
+
export declare function withGetRetry<T>(fn: () => Promise<T>, maxRetries?: number, baseDelayMs?: number): Promise<T>;
|
|
52
|
+
/**
|
|
53
|
+
* Mask Bangladeshi phone numbers for privacy-safe audit logging (e.g., 017****1234).
|
|
54
|
+
*/
|
|
55
|
+
export declare function maskPhone(phone: string): string;
|
package/dist/envelope.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
export class McpToolError extends Error {
|
|
3
|
+
code;
|
|
4
|
+
retryable;
|
|
5
|
+
courier;
|
|
6
|
+
source;
|
|
7
|
+
extraFields;
|
|
8
|
+
constructor(opts) {
|
|
9
|
+
super(opts.message);
|
|
10
|
+
this.name = "McpToolError";
|
|
11
|
+
this.code = opts.code;
|
|
12
|
+
this.retryable = Boolean(opts.retryable);
|
|
13
|
+
this.courier = opts.courier;
|
|
14
|
+
this.source = opts.source || "live_api";
|
|
15
|
+
this.extraFields = opts.extraFields;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Wrap a successful tool payload in the global response envelope while keeping
|
|
20
|
+
* existing top-level fields intact alongside the envelope for backward compatibility.
|
|
21
|
+
*/
|
|
22
|
+
export function wrapSuccess(payload, source = "live_api") {
|
|
23
|
+
const spread = payload && typeof payload === "object" && !Array.isArray(payload)
|
|
24
|
+
? payload
|
|
25
|
+
: { items: payload, success: true };
|
|
26
|
+
return {
|
|
27
|
+
...spread,
|
|
28
|
+
ok: true,
|
|
29
|
+
data: payload,
|
|
30
|
+
error: null,
|
|
31
|
+
meta: {
|
|
32
|
+
request_id: randomUUID(),
|
|
33
|
+
source,
|
|
34
|
+
as_of: new Date().toISOString(),
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Sanitize any error (including raw Axios errors) into a structured GlobalEnvelope
|
|
40
|
+
* that NEVER leaks raw Axios/HTTP error strings ("Request failed with status code ...").
|
|
41
|
+
*/
|
|
42
|
+
export function wrapError(err, defaultSource = "live_api", defaultCourier) {
|
|
43
|
+
if (err instanceof McpToolError) {
|
|
44
|
+
return {
|
|
45
|
+
...(err.extraFields || {}),
|
|
46
|
+
success: false,
|
|
47
|
+
ok: false,
|
|
48
|
+
data: null,
|
|
49
|
+
error: {
|
|
50
|
+
code: err.code,
|
|
51
|
+
message: sanitizeMessage(err.message),
|
|
52
|
+
retryable: err.retryable,
|
|
53
|
+
...(err.courier ? { courier: err.courier } : defaultCourier ? { courier: defaultCourier } : {}),
|
|
54
|
+
},
|
|
55
|
+
meta: {
|
|
56
|
+
request_id: randomUUID(),
|
|
57
|
+
source: err.source || defaultSource,
|
|
58
|
+
as_of: new Date().toISOString(),
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const httpStatus = err?.response?.status;
|
|
63
|
+
const errCode = String(err?.code || "");
|
|
64
|
+
const rawMsg = String(err?.message || "Unexpected operational error");
|
|
65
|
+
let code = "COURIER_UPSTREAM_ERROR";
|
|
66
|
+
let retryable = false;
|
|
67
|
+
let cleanMsg = "Upstream courier gateway error.";
|
|
68
|
+
if (rawMsg.toLowerCase().includes("not configured")) {
|
|
69
|
+
code = "COURIER_NOT_CONFIGURED";
|
|
70
|
+
retryable = false;
|
|
71
|
+
cleanMsg = sanitizeMessage(rawMsg);
|
|
72
|
+
}
|
|
73
|
+
else if (httpStatus === 401 || httpStatus === 403) {
|
|
74
|
+
code = "COURIER_UPSTREAM_ERROR";
|
|
75
|
+
retryable = false;
|
|
76
|
+
cleanMsg = "Courier upstream authentication or access rejected by provider.";
|
|
77
|
+
}
|
|
78
|
+
else if (httpStatus === 404) {
|
|
79
|
+
code = "CONSIGNMENT_NOT_FOUND";
|
|
80
|
+
retryable = false;
|
|
81
|
+
cleanMsg = "Requested consignment or resource was not found in the courier network.";
|
|
82
|
+
}
|
|
83
|
+
else if (httpStatus === 429 ||
|
|
84
|
+
(httpStatus && httpStatus >= 500) ||
|
|
85
|
+
errCode === "ECONNABORTED" ||
|
|
86
|
+
errCode === "ETIMEDOUT" ||
|
|
87
|
+
errCode === "ECONNRESET" ||
|
|
88
|
+
rawMsg.toLowerCase().includes("timeout")) {
|
|
89
|
+
code = "COURIER_UPSTREAM_ERROR";
|
|
90
|
+
retryable = true;
|
|
91
|
+
cleanMsg = "Courier upstream service temporarily unavailable or timed out.";
|
|
92
|
+
}
|
|
93
|
+
else if (httpStatus === 400 || httpStatus === 422 || rawMsg.toLowerCase().includes("invalid")) {
|
|
94
|
+
code = "INVALID_INPUT";
|
|
95
|
+
retryable = false;
|
|
96
|
+
cleanMsg = sanitizeMessage(rawMsg);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
cleanMsg = sanitizeMessage(rawMsg);
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
success: false,
|
|
103
|
+
ok: false,
|
|
104
|
+
data: null,
|
|
105
|
+
error: {
|
|
106
|
+
code,
|
|
107
|
+
message: cleanMsg,
|
|
108
|
+
retryable,
|
|
109
|
+
...(defaultCourier ? { courier: defaultCourier } : {}),
|
|
110
|
+
},
|
|
111
|
+
meta: {
|
|
112
|
+
request_id: randomUUID(),
|
|
113
|
+
source: defaultSource,
|
|
114
|
+
as_of: new Date().toISOString(),
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
export function sanitizeMessage(msg) {
|
|
119
|
+
return String(msg || "Operation failed")
|
|
120
|
+
.replace(/Request failed with status code \d+/gi, "Upstream provider returned an error status")
|
|
121
|
+
.replace(/AxiosError:\s*/gi, "")
|
|
122
|
+
.trim();
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Bounded retry with exponential backoff for read-only GET operations.
|
|
126
|
+
* Retries only on 429, 5xx, or network timeouts.
|
|
127
|
+
*/
|
|
128
|
+
export async function withGetRetry(fn, maxRetries = 2, baseDelayMs = 150) {
|
|
129
|
+
let attempt = 0;
|
|
130
|
+
while (true) {
|
|
131
|
+
try {
|
|
132
|
+
return await fn();
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
const status = err?.response?.status;
|
|
136
|
+
const code = String(err?.code || "");
|
|
137
|
+
const isRetryable = status === 429 ||
|
|
138
|
+
(typeof status === "number" && status >= 500) ||
|
|
139
|
+
code === "ECONNABORTED" ||
|
|
140
|
+
code === "ETIMEDOUT" ||
|
|
141
|
+
code === "ECONNRESET";
|
|
142
|
+
if (!isRetryable || attempt >= maxRetries) {
|
|
143
|
+
throw err;
|
|
144
|
+
}
|
|
145
|
+
attempt++;
|
|
146
|
+
const delay = baseDelayMs * Math.pow(2, attempt - 1);
|
|
147
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Mask Bangladeshi phone numbers for privacy-safe audit logging (e.g., 017****1234).
|
|
153
|
+
*/
|
|
154
|
+
export function maskPhone(phone) {
|
|
155
|
+
const digits = String(phone || "").replace(/\D/g, "");
|
|
156
|
+
if (digits.length < 7)
|
|
157
|
+
return "***";
|
|
158
|
+
return `${digits.slice(0, 3)}****${digits.slice(-4)}`;
|
|
159
|
+
}
|
package/dist/http.d.ts
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { type Express, type Request } from "express";
|
|
2
|
+
import { EnvSource } from "./config.js";
|
|
3
|
+
/**
|
|
4
|
+
* F10 Fix: Pull client courier keys strictly from request headers.
|
|
5
|
+
* Query parameters are completely ignored to prevent credential leakage in URL logs.
|
|
6
|
+
*/
|
|
7
|
+
export declare function keysFromRequest(req: Request): EnvSource;
|
|
8
|
+
export declare const app: Express;
|