@open-mena-mcp/core 0.0.0-stage → 0.1.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 +13 -2
- package/dist/errors.d.ts +31 -0
- package/dist/errors.js +62 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +19 -0
- package/dist/logger.js +43 -0
- package/dist/logger.js.map +1 -0
- package/dist/rate-limiter.d.ts +24 -0
- package/dist/rate-limiter.js +50 -0
- package/dist/rate-limiter.js.map +1 -0
- package/dist/zod-helpers.d.ts +11 -0
- package/dist/zod-helpers.js +35 -0
- package/dist/zod-helpers.js.map +1 -0
- package/package.json +45 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dinaelsayed99
|
|
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,14 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @open-mena-mcp/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shared building blocks for the open-mena-mcp servers: error handling, a token-bucket rate limiter, a structured logger (writes to stderr, redacts secrets) and Zod helpers.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { McpToolError, wrapTool, createLogger, RateLimiter, parseInput } from "@open-mena-mcp/core";
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
- `McpToolError`, `toolOk`, `toolError`, `wrapTool`: turn thrown errors into MCP tool results with stable error codes.
|
|
10
|
+
- `RateLimiter`: local token bucket (`tryAcquire`, `acquireOrThrow`).
|
|
11
|
+
- `createLogger`: JSON-lines logger on stderr so stdout stays clean for the stdio MCP transport.
|
|
12
|
+
- `parseInput`, `isoDate`, `requireEnv`: Zod helpers with readable `INVALID_INPUT` and `AUTH_MISSING` errors.
|
|
13
|
+
|
|
14
|
+
Requires Node.js 20 or newer. MIT licensed. Part of [open-mena-mcp](https://github.com/Dinaelsayed99/open-mena-mcp).
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Error codes shared by all open-mena-mcp servers. */
|
|
2
|
+
export type McpErrorCode = "INVALID_INPUT" | "AUTH_MISSING" | "AUTH_FAILED" | "NOT_FOUND" | "RATE_LIMITED" | "UPSTREAM_ERROR" | "TIMEOUT" | "INTERNAL";
|
|
3
|
+
export declare class McpToolError extends Error {
|
|
4
|
+
readonly code: McpErrorCode;
|
|
5
|
+
readonly status?: number;
|
|
6
|
+
readonly details?: unknown;
|
|
7
|
+
constructor(code: McpErrorCode, message: string, options?: {
|
|
8
|
+
status?: number;
|
|
9
|
+
details?: unknown;
|
|
10
|
+
cause?: unknown;
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
/** Shape of an MCP tool result (compatible with the official SDK's CallToolResult). */
|
|
14
|
+
export interface ToolResult {
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
content: {
|
|
17
|
+
type: "text";
|
|
18
|
+
text: string;
|
|
19
|
+
}[];
|
|
20
|
+
isError?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/** Map an HTTP status code to an error code. */
|
|
23
|
+
export declare function codeFromHttpStatus(status: number): McpErrorCode;
|
|
24
|
+
/** Convert anything thrown into an McpToolError. Never leaks stack traces. */
|
|
25
|
+
export declare function toMcpToolError(err: unknown): McpToolError;
|
|
26
|
+
/** Build a successful tool result from JSON-serialisable data. */
|
|
27
|
+
export declare function toolOk(data: unknown): ToolResult;
|
|
28
|
+
/** Build an error tool result for the MCP client. */
|
|
29
|
+
export declare function toolError(err: unknown): ToolResult;
|
|
30
|
+
/** Wrap a tool handler so thrown errors become MCP error results. */
|
|
31
|
+
export declare function wrapTool<A>(handler: (args: A) => Promise<unknown> | unknown): (args: A) => Promise<ToolResult>;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export class McpToolError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
status;
|
|
4
|
+
details;
|
|
5
|
+
constructor(code, message, options = {}) {
|
|
6
|
+
super(message, options.cause === undefined ? undefined : { cause: options.cause });
|
|
7
|
+
this.name = "McpToolError";
|
|
8
|
+
this.code = code;
|
|
9
|
+
if (options.status !== undefined)
|
|
10
|
+
this.status = options.status;
|
|
11
|
+
if (options.details !== undefined)
|
|
12
|
+
this.details = options.details;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/** Map an HTTP status code to an error code. */
|
|
16
|
+
export function codeFromHttpStatus(status) {
|
|
17
|
+
if (status === 401 || status === 403)
|
|
18
|
+
return "AUTH_FAILED";
|
|
19
|
+
if (status === 404)
|
|
20
|
+
return "NOT_FOUND";
|
|
21
|
+
if (status === 429)
|
|
22
|
+
return "RATE_LIMITED";
|
|
23
|
+
if (status === 400 || status === 422)
|
|
24
|
+
return "INVALID_INPUT";
|
|
25
|
+
if (status === 408 || status === 504)
|
|
26
|
+
return "TIMEOUT";
|
|
27
|
+
return "UPSTREAM_ERROR";
|
|
28
|
+
}
|
|
29
|
+
/** Convert anything thrown into an McpToolError. Never leaks stack traces. */
|
|
30
|
+
export function toMcpToolError(err) {
|
|
31
|
+
if (err instanceof McpToolError)
|
|
32
|
+
return err;
|
|
33
|
+
if (err instanceof Error) {
|
|
34
|
+
return new McpToolError("INTERNAL", err.message, { cause: err });
|
|
35
|
+
}
|
|
36
|
+
return new McpToolError("INTERNAL", "Unknown error", { details: String(err) });
|
|
37
|
+
}
|
|
38
|
+
/** Build a successful tool result from JSON-serialisable data. */
|
|
39
|
+
export function toolOk(data) {
|
|
40
|
+
const text = typeof data === "string" ? data : JSON.stringify(data, null, 2);
|
|
41
|
+
return { content: [{ type: "text", text }] };
|
|
42
|
+
}
|
|
43
|
+
/** Build an error tool result for the MCP client. */
|
|
44
|
+
export function toolError(err) {
|
|
45
|
+
const e = toMcpToolError(err);
|
|
46
|
+
const payload = { code: e.code, message: e.message };
|
|
47
|
+
if (e.status !== undefined)
|
|
48
|
+
payload.status = e.status;
|
|
49
|
+
return { isError: true, content: [{ type: "text", text: JSON.stringify(payload) }] };
|
|
50
|
+
}
|
|
51
|
+
/** Wrap a tool handler so thrown errors become MCP error results. */
|
|
52
|
+
export function wrapTool(handler) {
|
|
53
|
+
return async (args) => {
|
|
54
|
+
try {
|
|
55
|
+
return toolOk(await handler(args));
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
return toolError(err);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,CAAC,CAAC;QACrC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC;QACxB,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC"}
|
package/dist/logger.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
2
|
+
export interface LoggerOptions {
|
|
3
|
+
level?: LogLevel;
|
|
4
|
+
name?: string;
|
|
5
|
+
/** Output sink. Defaults to stderr so stdout stays clean for the stdio MCP transport. */
|
|
6
|
+
write?: (line: string) => void;
|
|
7
|
+
now?: () => Date;
|
|
8
|
+
}
|
|
9
|
+
export interface Logger {
|
|
10
|
+
debug(msg: string, fields?: Record<string, unknown>): void;
|
|
11
|
+
info(msg: string, fields?: Record<string, unknown>): void;
|
|
12
|
+
warn(msg: string, fields?: Record<string, unknown>): void;
|
|
13
|
+
error(msg: string, fields?: Record<string, unknown>): void;
|
|
14
|
+
child(name: string): Logger;
|
|
15
|
+
}
|
|
16
|
+
/** Replace values of secret-looking keys, recursively. */
|
|
17
|
+
export declare function redact(value: unknown, depth?: number): unknown;
|
|
18
|
+
/** JSON-lines structured logger that writes to stderr. */
|
|
19
|
+
export declare function createLogger(options?: LoggerOptions): Logger;
|
package/dist/logger.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const ORDER = { debug: 10, info: 20, warn: 30, error: 40 };
|
|
2
|
+
const SECRET_KEY = /(authorization|api[-_]?key|secret|token|password|passwd)/i;
|
|
3
|
+
/** Replace values of secret-looking keys, recursively. */
|
|
4
|
+
export function redact(value, depth = 0) {
|
|
5
|
+
if (depth > 5 || value === null || typeof value !== "object")
|
|
6
|
+
return value;
|
|
7
|
+
if (Array.isArray(value))
|
|
8
|
+
return value.map((v) => redact(v, depth + 1));
|
|
9
|
+
const out = {};
|
|
10
|
+
for (const [k, v] of Object.entries(value)) {
|
|
11
|
+
out[k] = SECRET_KEY.test(k) ? "[REDACTED]" : redact(v, depth + 1);
|
|
12
|
+
}
|
|
13
|
+
return out;
|
|
14
|
+
}
|
|
15
|
+
/** JSON-lines structured logger that writes to stderr. */
|
|
16
|
+
export function createLogger(options = {}) {
|
|
17
|
+
const min = ORDER[options.level ?? "info"];
|
|
18
|
+
const write = options.write ?? ((line) => process.stderr.write(line + "\n"));
|
|
19
|
+
const now = options.now ?? (() => new Date());
|
|
20
|
+
const make = (name) => {
|
|
21
|
+
const log = (level, msg, fields) => {
|
|
22
|
+
if (ORDER[level] < min)
|
|
23
|
+
return;
|
|
24
|
+
const entry = {
|
|
25
|
+
time: now().toISOString(),
|
|
26
|
+
level,
|
|
27
|
+
...(name ? { logger: name } : {}),
|
|
28
|
+
msg,
|
|
29
|
+
...(fields ? redact(fields) : {}),
|
|
30
|
+
};
|
|
31
|
+
write(JSON.stringify(entry));
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
debug: (m, f) => log("debug", m, f),
|
|
35
|
+
info: (m, f) => log("info", m, f),
|
|
36
|
+
warn: (m, f) => log("warn", m, f),
|
|
37
|
+
error: (m, f) => log("error", m, f),
|
|
38
|
+
child: (n) => make(name ? `${name}.${n}` : n),
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
return make(options.name);
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAEA,MAAM,KAAK,GAA6B,EAAE,KAAK,EAAE,EAAE,EAAE,I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CAAC,IAAI,CAAC,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface RateLimiterOptions {
|
|
2
|
+
/** Maximum burst size (bucket capacity). */
|
|
3
|
+
capacity: number;
|
|
4
|
+
/** Tokens added per second. */
|
|
5
|
+
refillPerSecond: number;
|
|
6
|
+
/** Injectable clock in ms, for tests. */
|
|
7
|
+
now?: () => number;
|
|
8
|
+
}
|
|
9
|
+
/** Token bucket rate limiter. */
|
|
10
|
+
export declare class RateLimiter {
|
|
11
|
+
private tokens;
|
|
12
|
+
private last;
|
|
13
|
+
private readonly capacity;
|
|
14
|
+
private readonly refillPerSecond;
|
|
15
|
+
private readonly now;
|
|
16
|
+
constructor(options: RateLimiterOptions);
|
|
17
|
+
private refill;
|
|
18
|
+
/** Take a token if available. Returns false when limited. */
|
|
19
|
+
tryAcquire(): boolean;
|
|
20
|
+
/** Milliseconds until the next token is available (0 if available now). */
|
|
21
|
+
msUntilAvailable(): number;
|
|
22
|
+
/** Take a token or throw a RATE_LIMITED error. */
|
|
23
|
+
acquireOrThrow(): void;
|
|
24
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { McpToolError } from "./errors.js";
|
|
2
|
+
/** Token bucket rate limiter. */
|
|
3
|
+
export class RateLimiter {
|
|
4
|
+
tokens;
|
|
5
|
+
last;
|
|
6
|
+
capacity;
|
|
7
|
+
refillPerSecond;
|
|
8
|
+
now;
|
|
9
|
+
constructor(options) {
|
|
10
|
+
if (options.capacity <= 0 || options.refillPerSecond <= 0) {
|
|
11
|
+
throw new McpToolError("INVALID_INPUT", "capacity and refillPerSecond must be positive");
|
|
12
|
+
}
|
|
13
|
+
this.capacity = options.capacity;
|
|
14
|
+
this.refillPerSecond = options.refillPerSecond;
|
|
15
|
+
this.now = options.now ?? Date.now;
|
|
16
|
+
this.tokens = options.capacity;
|
|
17
|
+
this.last = this.now();
|
|
18
|
+
}
|
|
19
|
+
refill() {
|
|
20
|
+
const t = this.now();
|
|
21
|
+
const elapsed = Math.max(0, t - this.last) / 1000;
|
|
22
|
+
this.tokens = Math.min(this.capacity, this.tokens + elapsed * this.refillPerSecond);
|
|
23
|
+
this.last = t;
|
|
24
|
+
}
|
|
25
|
+
/** Take a token if available. Returns false when limited. */
|
|
26
|
+
tryAcquire() {
|
|
27
|
+
this.refill();
|
|
28
|
+
if (this.tokens >= 1) {
|
|
29
|
+
this.tokens -= 1;
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
/** Milliseconds until the next token is available (0 if available now). */
|
|
35
|
+
msUntilAvailable() {
|
|
36
|
+
this.refill();
|
|
37
|
+
if (this.tokens >= 1)
|
|
38
|
+
return 0;
|
|
39
|
+
return Math.ceil(((1 - this.tokens) / this.refillPerSecond) * 1000);
|
|
40
|
+
}
|
|
41
|
+
/** Take a token or throw a RATE_LIMITED error. */
|
|
42
|
+
acquireOrThrow() {
|
|
43
|
+
if (!this.tryAcquire()) {
|
|
44
|
+
throw new McpToolError("RATE_LIMITED", "Local rate limit exceeded", {
|
|
45
|
+
details: { retryAfterMs: this.msUntilAvailable() },
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=rate-limiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limiter.js","sourceRoot":"","sources":["../src/rate-limiter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAW3C,iCAAiC;AACjC,MAAM,OAAO,WAAW;IACd,MAAM,CAAS;IACf,IAAI,CAAS;IACJ,QAAQ,CAAS;IACjB,eAAe,CAAS;IACxB,GAAG,CAAe;IAEnC,YAAY,OAA2B;QACrC,IAAI,OAAO,CAAC,QAAQ,IAAI,CAAC,IAAI,OAAO,CAAC,eAAe,IAAI,CAAC,EAAE,CAAC;YAC1D,MAAM,IAA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,IAAI,CAAC,CAAC;IACtE,CAAC;IAED,kDAAkD;IAClD,cAAc;QACZ,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC;YACvB,MAAM,IAAI,YAAY,CAAC,cAAc,EAAE,2BAA2B,EAAE;gBAClE,OAAO,EAAE,EAAE,YAAY,EAAE,IAAI,CAAC,gBAAgB,EAAE,EAAE;aACnD,CAAC,CAAC;QACL,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/** String field with a required description (shown to the LLM client). */
|
|
3
|
+
export declare function described<T extends z.ZodTypeAny>(schema: T, description: string): T;
|
|
4
|
+
/** ISO date string YYYY-MM-DD. */
|
|
5
|
+
export declare const isoDate: z.ZodString;
|
|
6
|
+
/** Positive integer with optional max. */
|
|
7
|
+
export declare function positiveInt(max?: number): z.ZodNumber;
|
|
8
|
+
/** Parse input with a schema, throwing a readable INVALID_INPUT error. */
|
|
9
|
+
export declare function parseInput<S extends z.ZodTypeAny>(schema: S, input: unknown): z.infer<S>;
|
|
10
|
+
/** Read a required environment variable, throwing AUTH_MISSING if absent. */
|
|
11
|
+
export declare function requireEnv(name: string, env?: Record<string, string | undefined>): string;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { McpToolError } from "./errors.js";
|
|
3
|
+
/** String field with a required description (shown to the LLM client). */
|
|
4
|
+
export function described(schema, description) {
|
|
5
|
+
return schema.describe(description);
|
|
6
|
+
}
|
|
7
|
+
/** ISO date string YYYY-MM-DD. */
|
|
8
|
+
export const isoDate = z
|
|
9
|
+
.string()
|
|
10
|
+
.regex(/^\d{4}-\d{2}-\d{2}$/, "Expected a date in YYYY-MM-DD format");
|
|
11
|
+
/** Positive integer with optional max. */
|
|
12
|
+
export function positiveInt(max) {
|
|
13
|
+
const base = z.number().int().positive();
|
|
14
|
+
return max === undefined ? base : base.max(max);
|
|
15
|
+
}
|
|
16
|
+
/** Parse input with a schema, throwing a readable INVALID_INPUT error. */
|
|
17
|
+
export function parseInput(schema, input) {
|
|
18
|
+
const result = schema.safeParse(input);
|
|
19
|
+
if (!result.success) {
|
|
20
|
+
const message = result.error.issues
|
|
21
|
+
.map((i) => `${i.path.join(".") || "input"}: ${i.message}`)
|
|
22
|
+
.join("; ");
|
|
23
|
+
throw new McpToolError("INVALID_INPUT", message, { details: result.error.issues });
|
|
24
|
+
}
|
|
25
|
+
return result.data;
|
|
26
|
+
}
|
|
27
|
+
/** Read a required environment variable, throwing AUTH_MISSING if absent. */
|
|
28
|
+
export function requireEnv(name, env = process.env) {
|
|
29
|
+
const v = env[name];
|
|
30
|
+
if (!v) {
|
|
31
|
+
throw new McpToolError("AUTH_MISSING", `Missing required environment variable ${name}`);
|
|
32
|
+
}
|
|
33
|
+
return v;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=zod-helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-helpers.js","sourceRoot":"","sources":["../src/zod-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,0EAA0E;AAC1E,MAAM,UAAU,SAAS,CAAyB,MAAS,EAAE,WAAmB;IAC9E,OAAO,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAM,CAAC;AAC3C,CAAC;AAED,kCAAkC;AAClC,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC;KACrB,MAAM,EAAE;KACR,KAAK,CAAC,qBAAqB,EAAE,sCAAsC,CAAC,CAAC;AAExE,0CAA0C;AAC1C,MAAM,UAAU,WAAW,CAAC,GAAY;IACtC,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC;IACzC,OAAO,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAClD,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,UAAU,CAAyB,MAAS,EAAE,KAAc;IAC1E,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IACvC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM;aAChC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,OAAO,KAAK,CAAC,CAAC,OAAO,EAAE,CAAC;aAC1D,IAAI,CAAC,IAAI,CAAC,CAAC;QACd,MAAM,IAAI,YAAY,CAAC,eAAe,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;IACrF,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC;AACrB,CAAC;AAED,6EAA6E;AAC7E,MAAM,UAAU,UAAU,CACxB,IAAY,EACZ,MAA0C,OAAO,CAAC,GAAG;IAErD,MAAM,CAAC,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC;IACpB,IAAI,CAAC,CAAC,EAAE,CAAC;QACP,MAAM,IAAI,YAAY,CAAC,cAAc,EAAE,yCAAyC,IAAI,EAAE,CAAC,CAAC;IAC1F,CAAC;IACD,OAAO,CAAC,CAAC;AACX,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mena-mcp/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared MCP error handling, rate limiter, structured logger and Zod helpers for open-mena-mcp servers",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"LICENSE",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/Dinaelsayed99/open-mena-mcp.git",
|
|
23
|
+
"directory": "packages/core"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"zod": "^3.25.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"typescript": "^5.5.0",
|
|
33
|
+
"vitest": "^4.1.12",
|
|
34
|
+
"@types/node": "^20.14.0"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/Dinaelsayed99/open-mena-mcp/tree/main/packages/core#readme",
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/Dinaelsayed99/open-mena-mcp/issues"
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=20"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsc -p tsconfig.build.json",
|
|
45
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
46
|
+
"test": "vitest run"
|
|
47
|
+
}
|
|
6
48
|
}
|