@raily/sdk 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 +166 -0
- package/README.md +71 -0
- package/dist/index.cjs +130 -0
- package/dist/index.d.cts +43 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +103 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work; and
|
|
103
|
+
|
|
104
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
105
|
+
distribution, then any Derivative Works that You distribute must
|
|
106
|
+
include a readable copy of the attribution notices contained
|
|
107
|
+
within such NOTICE file.
|
|
108
|
+
|
|
109
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
110
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
111
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
112
|
+
this License, without any additional terms or conditions.
|
|
113
|
+
|
|
114
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
115
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
116
|
+
except as required for reasonable and customary use in describing the
|
|
117
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
118
|
+
|
|
119
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed
|
|
120
|
+
to in writing, Licensor provides the Work (and each Contributor
|
|
121
|
+
provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES
|
|
122
|
+
OR CONDITIONS OF ANY KIND, either express or implied, including,
|
|
123
|
+
without limitation, any warranties or conditions of TITLE,
|
|
124
|
+
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR
|
|
125
|
+
PURPOSE. You are solely responsible for determining the
|
|
126
|
+
appropriateness of using or redistributing the Work and assume any
|
|
127
|
+
risks associated with Your exercise of permissions under this License.
|
|
128
|
+
|
|
129
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
130
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
131
|
+
unless required by applicable law (such as deliberate and grossly
|
|
132
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
133
|
+
liable to You for damages, including any direct, indirect, special,
|
|
134
|
+
incidental, or consequential damages of any character arising as a
|
|
135
|
+
result of this License or out of the use or inability to use the
|
|
136
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
137
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
138
|
+
other commercial damages or losses), even if such Contributor
|
|
139
|
+
has been advised of the possibility of such damages.
|
|
140
|
+
|
|
141
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
142
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
143
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
144
|
+
or other liability obligations and/or rights consistent with this
|
|
145
|
+
License. However, in accepting such obligations, You may act only
|
|
146
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
147
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
148
|
+
defend, and hold each Contributor harmless for any liability
|
|
149
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
150
|
+
of your accepting any such warranty or additional liability.
|
|
151
|
+
|
|
152
|
+
END OF TERMS AND CONDITIONS
|
|
153
|
+
|
|
154
|
+
Copyright 2026 Raily
|
|
155
|
+
|
|
156
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
157
|
+
you may not use this file except in compliance with the License.
|
|
158
|
+
You may obtain a copy of the License at
|
|
159
|
+
|
|
160
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
161
|
+
|
|
162
|
+
Unless required by applicable law or agreed to in writing, software
|
|
163
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
164
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
165
|
+
See the License for the specific language governing permissions and
|
|
166
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Raily TypeScript SDK
|
|
2
|
+
|
|
3
|
+
Official TypeScript/JavaScript client for the [Raily](https://raily.ai) search API —
|
|
4
|
+
search your own sources from your app with a typed, one-call client.
|
|
5
|
+
|
|
6
|
+
[Documentation](https://docs.raily.ai) · [Source](https://github.com/Raily-ai/raily-typescript)
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @raily/sdk
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Quickstart
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { Raily } from "@raily/sdk";
|
|
18
|
+
|
|
19
|
+
const client = new Raily({ apiKey: "rly_...", endpoint: "https://<your-endpoint-url>" });
|
|
20
|
+
|
|
21
|
+
const hits = await client.search("anti-aging serum", { limit: 5 });
|
|
22
|
+
for (const hit of hits) {
|
|
23
|
+
console.log(hit.id, hit.score, hit.fields, hit.text);
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Authentication
|
|
28
|
+
|
|
29
|
+
Create an API key in your Raily workspace (**Identities → API keys**) and pass it as
|
|
30
|
+
`apiKey`. The `endpoint` is your endpoint's URL. A key is scoped to one workspace and to
|
|
31
|
+
the endpoint it was issued for — using it elsewhere is rejected.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
const client = new Raily({
|
|
35
|
+
apiKey: process.env.RAILY_API_KEY!,
|
|
36
|
+
endpoint: process.env.RAILY_ENDPOINT!,
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Results
|
|
41
|
+
|
|
42
|
+
`search()` resolves to `SearchHit[]`:
|
|
43
|
+
|
|
44
|
+
| field | type | meaning |
|
|
45
|
+
|---|---|---|
|
|
46
|
+
| `id` | `string \| null` | stable result id |
|
|
47
|
+
| `score` | `number \| null` | relevance score |
|
|
48
|
+
| `source_collection` | `string \| null` | the source the hit came from |
|
|
49
|
+
| `fields` | `unknown[]` | rendered display fields (when the source's display is configured) |
|
|
50
|
+
| `text` | `string \| null` | rendered text |
|
|
51
|
+
| `raw` | `Record<string, unknown>` | the full result object (forward-compatible) |
|
|
52
|
+
|
|
53
|
+
## Errors
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { RailyError, RailyAuthError } from "@raily/sdk";
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
await client.search("query");
|
|
60
|
+
} catch (err) {
|
|
61
|
+
if (err instanceof RailyAuthError) {
|
|
62
|
+
// key missing / invalid / expired / not authorized for this endpoint
|
|
63
|
+
} else if (err instanceof RailyError) {
|
|
64
|
+
// everything else (network, server, protocol)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## License
|
|
70
|
+
|
|
71
|
+
Apache-2.0
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
Raily: () => Raily,
|
|
24
|
+
RailyAuthError: () => RailyAuthError,
|
|
25
|
+
RailyError: () => RailyError
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
var import_client = require("@modelcontextprotocol/sdk/client/index.js");
|
|
29
|
+
var import_streamableHttp = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
|
|
30
|
+
var AUTH_SIGNALS = ["401", "403", "unauthorized", "forbidden", "invalid api key", "not active"];
|
|
31
|
+
function properties(tool) {
|
|
32
|
+
return tool.inputSchema?.properties ?? {};
|
|
33
|
+
}
|
|
34
|
+
function pickSearchTool(tools) {
|
|
35
|
+
if (!tools || tools.length === 0) throw new RailyError("this endpoint exposes no tools.");
|
|
36
|
+
const queryTools = tools.filter((t) => "query" in properties(t) && t.name !== "search_app");
|
|
37
|
+
if (queryTools.length === 0) throw new RailyError("this endpoint exposes no search tool.");
|
|
38
|
+
const signature = queryTools.filter((t) => "exact_user_query" in properties(t));
|
|
39
|
+
const pool = signature.length > 0 ? signature : queryTools;
|
|
40
|
+
return pool.find((t) => t.name === "search") ?? pool[0];
|
|
41
|
+
}
|
|
42
|
+
function buildArgs(tool, query, limit) {
|
|
43
|
+
const props = properties(tool);
|
|
44
|
+
const args = { query };
|
|
45
|
+
if ("exact_user_query" in props) args.exact_user_query = query;
|
|
46
|
+
if ("limit" in props) args.limit = limit;
|
|
47
|
+
return args;
|
|
48
|
+
}
|
|
49
|
+
var RailyError = class extends Error {
|
|
50
|
+
constructor(message) {
|
|
51
|
+
super(message);
|
|
52
|
+
this.name = "RailyError";
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var RailyAuthError = class extends RailyError {
|
|
56
|
+
constructor(message) {
|
|
57
|
+
super(message);
|
|
58
|
+
this.name = "RailyAuthError";
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
function isAuthError(err) {
|
|
62
|
+
const anyErr = err;
|
|
63
|
+
const status = anyErr?.status ?? anyErr?.code;
|
|
64
|
+
if (status === 401 || status === 403) return true;
|
|
65
|
+
const text = String(anyErr?.message ?? err ?? "").toLowerCase();
|
|
66
|
+
return AUTH_SIGNALS.some((s) => text.includes(s));
|
|
67
|
+
}
|
|
68
|
+
function toHit(value) {
|
|
69
|
+
const r = value ?? {};
|
|
70
|
+
const id = r.id;
|
|
71
|
+
const score = r.score;
|
|
72
|
+
return {
|
|
73
|
+
id: id === void 0 || id === null ? null : String(id),
|
|
74
|
+
score: typeof score === "number" ? score : null,
|
|
75
|
+
source_collection: r.source_collection ?? null,
|
|
76
|
+
fields: Array.isArray(r.fields) ? r.fields : [],
|
|
77
|
+
text: r.text || null,
|
|
78
|
+
raw: r
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function parseHits(structured) {
|
|
82
|
+
const results = structured?.results;
|
|
83
|
+
return Array.isArray(results) ? results.map(toHit) : [];
|
|
84
|
+
}
|
|
85
|
+
var Raily = class {
|
|
86
|
+
apiKey;
|
|
87
|
+
endpoint;
|
|
88
|
+
timeoutMs;
|
|
89
|
+
constructor(options) {
|
|
90
|
+
if (!options?.apiKey) throw new RailyError("apiKey is required");
|
|
91
|
+
if (!options?.endpoint) throw new RailyError("endpoint is required");
|
|
92
|
+
this.apiKey = options.apiKey;
|
|
93
|
+
this.endpoint = options.endpoint;
|
|
94
|
+
this.timeoutMs = options.timeoutMs ?? 6e4;
|
|
95
|
+
}
|
|
96
|
+
async search(query, options = {}) {
|
|
97
|
+
if (!query) throw new RailyError("query is required");
|
|
98
|
+
const transport = new import_streamableHttp.StreamableHTTPClientTransport(new URL(this.endpoint), {
|
|
99
|
+
requestInit: { headers: { Authorization: `Bearer ${this.apiKey}` } }
|
|
100
|
+
});
|
|
101
|
+
const client = new import_client.Client({ name: "raily-sdk", version: "0.1.0" }, { capabilities: {} });
|
|
102
|
+
const limit = options.limit ?? 3;
|
|
103
|
+
try {
|
|
104
|
+
await client.connect(transport);
|
|
105
|
+
const { tools } = await client.listTools();
|
|
106
|
+
const tool = pickSearchTool(tools);
|
|
107
|
+
const result = await client.callTool(
|
|
108
|
+
{ name: tool.name, arguments: buildArgs(tool, query, limit) },
|
|
109
|
+
void 0,
|
|
110
|
+
{ timeout: this.timeoutMs }
|
|
111
|
+
);
|
|
112
|
+
if (result?.isError) {
|
|
113
|
+
throw new RailyError("search failed on the server.");
|
|
114
|
+
}
|
|
115
|
+
return parseHits(result.structuredContent).slice(0, limit);
|
|
116
|
+
} catch (err) {
|
|
117
|
+
if (err instanceof RailyError) throw err;
|
|
118
|
+
if (isAuthError(err)) throw new RailyAuthError("API key was rejected for this endpoint.");
|
|
119
|
+
throw new RailyError(`search request failed: ${String(err?.message ?? err)}`);
|
|
120
|
+
} finally {
|
|
121
|
+
await client.close().catch(() => void 0);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
126
|
+
0 && (module.exports = {
|
|
127
|
+
Raily,
|
|
128
|
+
RailyAuthError,
|
|
129
|
+
RailyError
|
|
130
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** One search result — the curated developer projection, never the raw payload. */
|
|
2
|
+
interface SearchHit {
|
|
3
|
+
id: string | null;
|
|
4
|
+
score: number | null;
|
|
5
|
+
source_collection: string | null;
|
|
6
|
+
fields: unknown[];
|
|
7
|
+
text: string | null;
|
|
8
|
+
/** The full result object, for forward compatibility. */
|
|
9
|
+
raw: Record<string, unknown>;
|
|
10
|
+
}
|
|
11
|
+
declare class RailyError extends Error {
|
|
12
|
+
constructor(message: string);
|
|
13
|
+
}
|
|
14
|
+
/** The key was missing/invalid/expired, or not authorized for this endpoint
|
|
15
|
+
* (also covers a cross-tenant key or a key bound to a different endpoint). */
|
|
16
|
+
declare class RailyAuthError extends RailyError {
|
|
17
|
+
constructor(message: string);
|
|
18
|
+
}
|
|
19
|
+
interface RailyOptions {
|
|
20
|
+
apiKey: string;
|
|
21
|
+
endpoint: string;
|
|
22
|
+
/** Request timeout in milliseconds (default 60000). */
|
|
23
|
+
timeoutMs?: number;
|
|
24
|
+
}
|
|
25
|
+
interface SearchOptions {
|
|
26
|
+
/** Maximum number of results, 1–10 (default 3). */
|
|
27
|
+
limit?: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Client for the Raily search API.
|
|
31
|
+
*
|
|
32
|
+
* Each call opens a short-lived authenticated connection, so the client holds no
|
|
33
|
+
* mutable session state and is safe to reuse.
|
|
34
|
+
*/
|
|
35
|
+
declare class Raily {
|
|
36
|
+
private readonly apiKey;
|
|
37
|
+
private readonly endpoint;
|
|
38
|
+
private readonly timeoutMs;
|
|
39
|
+
constructor(options: RailyOptions);
|
|
40
|
+
search(query: string, options?: SearchOptions): Promise<SearchHit[]>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { Raily, RailyAuthError, RailyError, type RailyOptions, type SearchHit, type SearchOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** One search result — the curated developer projection, never the raw payload. */
|
|
2
|
+
interface SearchHit {
|
|
3
|
+
id: string | null;
|
|
4
|
+
score: number | null;
|
|
5
|
+
source_collection: string | null;
|
|
6
|
+
fields: unknown[];
|
|
7
|
+
text: string | null;
|
|
8
|
+
/** The full result object, for forward compatibility. */
|
|
9
|
+
raw: Record<string, unknown>;
|
|
10
|
+
}
|
|
11
|
+
declare class RailyError extends Error {
|
|
12
|
+
constructor(message: string);
|
|
13
|
+
}
|
|
14
|
+
/** The key was missing/invalid/expired, or not authorized for this endpoint
|
|
15
|
+
* (also covers a cross-tenant key or a key bound to a different endpoint). */
|
|
16
|
+
declare class RailyAuthError extends RailyError {
|
|
17
|
+
constructor(message: string);
|
|
18
|
+
}
|
|
19
|
+
interface RailyOptions {
|
|
20
|
+
apiKey: string;
|
|
21
|
+
endpoint: string;
|
|
22
|
+
/** Request timeout in milliseconds (default 60000). */
|
|
23
|
+
timeoutMs?: number;
|
|
24
|
+
}
|
|
25
|
+
interface SearchOptions {
|
|
26
|
+
/** Maximum number of results, 1–10 (default 3). */
|
|
27
|
+
limit?: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Client for the Raily search API.
|
|
31
|
+
*
|
|
32
|
+
* Each call opens a short-lived authenticated connection, so the client holds no
|
|
33
|
+
* mutable session state and is safe to reuse.
|
|
34
|
+
*/
|
|
35
|
+
declare class Raily {
|
|
36
|
+
private readonly apiKey;
|
|
37
|
+
private readonly endpoint;
|
|
38
|
+
private readonly timeoutMs;
|
|
39
|
+
constructor(options: RailyOptions);
|
|
40
|
+
search(query: string, options?: SearchOptions): Promise<SearchHit[]>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { Raily, RailyAuthError, RailyError, type RailyOptions, type SearchHit, type SearchOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
3
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
4
|
+
var AUTH_SIGNALS = ["401", "403", "unauthorized", "forbidden", "invalid api key", "not active"];
|
|
5
|
+
function properties(tool) {
|
|
6
|
+
return tool.inputSchema?.properties ?? {};
|
|
7
|
+
}
|
|
8
|
+
function pickSearchTool(tools) {
|
|
9
|
+
if (!tools || tools.length === 0) throw new RailyError("this endpoint exposes no tools.");
|
|
10
|
+
const queryTools = tools.filter((t) => "query" in properties(t) && t.name !== "search_app");
|
|
11
|
+
if (queryTools.length === 0) throw new RailyError("this endpoint exposes no search tool.");
|
|
12
|
+
const signature = queryTools.filter((t) => "exact_user_query" in properties(t));
|
|
13
|
+
const pool = signature.length > 0 ? signature : queryTools;
|
|
14
|
+
return pool.find((t) => t.name === "search") ?? pool[0];
|
|
15
|
+
}
|
|
16
|
+
function buildArgs(tool, query, limit) {
|
|
17
|
+
const props = properties(tool);
|
|
18
|
+
const args = { query };
|
|
19
|
+
if ("exact_user_query" in props) args.exact_user_query = query;
|
|
20
|
+
if ("limit" in props) args.limit = limit;
|
|
21
|
+
return args;
|
|
22
|
+
}
|
|
23
|
+
var RailyError = class extends Error {
|
|
24
|
+
constructor(message) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "RailyError";
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var RailyAuthError = class extends RailyError {
|
|
30
|
+
constructor(message) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.name = "RailyAuthError";
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
function isAuthError(err) {
|
|
36
|
+
const anyErr = err;
|
|
37
|
+
const status = anyErr?.status ?? anyErr?.code;
|
|
38
|
+
if (status === 401 || status === 403) return true;
|
|
39
|
+
const text = String(anyErr?.message ?? err ?? "").toLowerCase();
|
|
40
|
+
return AUTH_SIGNALS.some((s) => text.includes(s));
|
|
41
|
+
}
|
|
42
|
+
function toHit(value) {
|
|
43
|
+
const r = value ?? {};
|
|
44
|
+
const id = r.id;
|
|
45
|
+
const score = r.score;
|
|
46
|
+
return {
|
|
47
|
+
id: id === void 0 || id === null ? null : String(id),
|
|
48
|
+
score: typeof score === "number" ? score : null,
|
|
49
|
+
source_collection: r.source_collection ?? null,
|
|
50
|
+
fields: Array.isArray(r.fields) ? r.fields : [],
|
|
51
|
+
text: r.text || null,
|
|
52
|
+
raw: r
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function parseHits(structured) {
|
|
56
|
+
const results = structured?.results;
|
|
57
|
+
return Array.isArray(results) ? results.map(toHit) : [];
|
|
58
|
+
}
|
|
59
|
+
var Raily = class {
|
|
60
|
+
apiKey;
|
|
61
|
+
endpoint;
|
|
62
|
+
timeoutMs;
|
|
63
|
+
constructor(options) {
|
|
64
|
+
if (!options?.apiKey) throw new RailyError("apiKey is required");
|
|
65
|
+
if (!options?.endpoint) throw new RailyError("endpoint is required");
|
|
66
|
+
this.apiKey = options.apiKey;
|
|
67
|
+
this.endpoint = options.endpoint;
|
|
68
|
+
this.timeoutMs = options.timeoutMs ?? 6e4;
|
|
69
|
+
}
|
|
70
|
+
async search(query, options = {}) {
|
|
71
|
+
if (!query) throw new RailyError("query is required");
|
|
72
|
+
const transport = new StreamableHTTPClientTransport(new URL(this.endpoint), {
|
|
73
|
+
requestInit: { headers: { Authorization: `Bearer ${this.apiKey}` } }
|
|
74
|
+
});
|
|
75
|
+
const client = new Client({ name: "raily-sdk", version: "0.1.0" }, { capabilities: {} });
|
|
76
|
+
const limit = options.limit ?? 3;
|
|
77
|
+
try {
|
|
78
|
+
await client.connect(transport);
|
|
79
|
+
const { tools } = await client.listTools();
|
|
80
|
+
const tool = pickSearchTool(tools);
|
|
81
|
+
const result = await client.callTool(
|
|
82
|
+
{ name: tool.name, arguments: buildArgs(tool, query, limit) },
|
|
83
|
+
void 0,
|
|
84
|
+
{ timeout: this.timeoutMs }
|
|
85
|
+
);
|
|
86
|
+
if (result?.isError) {
|
|
87
|
+
throw new RailyError("search failed on the server.");
|
|
88
|
+
}
|
|
89
|
+
return parseHits(result.structuredContent).slice(0, limit);
|
|
90
|
+
} catch (err) {
|
|
91
|
+
if (err instanceof RailyError) throw err;
|
|
92
|
+
if (isAuthError(err)) throw new RailyAuthError("API key was rejected for this endpoint.");
|
|
93
|
+
throw new RailyError(`search request failed: ${String(err?.message ?? err)}`);
|
|
94
|
+
} finally {
|
|
95
|
+
await client.close().catch(() => void 0);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
export {
|
|
100
|
+
Raily,
|
|
101
|
+
RailyAuthError,
|
|
102
|
+
RailyError
|
|
103
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@raily/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Official TypeScript SDK for the Raily search API.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": { "types": "./dist/index.d.ts", "default": "./dist/index.js" },
|
|
13
|
+
"require": { "types": "./dist/index.d.cts", "default": "./dist/index.cjs" }
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
17
|
+
"repository": { "type": "git", "url": "https://github.com/Raily-ai/raily-typescript.git" },
|
|
18
|
+
"homepage": "https://docs.raily.ai",
|
|
19
|
+
"keywords": ["raily", "search", "retrieval", "rag", "mcp", "sdk"],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
22
|
+
"typecheck": "tsc --noEmit",
|
|
23
|
+
"test": "vitest run"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@modelcontextprotocol/sdk": "^1.12.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^22.0.0",
|
|
30
|
+
"tsup": "^8.0.0",
|
|
31
|
+
"typescript": "^5.6.0",
|
|
32
|
+
"vitest": "^2.1.0"
|
|
33
|
+
},
|
|
34
|
+
"engines": { "node": ">=18" }
|
|
35
|
+
}
|