keryx 0.46.0 → 0.48.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/README.md +1 -1
- package/actions/status.ts +9 -7
- package/classes/Action.ts +9 -0
- package/config/database.ts +2 -1
- package/config/rateLimit.ts +10 -0
- package/config/server/mcp.ts +13 -0
- package/index.ts +1 -1
- package/initializers/db.ts +28 -0
- package/initializers/mcp.ts +181 -39
- package/initializers/oauth.ts +5 -17
- package/initializers/redis.ts +23 -1
- package/initializers/resque.ts +14 -3
- package/middleware/rateLimit.ts +32 -0
- package/middleware/transaction.ts +3 -1
- package/package.json +7 -5
- package/templates/scaffold/env.example.mustache +16 -1
- package/testing/index.ts +2 -2
- package/util/cli.ts +9 -1
- package/util/connectionString.ts +26 -1
- package/util/mcpServer.ts +353 -38
- package/util/scaffold.ts +24 -3
- package/util/transaction.ts +19 -1
- package/util/zodMixins.ts +2 -0
package/middleware/rateLimit.ts
CHANGED
|
@@ -82,6 +82,38 @@ export async function checkRateLimit(
|
|
|
82
82
|
return { limit, remaining, resetAt };
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Build the HTTP 429 response for a request that exceeded a rate limit checked
|
|
87
|
+
* outside the action pipeline (OAuth endpoints, the MCP endpoint). The body is
|
|
88
|
+
* an OAuth-style `{ error, error_description }` object, and `Retry-After` plus
|
|
89
|
+
* the `X-RateLimit-*` headers tell the client when to try again.
|
|
90
|
+
*
|
|
91
|
+
* @param info - The result of {@link checkRateLimit}; must have `retryAfter` set.
|
|
92
|
+
* @param extraHeaders - Additional headers to include (e.g. CORS headers).
|
|
93
|
+
*/
|
|
94
|
+
export function rateLimitExceededResponse(
|
|
95
|
+
info: RateLimitInfo,
|
|
96
|
+
extraHeaders: Record<string, string> = {},
|
|
97
|
+
): Response {
|
|
98
|
+
return new Response(
|
|
99
|
+
JSON.stringify({
|
|
100
|
+
error: "rate_limit_exceeded",
|
|
101
|
+
error_description: `Rate limit exceeded. Try again in ${info.retryAfter} seconds.`,
|
|
102
|
+
}),
|
|
103
|
+
{
|
|
104
|
+
status: 429,
|
|
105
|
+
headers: {
|
|
106
|
+
"Content-Type": "application/json",
|
|
107
|
+
...extraHeaders,
|
|
108
|
+
"Retry-After": String(info.retryAfter),
|
|
109
|
+
"X-RateLimit-Limit": String(info.limit),
|
|
110
|
+
"X-RateLimit-Remaining": "0",
|
|
111
|
+
"X-RateLimit-Reset": String(info.resetAt),
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
85
117
|
/**
|
|
86
118
|
* Action middleware that enforces per-connection rate limiting. Add to an action's
|
|
87
119
|
* `middleware` array to apply. Throws `ErrorType.CONNECTION_RATE_LIMITED` when exceeded.
|
|
@@ -4,7 +4,7 @@ import { api, logger } from "../api";
|
|
|
4
4
|
import type { ActionMiddleware } from "../classes/Action";
|
|
5
5
|
import type { Connection } from "../classes/Connection";
|
|
6
6
|
import type { TypedError } from "../classes/TypedError";
|
|
7
|
-
import type
|
|
7
|
+
import { assertDatabaseEnabled, type Transaction } from "../util/transaction";
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Action middleware that wraps the entire action execution in a database transaction.
|
|
@@ -50,6 +50,8 @@ export const TransactionMiddleware: ActionMiddleware = {
|
|
|
50
50
|
_params: Record<string, unknown>,
|
|
51
51
|
connection: Connection,
|
|
52
52
|
) => {
|
|
53
|
+
assertDatabaseEnabled("TransactionMiddleware");
|
|
54
|
+
|
|
53
55
|
// If a parent action already opened a transaction, reuse it.
|
|
54
56
|
// Track depth so only the outermost middleware commits/rolls back.
|
|
55
57
|
const depth = (connection.metadata._txDepth as number | undefined) ?? 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "keryx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.48.0",
|
|
4
4
|
"module": "index.ts",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -87,15 +87,17 @@
|
|
|
87
87
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
88
88
|
"@opentelemetry/api": "^1.9.1",
|
|
89
89
|
"@opentelemetry/sdk-metrics": "^2.7.0",
|
|
90
|
+
"@types/ioredis-mock": "^8.2.8",
|
|
91
|
+
"@types/mustache": "^4.2.6",
|
|
92
|
+
"@types/pg": "^8.20.0",
|
|
93
|
+
"commander": "^14.0.3",
|
|
90
94
|
"ioredis": "^5.10.1",
|
|
95
|
+
"ioredis-mock": "^8.13.1",
|
|
91
96
|
"mustache": "^4.2.0",
|
|
92
97
|
"node-resque": "^9.5.2",
|
|
93
98
|
"pg": "^8.20.0",
|
|
94
99
|
"ts-morph": "^28.0.0",
|
|
95
|
-
"typescript": "^7.0.2"
|
|
96
|
-
"commander": "^14.0.3",
|
|
97
|
-
"@types/mustache": "^4.2.6",
|
|
98
|
-
"@types/pg": "^8.20.0"
|
|
100
|
+
"typescript": "^7.0.2"
|
|
99
101
|
},
|
|
100
102
|
"peerDependencies": {
|
|
101
103
|
"drizzle-orm": "^0.45.2",
|
|
@@ -20,12 +20,27 @@ MCP_SERVER_ENABLED=true
|
|
|
20
20
|
SESSION_TTL=86400
|
|
21
21
|
SESSION_COOKIE_NAME="__session"
|
|
22
22
|
|
|
23
|
+
{{#includeDb}}
|
|
23
24
|
DATABASE_URL="postgres://$USER@localhost:5432/{{projectName}}"
|
|
24
25
|
DATABASE_URL_TEST="postgres://$USER@localhost:5432/{{projectName}}-test"
|
|
25
26
|
DATABASE_AUTO_MIGRATE=true
|
|
26
|
-
|
|
27
|
+
{{/includeDb}}
|
|
28
|
+
{{^includeDb}}
|
|
29
|
+
# No database: an empty DATABASE_URL (or "none") runs without PostgreSQL.
|
|
30
|
+
# Set a postgres:// URL here to add one later.
|
|
31
|
+
DATABASE_URL=""
|
|
32
|
+
{{/includeDb}}
|
|
33
|
+
|
|
34
|
+
{{#includeRedis}}
|
|
27
35
|
REDIS_URL="redis://localhost:6379/0"
|
|
28
36
|
REDIS_URL_TEST="redis://localhost:6379/1"
|
|
37
|
+
{{/includeRedis}}
|
|
38
|
+
{{^includeRedis}}
|
|
39
|
+
# In-memory Redis: tasks, PubSub, sessions and caching work without a Redis server,
|
|
40
|
+
# but state lives in this process only and is lost on restart.
|
|
41
|
+
# Use a redis:// URL to run more than one process or keep state across restarts.
|
|
42
|
+
REDIS_URL="memory://"
|
|
43
|
+
{{/includeRedis}}
|
|
29
44
|
|
|
30
45
|
RATE_LIMIT_ENABLED=true
|
|
31
46
|
RATE_LIMIT_WINDOW_MS=60000
|
package/testing/index.ts
CHANGED
|
@@ -65,8 +65,8 @@ export function serverUrl(): string {
|
|
|
65
65
|
* start/stop pair rather than splitting it, or handle cleanup in `afterEach`.
|
|
66
66
|
*
|
|
67
67
|
* @param opts.clearDatabase - Truncate all tables in `beforeAll`. Default `false`.
|
|
68
|
-
* Requires the `db` initializer to be active
|
|
69
|
-
* persistent state.
|
|
68
|
+
* Requires the `db` initializer to be active; a no-op when the database is
|
|
69
|
+
* disabled (empty `DATABASE_URL`). Opt in for tests that mutate persistent state.
|
|
70
70
|
* @param opts.clearRedis - Flush the current Redis DB in `beforeAll`. Default
|
|
71
71
|
* `false`. Requires the `redis` initializer to be active. Opt in for tests
|
|
72
72
|
* that exercise pub/sub so messages from prior tests don't leak in.
|
package/util/cli.ts
CHANGED
|
@@ -39,7 +39,14 @@ export async function buildProgram(opts: {
|
|
|
39
39
|
.description("Scaffold a new Keryx application with project boilerplate")
|
|
40
40
|
.option("-y, --yes", "Skip prompts and use defaults")
|
|
41
41
|
.option("--no-interactive", "Skip prompts and use defaults")
|
|
42
|
-
.option(
|
|
42
|
+
.option(
|
|
43
|
+
"--no-db",
|
|
44
|
+
"Run without PostgreSQL (no database files, empty DATABASE_URL)",
|
|
45
|
+
)
|
|
46
|
+
.option(
|
|
47
|
+
"--no-redis",
|
|
48
|
+
"Run without a Redis server (in-memory Redis, REDIS_URL=memory://)",
|
|
49
|
+
)
|
|
43
50
|
.option("--no-example", "Skip example action")
|
|
44
51
|
.option(
|
|
45
52
|
"--force",
|
|
@@ -52,6 +59,7 @@ export async function buildProgram(opts: {
|
|
|
52
59
|
projectName = projectName || "my-keryx-app";
|
|
53
60
|
options = {
|
|
54
61
|
includeDb: cmdOpts.db !== false,
|
|
62
|
+
includeRedis: cmdOpts.redis !== false,
|
|
55
63
|
includeExample: cmdOpts.example !== false,
|
|
56
64
|
force: cmdOpts.force === true,
|
|
57
65
|
};
|
package/util/connectionString.ts
CHANGED
|
@@ -1,7 +1,32 @@
|
|
|
1
1
|
import { ErrorType, TypedError } from "../classes/TypedError";
|
|
2
2
|
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Whether a Redis connection string selects the in-process, in-memory Redis
|
|
5
|
+
* (`ioredis-mock`) instead of a real server. Any URL with the `memory:` scheme
|
|
6
|
+
* qualifies, e.g. `memory://`.
|
|
7
|
+
*
|
|
8
|
+
* @param connectionString - The configured `REDIS_URL`.
|
|
9
|
+
* @returns `true` for `memory:` URLs.
|
|
10
|
+
*/
|
|
11
|
+
export function isMemoryRedis(connectionString: string) {
|
|
12
|
+
return connectionString.trim().toLowerCase().startsWith("memory:");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Whether a database connection string turns the database off. An empty string
|
|
17
|
+
* or the literal `"none"` (case-insensitive) means "run without Postgres".
|
|
18
|
+
*
|
|
19
|
+
* @param connectionString - The configured `DATABASE_URL`.
|
|
20
|
+
* @returns `true` when no database should be connected.
|
|
21
|
+
*/
|
|
22
|
+
export function isDatabaseDisabled(connectionString: string) {
|
|
23
|
+
const value = connectionString.trim().toLowerCase();
|
|
24
|
+
return value === "" || value === "none";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Strip the password from a connection string for safe logging. Preserves protocol, user, host, port, and path. Unparseable strings are returned as `"(invalid connection string)"`. */
|
|
4
28
|
export function formatConnectionStringForLogging(connectionString: string) {
|
|
29
|
+
if (!URL.canParse(connectionString)) return "(invalid connection string)";
|
|
5
30
|
const connectionStringParsed = new URL(connectionString);
|
|
6
31
|
const connectionStringInfo = `${connectionStringParsed.protocol ? `${connectionStringParsed.protocol}//` : ""}${connectionStringParsed.username ? `${connectionStringParsed.username}@` : ""}${connectionStringParsed.hostname}:${connectionStringParsed.port}${connectionStringParsed.pathname}`;
|
|
7
32
|
return connectionStringInfo;
|