@gizmodata/hardcharger-mcp 0.0.0 → 2.0.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/CHANGELOG.md +119 -0
- package/LICENSE +201 -0
- package/NOTICE +8 -0
- package/README.md +486 -3
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +75 -0
- package/dist/cli.js.map +1 -0
- package/dist/connection.d.ts +260 -0
- package/dist/connection.js +797 -0
- package/dist/connection.js.map +1 -0
- package/dist/format.d.ts +56 -0
- package/dist/format.js +240 -0
- package/dist/format.js.map +1 -0
- package/dist/oauth-facade.d.ts +50 -0
- package/dist/oauth-facade.js +223 -0
- package/dist/oauth-facade.js.map +1 -0
- package/dist/oauth.d.ts +94 -0
- package/dist/oauth.js +228 -0
- package/dist/oauth.js.map +1 -0
- package/dist/params.d.ts +61 -0
- package/dist/params.js +200 -0
- package/dist/params.js.map +1 -0
- package/dist/registry.d.ts +45 -0
- package/dist/registry.js +91 -0
- package/dist/registry.js.map +1 -0
- package/dist/server.d.ts +38 -0
- package/dist/server.js +753 -0
- package/dist/server.js.map +1 -0
- package/dist/sessions.d.ts +52 -0
- package/dist/sessions.js +113 -0
- package/dist/sessions.js.map +1 -0
- package/dist/sql-guard.d.ts +62 -0
- package/dist/sql-guard.js +460 -0
- package/dist/sql-guard.js.map +1 -0
- package/dist/sso.d.ts +29 -0
- package/dist/sso.js +145 -0
- package/dist/sso.js.map +1 -0
- package/dist/transports.d.ts +29 -0
- package/dist/transports.js +308 -0
- package/dist/transports.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +8 -0
- package/dist/version.js.map +1 -0
- package/package.json +65 -9
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauth-facade.js","sourceRoot":"","sources":["../src/oauth-facade.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,EAAE;AACF,mEAAmE;AACnE,sEAAsE;AACtE,0EAA0E;AAC1E,4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,CAAC,MAAM,CAAC,cAAc,EAAE,IAAI,CAAC,SAAS,CAAC;iBACxE,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE;gBACZ,IAAI,CAAC,MAAM,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;gBACtC,OAAO,GAAG,CAAC;YACb,CAAC,CAAC;iBACD,OAAO,CAAC,GAAG,EAAE;gBACZ,IAAI,CAAC,OAAO,GAAG,SAAS,CAAC;YAC3B,CAAC,CAAC,CAAC;QACP,CAAC;QACD,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF"}
|
package/dist/oauth.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { type JWTPayload, type JWTVerifyGetKey } from "jose";
|
|
2
|
+
export interface OAuthConfig {
|
|
3
|
+
/** Canonical public URL of the MCP endpoint, e.g. https://mcp.example.com/mcp. */
|
|
4
|
+
publicUrl: string;
|
|
5
|
+
/** Issuer URL of the OpenID Connect provider (the `iss` claim). */
|
|
6
|
+
issuer: string;
|
|
7
|
+
/** Accepted `aud` values; a token must carry at least one of them. */
|
|
8
|
+
audiences: string[];
|
|
9
|
+
/** JWKS endpoint; discovered from the issuer when unset. */
|
|
10
|
+
jwksUri?: string;
|
|
11
|
+
/** Scopes advertised to clients (`scopes_supported`) and requested on a 401. */
|
|
12
|
+
scopes: string[];
|
|
13
|
+
/** Claims tried in order to name the caller (first non-empty string wins). */
|
|
14
|
+
userClaims: string[];
|
|
15
|
+
/** Glob patterns (`*@example.com`, `alice@example.com`); empty = everyone the provider signs in. */
|
|
16
|
+
authorizedEmails: string[];
|
|
17
|
+
/**
|
|
18
|
+
* Publish an authorization-server facade and proxy the token endpoint so
|
|
19
|
+
* refresh_token grants carry the API scope (needed for Microsoft Entra ID).
|
|
20
|
+
*/
|
|
21
|
+
tokenProxy?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/** Identity derived from a verified access token. */
|
|
24
|
+
export interface AuthenticatedUser {
|
|
25
|
+
/** The `sub` claim. */
|
|
26
|
+
subject: string;
|
|
27
|
+
/** Display name: the first configured user claim that is present. */
|
|
28
|
+
name: string;
|
|
29
|
+
/** Email-like address if any of `email`, `preferred_username` or `upn` looks like one. */
|
|
30
|
+
email?: string;
|
|
31
|
+
claims: JWTPayload;
|
|
32
|
+
}
|
|
33
|
+
export type AuthFailureCode = "invalid_token" | "forbidden";
|
|
34
|
+
export declare class OAuthError extends Error {
|
|
35
|
+
readonly code: AuthFailureCode;
|
|
36
|
+
constructor(code: AuthFailureCode, message: string);
|
|
37
|
+
/** HTTP status the transport should answer with. */
|
|
38
|
+
get status(): 401 | 403;
|
|
39
|
+
}
|
|
40
|
+
/** Asymmetric algorithms only: a symmetric alg would let the JWKS-hosted public key act as a secret. */
|
|
41
|
+
export declare const ALLOWED_ALGORITHMS: string[];
|
|
42
|
+
/**
|
|
43
|
+
* OpenID Connect scope that asks the provider for a refresh token. Claude
|
|
44
|
+
* requests exactly the scopes named in the WWW-Authenticate challenge, so it
|
|
45
|
+
* must be advertised here or the client cannot outlive the access token.
|
|
46
|
+
*/
|
|
47
|
+
export declare const OFFLINE_ACCESS_SCOPE = "offline_access";
|
|
48
|
+
/** Tolerated clock skew between the provider and this host, in seconds. */
|
|
49
|
+
export declare const CLOCK_TOLERANCE_SECONDS = 60;
|
|
50
|
+
/** Well-known path of the protected-resource metadata, without and with the endpoint path (RFC 9728 §3.1). */
|
|
51
|
+
export declare function protectedResourceMetadataPaths(publicUrl: string): string[];
|
|
52
|
+
/** URL of the protected-resource metadata document (the path-suffixed form, which clients try first). */
|
|
53
|
+
export declare function protectedResourceMetadataUrl(publicUrl: string): string;
|
|
54
|
+
/** Candidate discovery documents for an issuer: OpenID Connect first, then RFC 8414 (both path placements). */
|
|
55
|
+
export declare function discoveryUrls(issuer: string): string[];
|
|
56
|
+
/** Case-insensitive glob match where `*` matches any run of characters. */
|
|
57
|
+
export declare function emailMatches(email: string, pattern: string): boolean;
|
|
58
|
+
/** Builds the identity for a verified token payload. */
|
|
59
|
+
export declare function userFromClaims(claims: JWTPayload, userClaims: string[]): AuthenticatedUser;
|
|
60
|
+
export interface VerifierOptions {
|
|
61
|
+
/** Test hook: replaces global fetch for discovery. */
|
|
62
|
+
fetch?: typeof fetch;
|
|
63
|
+
/** Test hook: replaces the remote JWKS key resolver. */
|
|
64
|
+
getKey?: JWTVerifyGetKey;
|
|
65
|
+
log?: (message: string) => void;
|
|
66
|
+
}
|
|
67
|
+
/** Verifies bearer tokens against one OpenID Connect provider. */
|
|
68
|
+
export declare class OAuthVerifier {
|
|
69
|
+
readonly config: OAuthConfig;
|
|
70
|
+
private keyResolver;
|
|
71
|
+
private discovering;
|
|
72
|
+
private readonly fetchImpl;
|
|
73
|
+
private readonly log;
|
|
74
|
+
constructor(config: OAuthConfig, options?: VerifierOptions);
|
|
75
|
+
/** RFC 9728 protected-resource metadata document; `authorizationServer` overrides the issuer (token-proxy facade). */
|
|
76
|
+
protectedResourceMetadata(authorizationServer?: string): Record<string, unknown>;
|
|
77
|
+
/** `WWW-Authenticate` value for a 401 (RFC 6750 §3 + RFC 9728 §5.1). */
|
|
78
|
+
challenge(error?: AuthFailureCode, description?: string): string;
|
|
79
|
+
/**
|
|
80
|
+
* Validates a bearer token and returns the caller. Throws OAuthError with
|
|
81
|
+
* code `invalid_token` (401) for a missing, malformed, expired or
|
|
82
|
+
* mis-issued token and `forbidden` (403) when the caller is not on the
|
|
83
|
+
* email allowlist.
|
|
84
|
+
*/
|
|
85
|
+
verify(authorization: string | undefined): Promise<AuthenticatedUser>;
|
|
86
|
+
private resolveKeys;
|
|
87
|
+
/** Returns the JWKS URI: configured, or read from the provider's discovery document. */
|
|
88
|
+
private discoverJwks;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Fetches the provider's discovery document (OpenID Connect first, then RFC
|
|
92
|
+
* 8414). The document must name the configured issuer and a jwks_uri.
|
|
93
|
+
*/
|
|
94
|
+
export declare function discoverMetadata(issuer: string, fetchImpl?: typeof fetch): Promise<Record<string, unknown>>;
|
package/dist/oauth.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// OAuth 2.1 resource-server support for the Streamable HTTP transport.
|
|
2
|
+
//
|
|
3
|
+
// The MCP server never issues tokens itself. An OpenID Connect provider
|
|
4
|
+
// (Microsoft Entra ID, Okta, Auth0, Keycloak, Cognito, Clerk, ...) is the
|
|
5
|
+
// authorization server; this module
|
|
6
|
+
// - discovers the provider's JWKS from its issuer URL,
|
|
7
|
+
// - verifies the bearer token on every request (signature, issuer,
|
|
8
|
+
// audience, expiry) and derives the caller's identity from its claims,
|
|
9
|
+
// - optionally restricts callers to an email allowlist, and
|
|
10
|
+
// - renders the RFC 9728 protected-resource metadata and the
|
|
11
|
+
// WWW-Authenticate challenge that let MCP clients such as Claude.ai
|
|
12
|
+
// find the authorization server on their own.
|
|
13
|
+
//
|
|
14
|
+
// The Hardcharger connection itself keeps using the configured service
|
|
15
|
+
// credentials: the caller's token is validated here and never forwarded.
|
|
16
|
+
import { createRemoteJWKSet, jwtVerify } from "jose";
|
|
17
|
+
export class OAuthError extends Error {
|
|
18
|
+
code;
|
|
19
|
+
constructor(code, message) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.code = code;
|
|
22
|
+
this.name = "OAuthError";
|
|
23
|
+
}
|
|
24
|
+
/** HTTP status the transport should answer with. */
|
|
25
|
+
get status() {
|
|
26
|
+
return this.code === "forbidden" ? 403 : 401;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Asymmetric algorithms only: a symmetric alg would let the JWKS-hosted public key act as a secret. */
|
|
30
|
+
export const ALLOWED_ALGORITHMS = ["RS256", "RS384", "RS512", "PS256", "PS384", "PS512", "ES256", "ES384", "ES512", "EdDSA"];
|
|
31
|
+
/**
|
|
32
|
+
* OpenID Connect scope that asks the provider for a refresh token. Claude
|
|
33
|
+
* requests exactly the scopes named in the WWW-Authenticate challenge, so it
|
|
34
|
+
* must be advertised here or the client cannot outlive the access token.
|
|
35
|
+
*/
|
|
36
|
+
export const OFFLINE_ACCESS_SCOPE = "offline_access";
|
|
37
|
+
/** Tolerated clock skew between the provider and this host, in seconds. */
|
|
38
|
+
export const CLOCK_TOLERANCE_SECONDS = 60;
|
|
39
|
+
/** Well-known path of the protected-resource metadata, without and with the endpoint path (RFC 9728 §3.1). */
|
|
40
|
+
export function protectedResourceMetadataPaths(publicUrl) {
|
|
41
|
+
const path = new URL(publicUrl).pathname.replace(/\/+$/u, "");
|
|
42
|
+
const base = "/.well-known/oauth-protected-resource";
|
|
43
|
+
return path && path !== "/" ? [base, `${base}${path}`] : [base];
|
|
44
|
+
}
|
|
45
|
+
/** URL of the protected-resource metadata document (the path-suffixed form, which clients try first). */
|
|
46
|
+
export function protectedResourceMetadataUrl(publicUrl) {
|
|
47
|
+
const u = new URL(publicUrl);
|
|
48
|
+
const paths = protectedResourceMetadataPaths(publicUrl);
|
|
49
|
+
return `${u.origin}${paths[paths.length - 1]}`;
|
|
50
|
+
}
|
|
51
|
+
/** Candidate discovery documents for an issuer: OpenID Connect first, then RFC 8414 (both path placements). */
|
|
52
|
+
export function discoveryUrls(issuer) {
|
|
53
|
+
const u = new URL(issuer);
|
|
54
|
+
const path = u.pathname.replace(/\/+$/u, "");
|
|
55
|
+
const urls = [`${u.origin}${path}/.well-known/openid-configuration`];
|
|
56
|
+
if (path) {
|
|
57
|
+
urls.push(`${u.origin}/.well-known/oauth-authorization-server${path}`);
|
|
58
|
+
urls.push(`${u.origin}${path}/.well-known/oauth-authorization-server`);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
urls.push(`${u.origin}/.well-known/oauth-authorization-server`);
|
|
62
|
+
}
|
|
63
|
+
return urls;
|
|
64
|
+
}
|
|
65
|
+
/** Case-insensitive glob match where `*` matches any run of characters. */
|
|
66
|
+
export function emailMatches(email, pattern) {
|
|
67
|
+
const re = new RegExp("^" +
|
|
68
|
+
pattern
|
|
69
|
+
.trim()
|
|
70
|
+
.toLowerCase()
|
|
71
|
+
.split("*")
|
|
72
|
+
.map((s) => s.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&"))
|
|
73
|
+
.join(".*") +
|
|
74
|
+
"$", "u");
|
|
75
|
+
return re.test(email.trim().toLowerCase());
|
|
76
|
+
}
|
|
77
|
+
const EMAIL_CLAIMS = ["email", "preferred_username", "upn"];
|
|
78
|
+
function firstString(claims, names) {
|
|
79
|
+
for (const n of names) {
|
|
80
|
+
const v = claims[n];
|
|
81
|
+
if (typeof v === "string" && v.trim() !== "")
|
|
82
|
+
return v.trim();
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
/** Builds the identity for a verified token payload. */
|
|
87
|
+
export function userFromClaims(claims, userClaims) {
|
|
88
|
+
const subject = typeof claims.sub === "string" ? claims.sub : "";
|
|
89
|
+
const emailish = EMAIL_CLAIMS.map((n) => claims[n]).find((v) => typeof v === "string" && v.includes("@"));
|
|
90
|
+
return {
|
|
91
|
+
subject,
|
|
92
|
+
name: firstString(claims, userClaims) ?? subject ?? "unknown",
|
|
93
|
+
email: emailish?.trim(),
|
|
94
|
+
claims,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/** Verifies bearer tokens against one OpenID Connect provider. */
|
|
98
|
+
export class OAuthVerifier {
|
|
99
|
+
config;
|
|
100
|
+
keyResolver;
|
|
101
|
+
discovering;
|
|
102
|
+
fetchImpl;
|
|
103
|
+
log;
|
|
104
|
+
constructor(config, options = {}) {
|
|
105
|
+
this.config = config;
|
|
106
|
+
this.fetchImpl = options.fetch ?? fetch;
|
|
107
|
+
this.log = options.log ?? ((m) => console.error(m));
|
|
108
|
+
if (options.getKey)
|
|
109
|
+
this.keyResolver = options.getKey;
|
|
110
|
+
}
|
|
111
|
+
/** RFC 9728 protected-resource metadata document; `authorizationServer` overrides the issuer (token-proxy facade). */
|
|
112
|
+
protectedResourceMetadata(authorizationServer = this.config.issuer) {
|
|
113
|
+
const doc = {
|
|
114
|
+
resource: this.config.publicUrl,
|
|
115
|
+
authorization_servers: [authorizationServer],
|
|
116
|
+
bearer_methods_supported: ["header"],
|
|
117
|
+
};
|
|
118
|
+
if (this.config.scopes.length > 0)
|
|
119
|
+
doc.scopes_supported = this.config.scopes;
|
|
120
|
+
return doc;
|
|
121
|
+
}
|
|
122
|
+
/** `WWW-Authenticate` value for a 401 (RFC 6750 §3 + RFC 9728 §5.1). */
|
|
123
|
+
challenge(error, description) {
|
|
124
|
+
const parts = [];
|
|
125
|
+
if (error)
|
|
126
|
+
parts.push(`error="${error}"`);
|
|
127
|
+
if (description)
|
|
128
|
+
parts.push(`error_description="${description.replace(/["\\\r\n]/gu, " ")}"`);
|
|
129
|
+
parts.push(`resource_metadata="${protectedResourceMetadataUrl(this.config.publicUrl)}"`);
|
|
130
|
+
if (this.config.scopes.length > 0)
|
|
131
|
+
parts.push(`scope="${this.config.scopes.join(" ")}"`);
|
|
132
|
+
return `Bearer ${parts.join(", ")}`;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Validates a bearer token and returns the caller. Throws OAuthError with
|
|
136
|
+
* code `invalid_token` (401) for a missing, malformed, expired or
|
|
137
|
+
* mis-issued token and `forbidden` (403) when the caller is not on the
|
|
138
|
+
* email allowlist.
|
|
139
|
+
*/
|
|
140
|
+
async verify(authorization) {
|
|
141
|
+
const m = authorization ? /^Bearer\s+(.+)$/iu.exec(authorization.trim()) : null;
|
|
142
|
+
if (!m)
|
|
143
|
+
throw new OAuthError("invalid_token", "Bearer token required");
|
|
144
|
+
const token = m[1].trim();
|
|
145
|
+
const getKey = await this.resolveKeys();
|
|
146
|
+
let claims;
|
|
147
|
+
try {
|
|
148
|
+
const result = await jwtVerify(token, getKey, {
|
|
149
|
+
issuer: this.config.issuer,
|
|
150
|
+
audience: this.config.audiences,
|
|
151
|
+
algorithms: ALLOWED_ALGORITHMS,
|
|
152
|
+
clockTolerance: CLOCK_TOLERANCE_SECONDS,
|
|
153
|
+
});
|
|
154
|
+
claims = result.payload;
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
throw new OAuthError("invalid_token", err instanceof Error ? err.message : String(err));
|
|
158
|
+
}
|
|
159
|
+
const user = userFromClaims(claims, this.config.userClaims);
|
|
160
|
+
if (this.config.authorizedEmails.length > 0) {
|
|
161
|
+
const email = user.email;
|
|
162
|
+
const allowed = email !== undefined && this.config.authorizedEmails.some((p) => emailMatches(email, p));
|
|
163
|
+
if (!allowed) {
|
|
164
|
+
throw new OAuthError("forbidden", `${email ?? user.name} is not authorized to use this server`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return user;
|
|
168
|
+
}
|
|
169
|
+
resolveKeys() {
|
|
170
|
+
if (this.keyResolver)
|
|
171
|
+
return Promise.resolve(this.keyResolver);
|
|
172
|
+
if (!this.discovering) {
|
|
173
|
+
this.discovering = this.discoverJwks()
|
|
174
|
+
.then((uri) => {
|
|
175
|
+
this.keyResolver = createRemoteJWKSet(new URL(uri));
|
|
176
|
+
this.log(`[hardcharger-mcp] OAuth: verifying tokens from ${this.config.issuer} with keys at ${uri}`);
|
|
177
|
+
return this.keyResolver;
|
|
178
|
+
})
|
|
179
|
+
.finally(() => {
|
|
180
|
+
this.discovering = undefined;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
return this.discovering;
|
|
184
|
+
}
|
|
185
|
+
/** Returns the JWKS URI: configured, or read from the provider's discovery document. */
|
|
186
|
+
async discoverJwks() {
|
|
187
|
+
if (this.config.jwksUri)
|
|
188
|
+
return this.config.jwksUri;
|
|
189
|
+
try {
|
|
190
|
+
const doc = await discoverMetadata(this.config.issuer, this.fetchImpl);
|
|
191
|
+
return doc.jwks_uri;
|
|
192
|
+
}
|
|
193
|
+
catch (err) {
|
|
194
|
+
throw new OAuthError("invalid_token", `${err instanceof Error ? err.message : String(err)}; set HARDCHARGER_MCP_OAUTH_JWKS_URI to skip discovery`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Fetches the provider's discovery document (OpenID Connect first, then RFC
|
|
200
|
+
* 8414). The document must name the configured issuer and a jwks_uri.
|
|
201
|
+
*/
|
|
202
|
+
export async function discoverMetadata(issuer, fetchImpl = fetch) {
|
|
203
|
+
const errors = [];
|
|
204
|
+
for (const url of discoveryUrls(issuer)) {
|
|
205
|
+
try {
|
|
206
|
+
const res = await fetchImpl(url, { headers: { accept: "application/json" }, signal: AbortSignal.timeout(10000) });
|
|
207
|
+
if (!res.ok) {
|
|
208
|
+
errors.push(`${url}: HTTP ${res.status}`);
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const doc = (await res.json());
|
|
212
|
+
if (typeof doc.jwks_uri !== "string" || doc.jwks_uri === "") {
|
|
213
|
+
errors.push(`${url}: no jwks_uri`);
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (typeof doc.issuer === "string" && doc.issuer.replace(/\/+$/u, "") !== issuer.replace(/\/+$/u, "")) {
|
|
217
|
+
errors.push(`${url}: issuer ${doc.issuer} does not match ${issuer}`);
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
return doc;
|
|
221
|
+
}
|
|
222
|
+
catch (err) {
|
|
223
|
+
errors.push(`${url}: ${err instanceof Error ? err.message : String(err)}`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
throw new Error(`OAuth discovery failed for ${issuer} (${errors.join("; ")})`);
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=oauth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauth.js","sourceRoot":"","sources":["../src/oauth.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,EAAE;AACF,wEAAwE;AACxE,0EAA0E;AAC1E,oCAAoC;AACpC,yDAAyD;AACzD,qEAAqE;AACrE,2EAA2E;AAC3E,8DAA8D;AAC9D,+DAA+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,CAAC;YACD,IAAI,OAAO,GAAG,CAAC,MAAM,KAAK,QAAQ,IAAI,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,KAAK,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,EAAE,CAAC;gBACtG,MAAM,CAAC,IAAI,CAAC,GAAG,GAAG,YAAY,GAAG,CAAC,MAAM,mBAAmB,MAAM,EAAE,CAAC,CAAC;gBACrE,SAAS;YACX,CAAC;YACD,OAAO,GAAG,CAAC;QACb,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,CAAC,IAAI,CAAC,GAAG,GAAG,KAAK,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC7E,CAAC;IACH,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,8BAA8B,MAAM,KAAK,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACjF,CAAC"}
|
package/dist/params.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { JsonScalar } from "@gizmodata/hardcharger";
|
|
2
|
+
import { Table } from "apache-arrow";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
export declare const TYPED_PARAM_TYPES: readonly ["string", "number", "bigint", "boolean", "date", "timestamp", "binary", "null"];
|
|
5
|
+
export type TypedParamType = (typeof TYPED_PARAM_TYPES)[number];
|
|
6
|
+
export declare const typedParamSchema: z.ZodObject<{
|
|
7
|
+
type: z.ZodEnum<{
|
|
8
|
+
string: "string";
|
|
9
|
+
number: "number";
|
|
10
|
+
bigint: "bigint";
|
|
11
|
+
boolean: "boolean";
|
|
12
|
+
date: "date";
|
|
13
|
+
timestamp: "timestamp";
|
|
14
|
+
binary: "binary";
|
|
15
|
+
null: "null";
|
|
16
|
+
}>;
|
|
17
|
+
value: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>>;
|
|
18
|
+
}, z.core.$strict>;
|
|
19
|
+
export declare const paramValueSchema: z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodObject<{
|
|
20
|
+
type: z.ZodEnum<{
|
|
21
|
+
string: "string";
|
|
22
|
+
number: "number";
|
|
23
|
+
bigint: "bigint";
|
|
24
|
+
boolean: "boolean";
|
|
25
|
+
date: "date";
|
|
26
|
+
timestamp: "timestamp";
|
|
27
|
+
binary: "binary";
|
|
28
|
+
null: "null";
|
|
29
|
+
}>;
|
|
30
|
+
value: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>>;
|
|
31
|
+
}, z.core.$strict>]>;
|
|
32
|
+
export declare const paramsSchema: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodObject<{
|
|
33
|
+
type: z.ZodEnum<{
|
|
34
|
+
string: "string";
|
|
35
|
+
number: "number";
|
|
36
|
+
bigint: "bigint";
|
|
37
|
+
boolean: "boolean";
|
|
38
|
+
date: "date";
|
|
39
|
+
timestamp: "timestamp";
|
|
40
|
+
binary: "binary";
|
|
41
|
+
null: "null";
|
|
42
|
+
}>;
|
|
43
|
+
value: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>>;
|
|
44
|
+
}, z.core.$strict>]>>;
|
|
45
|
+
export type JsonParamValue = z.infer<typeof paramValueSchema>;
|
|
46
|
+
/** What a statement is bound with: JSON values, or one row of Arrow data. */
|
|
47
|
+
export type BoundParams = JsonScalar[] | Table;
|
|
48
|
+
export declare class ParameterError extends Error {
|
|
49
|
+
constructor(message: string);
|
|
50
|
+
}
|
|
51
|
+
/** Parses an ISO-8601 date or timestamp string into a Date (UTC when no zone given). */
|
|
52
|
+
export declare function parseIsoDateTime(text: string): Date | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Converts the JSON `params` array from a tool call into the values the client binds.
|
|
55
|
+
* Returns `undefined` when there is nothing to bind.
|
|
56
|
+
*/
|
|
57
|
+
export declare function convertParams(params: JsonParamValue[] | undefined): BoundParams | undefined;
|
|
58
|
+
/** How many values `bound` carries (one per placeholder). */
|
|
59
|
+
export declare function boundCount(bound: BoundParams | undefined): number;
|
|
60
|
+
/** Documentation for the `params` argument, shared by run_query / execute_statement. */
|
|
61
|
+
export declare const PARAMS_DESCRIPTION: string;
|
package/dist/params.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// Conversion of JSON tool arguments into the values the Hardcharger client binds to
|
|
2
|
+
// `?` / `$1` placeholders.
|
|
3
|
+
//
|
|
4
|
+
// Plain JSON values travel as Warp SQL JSON parameters (§6.1): string, number, boolean
|
|
5
|
+
// and null as they are. The server types each one from where it is used, so a date
|
|
6
|
+
// string compared with a DATE column, or inserted into a TIMESTAMP one, is read as a
|
|
7
|
+
// date. A typed object form `{ "type": "...", "value": ... }` covers what that cannot
|
|
8
|
+
// express: a bigint beyond JavaScript's safe integers (sent exactly), and dates,
|
|
9
|
+
// timestamps and binary values whose type the SQL does not give. When any parameter is
|
|
10
|
+
// a date, timestamp or binary value, all of them are bound as one row of Arrow data
|
|
11
|
+
// (the statement is prepared first), each with its own Arrow type.
|
|
12
|
+
import { Binary, Bool, DateDay, Field, Float64, Int64, Null, RecordBatch, Schema, Struct, Table, TimestampMicrosecond, Utf8, makeData, vectorFromArray, } from "apache-arrow";
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
export const TYPED_PARAM_TYPES = [
|
|
15
|
+
"string",
|
|
16
|
+
"number",
|
|
17
|
+
"bigint",
|
|
18
|
+
"boolean",
|
|
19
|
+
"date",
|
|
20
|
+
"timestamp",
|
|
21
|
+
"binary",
|
|
22
|
+
"null",
|
|
23
|
+
];
|
|
24
|
+
export const typedParamSchema = z
|
|
25
|
+
.object({
|
|
26
|
+
type: z.enum(TYPED_PARAM_TYPES),
|
|
27
|
+
value: z.union([z.string(), z.number(), z.boolean(), z.null()]).optional(),
|
|
28
|
+
})
|
|
29
|
+
.strict();
|
|
30
|
+
export const paramValueSchema = z.union([
|
|
31
|
+
z.string(),
|
|
32
|
+
z.number(),
|
|
33
|
+
z.boolean(),
|
|
34
|
+
z.null(),
|
|
35
|
+
typedParamSchema,
|
|
36
|
+
]);
|
|
37
|
+
export const paramsSchema = z.array(paramValueSchema);
|
|
38
|
+
export class ParameterError extends Error {
|
|
39
|
+
constructor(message) {
|
|
40
|
+
super(message);
|
|
41
|
+
this.name = "ParameterError";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// YYYY-MM-DD
|
|
45
|
+
const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/u;
|
|
46
|
+
// YYYY-MM-DD[T ]HH:MM[:SS[.fraction]][Z|±HH[:MM]]
|
|
47
|
+
const ISO_TIMESTAMP = /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d{1,9})?)?(?:Z|[+-]\d{2}(?::?\d{2})?)?$/u;
|
|
48
|
+
/** Parses an ISO-8601 date or timestamp string into a Date (UTC when no zone given). */
|
|
49
|
+
export function parseIsoDateTime(text) {
|
|
50
|
+
if (ISO_DATE.test(text)) {
|
|
51
|
+
const d = new Date(`${text}T00:00:00Z`);
|
|
52
|
+
return Number.isNaN(d.getTime()) ? undefined : d;
|
|
53
|
+
}
|
|
54
|
+
if (ISO_TIMESTAMP.test(text)) {
|
|
55
|
+
let normalized = text.replace(" ", "T");
|
|
56
|
+
// Treat zone-less timestamps as UTC so the bound value is deterministic.
|
|
57
|
+
if (!/(?:Z|[+-]\d{2}(?::?\d{2})?)$/u.test(normalized))
|
|
58
|
+
normalized += "Z";
|
|
59
|
+
// Trim fractions beyond milliseconds (Date only keeps ms).
|
|
60
|
+
normalized = normalized.replace(/(\.\d{3})\d+/u, "$1");
|
|
61
|
+
const d = new Date(normalized);
|
|
62
|
+
return Number.isNaN(d.getTime()) ? undefined : d;
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
function convertTyped(index, type, value) {
|
|
67
|
+
const where = `parameter ${index + 1} (type "${type}")`;
|
|
68
|
+
switch (type) {
|
|
69
|
+
case "null":
|
|
70
|
+
return { value: null };
|
|
71
|
+
case "string":
|
|
72
|
+
if (value === null || value === undefined)
|
|
73
|
+
return { value: null };
|
|
74
|
+
return { value: String(value) };
|
|
75
|
+
case "boolean":
|
|
76
|
+
if (typeof value === "boolean")
|
|
77
|
+
return { value };
|
|
78
|
+
if (value === "true")
|
|
79
|
+
return { value: true };
|
|
80
|
+
if (value === "false")
|
|
81
|
+
return { value: false };
|
|
82
|
+
throw new ParameterError(`${where}: expected a boolean value.`);
|
|
83
|
+
case "number":
|
|
84
|
+
if (typeof value === "number")
|
|
85
|
+
return { value };
|
|
86
|
+
if (typeof value === "string" && value.trim() !== "" && Number.isFinite(Number(value))) {
|
|
87
|
+
return { value: Number(value) };
|
|
88
|
+
}
|
|
89
|
+
throw new ParameterError(`${where}: expected a numeric value.`);
|
|
90
|
+
case "bigint": {
|
|
91
|
+
if (typeof value === "number") {
|
|
92
|
+
if (!Number.isSafeInteger(value)) {
|
|
93
|
+
throw new ParameterError(`${where}: ${value} is not a safe integer; pass the value as a decimal string.`);
|
|
94
|
+
}
|
|
95
|
+
return { value: BigInt(value) };
|
|
96
|
+
}
|
|
97
|
+
if (typeof value === "string" && /^-?\d+$/u.test(value.trim())) {
|
|
98
|
+
return { value: BigInt(value.trim()) };
|
|
99
|
+
}
|
|
100
|
+
throw new ParameterError(`${where}: expected an integer or a decimal-digit string.`);
|
|
101
|
+
}
|
|
102
|
+
case "date":
|
|
103
|
+
case "timestamp": {
|
|
104
|
+
let d;
|
|
105
|
+
if (typeof value === "number")
|
|
106
|
+
d = new Date(value);
|
|
107
|
+
else if (typeof value === "string")
|
|
108
|
+
d = parseIsoDateTime(value) ?? new Date(value);
|
|
109
|
+
else
|
|
110
|
+
throw new ParameterError(`${where}: expected an ISO-8601 string or epoch milliseconds.`);
|
|
111
|
+
if (Number.isNaN(d.getTime())) {
|
|
112
|
+
throw new ParameterError(`${where}: "${String(value)}" is not a valid ISO-8601 date/timestamp.`);
|
|
113
|
+
}
|
|
114
|
+
return { value: d, arrowType: type === "date" ? new DateDay() : new TimestampMicrosecond() };
|
|
115
|
+
}
|
|
116
|
+
case "binary": {
|
|
117
|
+
if (typeof value !== "string") {
|
|
118
|
+
throw new ParameterError(`${where}: expected a base64 string.`);
|
|
119
|
+
}
|
|
120
|
+
return { value: new Uint8Array(Buffer.from(value, "base64")), arrowType: new Binary() };
|
|
121
|
+
}
|
|
122
|
+
default:
|
|
123
|
+
throw new ParameterError(`${where}: unsupported parameter type.`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function convertOne(index, value) {
|
|
127
|
+
if (value === null)
|
|
128
|
+
return { value: null };
|
|
129
|
+
if (typeof value === "boolean" || typeof value === "string")
|
|
130
|
+
return { value };
|
|
131
|
+
if (typeof value === "number") {
|
|
132
|
+
if (!Number.isFinite(value))
|
|
133
|
+
throw new ParameterError(`parameter ${index + 1}: ${value} is not a finite number.`);
|
|
134
|
+
if (Number.isInteger(value) && !Number.isSafeInteger(value)) {
|
|
135
|
+
throw new ParameterError(`parameter ${index + 1}: ${value} exceeds JavaScript's safe integer range; ` +
|
|
136
|
+
`pass it as {"type":"bigint","value":"<digits>"}.`);
|
|
137
|
+
}
|
|
138
|
+
return { value };
|
|
139
|
+
}
|
|
140
|
+
if (typeof value === "object" && value !== null && "type" in value) {
|
|
141
|
+
return convertTyped(index, value.type, value.value);
|
|
142
|
+
}
|
|
143
|
+
throw new ParameterError(`parameter ${index + 1}: unsupported value.`);
|
|
144
|
+
}
|
|
145
|
+
/** The Arrow type for a value that JSON would have carried. */
|
|
146
|
+
function inferredType(value) {
|
|
147
|
+
if (value === null)
|
|
148
|
+
return new Null();
|
|
149
|
+
if (typeof value === "boolean")
|
|
150
|
+
return new Bool();
|
|
151
|
+
if (typeof value === "bigint")
|
|
152
|
+
return new Int64();
|
|
153
|
+
if (typeof value === "number")
|
|
154
|
+
return Number.isInteger(value) ? new Int64() : new Float64();
|
|
155
|
+
return new Utf8();
|
|
156
|
+
}
|
|
157
|
+
/** One row of Arrow parameters, a column per placeholder (named 1, 2, ...). */
|
|
158
|
+
function arrowRow(converted) {
|
|
159
|
+
const fields = [];
|
|
160
|
+
const children = converted.map((c, i) => {
|
|
161
|
+
const type = c.arrowType ?? inferredType(c.value);
|
|
162
|
+
let value = c.value;
|
|
163
|
+
if (typeof value === "number" && type instanceof Int64)
|
|
164
|
+
value = BigInt(value);
|
|
165
|
+
const vector = vectorFromArray([value], type);
|
|
166
|
+
fields.push(new Field(String(i + 1), vector.type, true));
|
|
167
|
+
return vector.data[0];
|
|
168
|
+
});
|
|
169
|
+
const schema = new Schema(fields);
|
|
170
|
+
const batch = new RecordBatch(schema, makeData({ type: new Struct(schema.fields), length: 1, children }));
|
|
171
|
+
return new Table([batch]);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Converts the JSON `params` array from a tool call into the values the client binds.
|
|
175
|
+
* Returns `undefined` when there is nothing to bind.
|
|
176
|
+
*/
|
|
177
|
+
export function convertParams(params) {
|
|
178
|
+
if (!params || params.length === 0)
|
|
179
|
+
return undefined;
|
|
180
|
+
const converted = params.map((v, i) => convertOne(i, v));
|
|
181
|
+
if (converted.some((c) => c.arrowType !== undefined))
|
|
182
|
+
return arrowRow(converted);
|
|
183
|
+
return converted.map((c) => c.value);
|
|
184
|
+
}
|
|
185
|
+
/** How many values `bound` carries (one per placeholder). */
|
|
186
|
+
export function boundCount(bound) {
|
|
187
|
+
if (bound === undefined)
|
|
188
|
+
return 0;
|
|
189
|
+
return bound instanceof Table ? bound.numCols : bound.length;
|
|
190
|
+
}
|
|
191
|
+
/** Documentation for the `params` argument, shared by run_query / execute_statement. */
|
|
192
|
+
export const PARAMS_DESCRIPTION = "Optional JSON array of values bound positionally to ? or $1, $2, ... placeholders in the SQL " +
|
|
193
|
+
"(bound parameters, never string interpolation). Use placeholders for any literal that " +
|
|
194
|
+
"comes from user input or data. Accepted values: string, number, boolean, null. The server types " +
|
|
195
|
+
"each value from where it is used, so an ISO-8601 date string compared with or inserted into a " +
|
|
196
|
+
"DATE/TIMESTAMP column is read as a date. For explicit types use " +
|
|
197
|
+
'{"type":"bigint"|"date"|"timestamp"|"binary"|"string"|"number"|"boolean"|"null","value":...} ' +
|
|
198
|
+
"(binary values are base64; bigint as a digit string for values beyond 2^53). A placeholder whose " +
|
|
199
|
+
"type nothing gives (e.g. SELECT ?) is text unless typed or cast, e.g. SELECT ?::INTEGER.";
|
|
200
|
+
//# sourceMappingURL=params.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"params.js","sourceRoot":"","sources":["../src/params.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,2BAA2B;AAC3B,EAAE;AACF,uFAAuF;AACvF,mFAAmF;AACnF,qFAAqF;AACrF,sFAAsF;AACtF,iFAAiF;AACjF,uFAAuF;AACvF,oFAAoF;AACpF,mEAAmE;AAGnE,OAAO,EACL,MAAM,EACN,IAAI,EAEJ,OAAO,EACP,KAAK,EACL,OAAO,EACP,KAAK,EACL,IAAI,EACJ,WAAW,EACX,MAAM,EACN,MAAM,EACN,KAAK,EACL,oBAAoB,EACpB,IAAI,EACJ,QAAQ,EACR,eAAe,GAChB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,QAAQ;IACR,QAAQ;IACR,QAAQ;IACR,SAAS;IACT,MAAM;IACN,WAAW;IACX,QAAQ;IACR,MAAM;CACE,CAAC;AAIX,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC;KAC9B,MAAM,CAAC;I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