hicreator-agent 0.1.0 → 0.1.2
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 +14 -4
- package/package.json +1 -1
- package/skills/hicreator/SKILL.md +4 -4
- package/src/cli.js +55 -42
- package/src/mcp-check.js +70 -23
- package/src/setup-flow.js +162 -0
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# hiCreator Agent
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Connect AI agents to hiCreator creator search, email lookup, and favorites. The installer provides verified one-step setup for Codex, Claude Code, and Cursor. Any client that supports remote Streamable HTTP MCP with a custom `Authorization` header can use the same six tools.
|
|
4
4
|
|
|
5
5
|
## Requirements
|
|
6
6
|
|
|
7
7
|
- Node.js 20 or newer.
|
|
8
8
|
- A hiCreator Developer API key beginning with `hc_`.
|
|
9
|
-
- Codex, Claude Code, or Cursor
|
|
9
|
+
- Codex, Claude Code, or Cursor for verified one-step setup, or another compatible MCP client for guided setup.
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
@@ -14,7 +14,7 @@ Install one hiCreator skill and remote MCP connection for Codex, Claude Code, an
|
|
|
14
14
|
npx --yes --prefer-online hicreator-agent@latest setup
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
The installer detects supported clients,
|
|
17
|
+
The installer detects supported clients, shows masked `*` feedback while the API key is entered, validates its format, and verifies the remote MCP connection without making a paid call. Invalid keys can be entered again, and temporary network or server failures retry the free connection check up to three times. Client configuration is written only after verification succeeds.
|
|
18
18
|
|
|
19
19
|
To choose clients explicitly:
|
|
20
20
|
|
|
@@ -36,7 +36,7 @@ npx --yes --prefer-online hicreator-agent@latest prepare --json
|
|
|
36
36
|
|
|
37
37
|
## Billing
|
|
38
38
|
|
|
39
|
-
AI search, similar-creator search, and email lookup consume hiCreator credits. The skill never automatically retries those tools. Creating, listing, and adding existing creators to
|
|
39
|
+
AI search, similar-creator search, and email lookup consume hiCreator credits. The skill never automatically retries those tools. Creating, listing, and adding existing creators to favorites are free.
|
|
40
40
|
|
|
41
41
|
## Local Files
|
|
42
42
|
|
|
@@ -46,3 +46,13 @@ AI search, similar-creator search, and email lookup consume hiCreator credits. T
|
|
|
46
46
|
- Cursor skill: `~/.cursor/skills/hicreator/`
|
|
47
47
|
|
|
48
48
|
Existing unrelated MCP servers and settings are preserved.
|
|
49
|
+
|
|
50
|
+
## Other AI Clients
|
|
51
|
+
|
|
52
|
+
For clients outside the verified list, add the remote MCP server using the client's official configuration:
|
|
53
|
+
|
|
54
|
+
- Transport: Streamable HTTP
|
|
55
|
+
- URL: `https://mcp.hicreator.ai/mcp`
|
|
56
|
+
- Header: `Authorization: Bearer <HICREATOR_API_KEY>`
|
|
57
|
+
|
|
58
|
+
Keep the API key in the client's local secret or header configuration. Do not paste it into an AI conversation. If the client also implements the Agent Skills specification, install `skills/hicreator/` into its supported skills directory; the MCP tools still work when Agent Skills are unavailable.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: hicreator
|
|
3
|
-
description: Find Instagram, YouTube, or TikTok creators, find similar creators or public contact emails, and organize creators in hiCreator
|
|
3
|
+
description: Find Instagram, YouTube, or TikTok creators, find similar creators or public contact emails, and organize creators in hiCreator favorites. Use when the user asks for creator discovery, creator email lookup, or saving creator results to hiCreator.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# hiCreator
|
|
@@ -22,9 +22,9 @@ Run it once per user workflow, before the first hiCreator tool call. If it retur
|
|
|
22
22
|
1. Infer or confirm the platform, requested count, hard filters, and whether the user wants results saved.
|
|
23
23
|
2. Use `search_creators_ai` for intent-based discovery or `find_similar_creators` for a reference account.
|
|
24
24
|
3. Check returned results against hard constraints and state which constraints the data could not verify.
|
|
25
|
-
4. To save results, call `list_creator_folders`; create the destination with `create_creator_folder` only when needed; then call `add_creators_to_folder` once with stable `platform` and `creator_id` values from search results.
|
|
25
|
+
4. To save results, call `list_creator_folders`; create the destination favorites list with `create_creator_folder` only when needed; then call `add_creators_to_folder` once with stable `platform` and `creator_id` values from search results.
|
|
26
26
|
5. Call `find_creator_email` only when the user explicitly asks for an email and the selected result does not already include one.
|
|
27
|
-
6. Report returned count, verified constraints,
|
|
27
|
+
6. Report returned count, verified constraints, favorites result, and any per-item failures.
|
|
28
28
|
|
|
29
29
|
## Billing Safety
|
|
30
30
|
|
|
@@ -32,4 +32,4 @@ Run it once per user workflow, before the first hiCreator tool call. If it retur
|
|
|
32
32
|
|
|
33
33
|
Never automatically retry a paid tool after a timeout, disconnect, or uncertain result. Tell the user the request may have been charged and ask them to inspect API history before deciding whether to retry. Never silently raise `limit`, broaden filters, run a second search, or perform email lookup.
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
Favorites listing, creation, and batch add are free. A failed free favorites operation may be retried without repeating the paid search. Do not claim support for deletion, rename, removal, or sharing.
|
package/src/cli.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Writable } from "node:stream";
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
3
2
|
import { homedir } from "node:os";
|
|
4
3
|
|
|
5
4
|
import {
|
|
@@ -13,6 +12,14 @@ import {
|
|
|
13
12
|
} from "./configure.js";
|
|
14
13
|
import { installedClients, syncSkill } from "./install-skill.js";
|
|
15
14
|
import { verifyMcp } from "./mcp-check.js";
|
|
15
|
+
import {
|
|
16
|
+
readMaskedSecret,
|
|
17
|
+
resolveSetupConnection,
|
|
18
|
+
} from "./setup-flow.js";
|
|
19
|
+
|
|
20
|
+
const packageVersion = JSON.parse(
|
|
21
|
+
await readFile(new URL("../package.json", import.meta.url), "utf8"),
|
|
22
|
+
).version;
|
|
16
23
|
|
|
17
24
|
function parseOptions(argv) {
|
|
18
25
|
const [command, ...rest] = argv;
|
|
@@ -35,37 +42,6 @@ function parseOptions(argv) {
|
|
|
35
42
|
return options;
|
|
36
43
|
}
|
|
37
44
|
|
|
38
|
-
async function promptForApiKey() {
|
|
39
|
-
if (!process.stdin.isTTY) {
|
|
40
|
-
throw new Error(
|
|
41
|
-
"Set HICREATOR_API_KEY or run setup in an interactive terminal.",
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
let muted = false;
|
|
45
|
-
const output = new Writable({
|
|
46
|
-
write(chunk, encoding, callback) {
|
|
47
|
-
if (!muted) process.stderr.write(chunk, encoding);
|
|
48
|
-
callback();
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
const readline = createInterface({ input: process.stdin, output, terminal: true });
|
|
52
|
-
const answerPromise = readline.question("hiCreator API key: ");
|
|
53
|
-
muted = true;
|
|
54
|
-
const answer = await answerPromise;
|
|
55
|
-
muted = false;
|
|
56
|
-
process.stderr.write("\n");
|
|
57
|
-
readline.close();
|
|
58
|
-
return answer;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function validateApiKey(value) {
|
|
62
|
-
const apiKey = value?.trim();
|
|
63
|
-
if (!/^hc_[A-Za-z0-9_-]{17,253}$/u.test(apiKey ?? "")) {
|
|
64
|
-
throw new Error("Enter a valid hiCreator Developer API key.");
|
|
65
|
-
}
|
|
66
|
-
return apiKey;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
45
|
function validateMcpUrl(value) {
|
|
70
46
|
let url;
|
|
71
47
|
try {
|
|
@@ -86,14 +62,26 @@ function validateMcpUrl(value) {
|
|
|
86
62
|
function printResult(result, json) {
|
|
87
63
|
if (json) process.stdout.write(`${JSON.stringify(result)}\n`);
|
|
88
64
|
else {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
65
|
+
const configurations = result.clients
|
|
66
|
+
.map((client) => `- ${client}: ${result.configPaths[client]}`)
|
|
67
|
+
.join("\n");
|
|
68
|
+
process.stdout.write([
|
|
69
|
+
`hiCreator ${result.version} is ready.`,
|
|
70
|
+
result.verified
|
|
71
|
+
? `MCP tools: ${result.toolCount} verified.`
|
|
72
|
+
: "MCP tools: verification skipped.",
|
|
73
|
+
"Configured clients:",
|
|
74
|
+
configurations,
|
|
75
|
+
"",
|
|
76
|
+
].join("\n"));
|
|
92
77
|
}
|
|
93
78
|
}
|
|
94
79
|
|
|
95
80
|
export async function setup(options = {}) {
|
|
96
81
|
const home = options.home ?? homedir();
|
|
82
|
+
const output = options.output ?? process.stderr;
|
|
83
|
+
const report = options.report ?? ((message) => output.write(`${message}\n`));
|
|
84
|
+
report("[1/5] Detecting supported clients...");
|
|
97
85
|
const clients = options.clients
|
|
98
86
|
?? await detectClients(options.pathValue);
|
|
99
87
|
if (clients.length === 0) {
|
|
@@ -101,25 +89,50 @@ export async function setup(options = {}) {
|
|
|
101
89
|
`No supported client found. Install one of: ${SUPPORTED_CLIENTS.join(", ")}.`,
|
|
102
90
|
);
|
|
103
91
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
);
|
|
92
|
+
report(`[ok] Found: ${clients.join(", ")}.`);
|
|
93
|
+
|
|
94
|
+
report("[2/5] Reading the API Key...");
|
|
107
95
|
const mcpUrl = validateMcpUrl(options.mcpUrl ?? DEFAULT_MCP_URL);
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
96
|
+
const suppliedApiKey = options.apiKey ?? process.env.HICREATOR_API_KEY;
|
|
97
|
+
const prompt = suppliedApiKey === undefined
|
|
98
|
+
? ({ attempt, maxAttempts }) => readMaskedSecret({
|
|
99
|
+
input: options.input ?? process.stdin,
|
|
100
|
+
output,
|
|
101
|
+
label: attempt === 1
|
|
102
|
+
? "hiCreator API Key: "
|
|
103
|
+
: `hiCreator API Key (${attempt}/${maxAttempts}): `,
|
|
104
|
+
})
|
|
105
|
+
: undefined;
|
|
106
|
+
const { apiKey, connection } = await resolveSetupConnection({
|
|
107
|
+
initialApiKey: suppliedApiKey,
|
|
108
|
+
prompt: options.prompt ?? prompt,
|
|
109
|
+
verify: options.verify ?? ((value) => verifyMcp(mcpUrl, value, {
|
|
110
|
+
clientVersion: packageVersion,
|
|
111
|
+
})),
|
|
112
|
+
report,
|
|
113
|
+
sleep: options.sleep,
|
|
114
|
+
skipVerify: options.skipVerify,
|
|
115
|
+
});
|
|
116
|
+
if (options.skipVerify) report("[3/5] MCP connection verification skipped.");
|
|
117
|
+
|
|
118
|
+
report("[4/5] Installing the hiCreator Skill...");
|
|
111
119
|
const skill = await syncSkill({ home, clients });
|
|
120
|
+
report(`[ok] Skill ${skill.updated ? "installed or updated" : "is already current"} (${skill.version}).`);
|
|
121
|
+
|
|
122
|
+
report("[5/5] Configuring clients...");
|
|
112
123
|
const configPaths = await configureClients({
|
|
113
124
|
home,
|
|
114
125
|
clients,
|
|
115
126
|
apiKey,
|
|
116
127
|
mcpUrl,
|
|
117
128
|
});
|
|
129
|
+
report(`[ok] Configured: ${clients.join(", ")}.`);
|
|
118
130
|
return {
|
|
119
131
|
...skill,
|
|
120
132
|
clients,
|
|
121
133
|
configPaths,
|
|
122
134
|
mcpUrl,
|
|
135
|
+
toolCount: connection?.tools.length ?? 0,
|
|
123
136
|
verified: Boolean(connection),
|
|
124
137
|
};
|
|
125
138
|
}
|
package/src/mcp-check.js
CHANGED
|
@@ -8,6 +8,15 @@ const EXPECTED_TOOLS = [
|
|
|
8
8
|
"add_creators_to_folder",
|
|
9
9
|
];
|
|
10
10
|
|
|
11
|
+
export class McpConnectionError extends Error {
|
|
12
|
+
constructor(message, { kind, status, cause } = {}) {
|
|
13
|
+
super(message, { cause });
|
|
14
|
+
this.name = "McpConnectionError";
|
|
15
|
+
this.kind = kind;
|
|
16
|
+
this.status = status;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
11
20
|
function parseMcpPayload(text) {
|
|
12
21
|
if (text.trimStart().startsWith("{")) return JSON.parse(text);
|
|
13
22
|
const data = text.split("\n").find((line) => line.startsWith("data: "));
|
|
@@ -16,43 +25,81 @@ function parseMcpPayload(text) {
|
|
|
16
25
|
}
|
|
17
26
|
|
|
18
27
|
async function request(mcpUrl, apiKey, id, method, params) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
28
|
+
let response;
|
|
29
|
+
try {
|
|
30
|
+
response = await fetch(mcpUrl, {
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: {
|
|
33
|
+
Accept: "application/json, text/event-stream",
|
|
34
|
+
Authorization: `Bearer ${apiKey}`,
|
|
35
|
+
"Content-Type": "application/json",
|
|
36
|
+
"MCP-Protocol-Version": PROTOCOL_VERSION,
|
|
37
|
+
},
|
|
38
|
+
body: JSON.stringify({
|
|
39
|
+
jsonrpc: "2.0",
|
|
40
|
+
id,
|
|
41
|
+
method,
|
|
42
|
+
...(params ? { params } : {}),
|
|
43
|
+
}),
|
|
44
|
+
redirect: "error",
|
|
45
|
+
signal: AbortSignal.timeout(10_000),
|
|
46
|
+
});
|
|
47
|
+
} catch (error) {
|
|
48
|
+
const timedOut = error?.name === "TimeoutError"
|
|
49
|
+
|| error?.name === "AbortError";
|
|
50
|
+
throw new McpConnectionError(
|
|
51
|
+
timedOut
|
|
52
|
+
? "The hiCreator MCP connection timed out."
|
|
53
|
+
: "Could not reach the hiCreator MCP server.",
|
|
54
|
+
{ kind: "transient", cause: error },
|
|
55
|
+
);
|
|
56
|
+
}
|
|
36
57
|
if (!response.ok) {
|
|
37
|
-
|
|
58
|
+
const status = response.status;
|
|
59
|
+
if (status === 401 || status === 403) {
|
|
60
|
+
throw new McpConnectionError(
|
|
61
|
+
`The API Key was rejected (HTTP ${status}). It may be invalid, disabled, or deleted.`,
|
|
62
|
+
{ kind: "auth", status },
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
const transient = status === 408 || status === 429 || status >= 500;
|
|
66
|
+
throw new McpConnectionError(
|
|
67
|
+
transient
|
|
68
|
+
? `The hiCreator MCP server is temporarily unavailable (HTTP ${status}).`
|
|
69
|
+
: `The hiCreator MCP server rejected the connection check (HTTP ${status}).`,
|
|
70
|
+
{ kind: transient ? "transient" : "protocol", status },
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
return parseMcpPayload(await response.text());
|
|
75
|
+
} catch (error) {
|
|
76
|
+
throw new McpConnectionError(
|
|
77
|
+
"The hiCreator MCP server returned an unreadable response.",
|
|
78
|
+
{ kind: "protocol", cause: error },
|
|
79
|
+
);
|
|
38
80
|
}
|
|
39
|
-
return parseMcpPayload(await response.text());
|
|
40
81
|
}
|
|
41
82
|
|
|
42
|
-
export async function verifyMcp(mcpUrl, apiKey) {
|
|
83
|
+
export async function verifyMcp(mcpUrl, apiKey, { clientVersion = "unknown" } = {}) {
|
|
43
84
|
const initialized = await request(mcpUrl, apiKey, 1, "initialize", {
|
|
44
85
|
protocolVersion: PROTOCOL_VERSION,
|
|
45
86
|
capabilities: {},
|
|
46
|
-
clientInfo: { name: "hicreator-agent-setup", version:
|
|
87
|
+
clientInfo: { name: "hicreator-agent-setup", version: clientVersion },
|
|
47
88
|
});
|
|
48
89
|
if (initialized.error || initialized.result?.serverInfo?.name !== "hicreator") {
|
|
49
|
-
throw new
|
|
90
|
+
throw new McpConnectionError(
|
|
91
|
+
"The hiCreator MCP server returned an unexpected initialize response.",
|
|
92
|
+
{ kind: "protocol" },
|
|
93
|
+
);
|
|
50
94
|
}
|
|
51
95
|
const listed = await request(mcpUrl, apiKey, 2, "tools/list");
|
|
52
96
|
const tools = listed.result?.tools?.map((tool) => tool.name);
|
|
53
97
|
if (!Array.isArray(tools)
|
|
54
98
|
|| EXPECTED_TOOLS.some((tool) => !tools.includes(tool))) {
|
|
55
|
-
throw new
|
|
99
|
+
throw new McpConnectionError(
|
|
100
|
+
"The hiCreator MCP server did not advertise all six tools.",
|
|
101
|
+
{ kind: "protocol" },
|
|
102
|
+
);
|
|
56
103
|
}
|
|
57
104
|
return { server: initialized.result.serverInfo, tools };
|
|
58
105
|
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { emitKeypressEvents } from "node:readline";
|
|
2
|
+
|
|
3
|
+
const API_KEY_PATTERN = /^hc_[A-Za-z0-9_-]+$/u;
|
|
4
|
+
const MIN_API_KEY_LENGTH = 20;
|
|
5
|
+
const MAX_API_KEY_LENGTH = 256;
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_KEY_ATTEMPTS = 3;
|
|
8
|
+
export const DEFAULT_CONNECTION_ATTEMPTS = 3;
|
|
9
|
+
|
|
10
|
+
export function validateApiKey(value) {
|
|
11
|
+
const apiKey = value?.trim();
|
|
12
|
+
if (!apiKey) throw new Error("API Key is required.");
|
|
13
|
+
if (!apiKey.startsWith("hc_")) {
|
|
14
|
+
throw new Error("API Key must start with hc_.");
|
|
15
|
+
}
|
|
16
|
+
if (apiKey.length < MIN_API_KEY_LENGTH
|
|
17
|
+
|| apiKey.length > MAX_API_KEY_LENGTH) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
`API Key must be ${MIN_API_KEY_LENGTH}-${MAX_API_KEY_LENGTH} characters long.`,
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
if (!API_KEY_PATTERN.test(apiKey)) {
|
|
23
|
+
throw new Error(
|
|
24
|
+
"API Key may contain only letters, numbers, underscores, and hyphens.",
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
return apiKey;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function readMaskedSecret({
|
|
31
|
+
input = process.stdin,
|
|
32
|
+
output = process.stderr,
|
|
33
|
+
label = "hiCreator API Key: ",
|
|
34
|
+
initializeKeypress = emitKeypressEvents,
|
|
35
|
+
} = {}) {
|
|
36
|
+
if (!input.isTTY || typeof input.setRawMode !== "function") {
|
|
37
|
+
throw new Error(
|
|
38
|
+
"Set HICREATOR_API_KEY or run setup in an interactive terminal.",
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
initializeKeypress(input);
|
|
43
|
+
const wasRaw = Boolean(input.isRaw);
|
|
44
|
+
let value = "";
|
|
45
|
+
let settled = false;
|
|
46
|
+
output.write(label);
|
|
47
|
+
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const cleanup = () => {
|
|
50
|
+
input.off("keypress", onKeypress);
|
|
51
|
+
input.off("end", onEnd);
|
|
52
|
+
input.off("error", onError);
|
|
53
|
+
if (!wasRaw) input.setRawMode(false);
|
|
54
|
+
input.pause?.();
|
|
55
|
+
};
|
|
56
|
+
const finish = (handler, result) => {
|
|
57
|
+
if (settled) return;
|
|
58
|
+
settled = true;
|
|
59
|
+
cleanup();
|
|
60
|
+
output.write("\n");
|
|
61
|
+
handler(result);
|
|
62
|
+
};
|
|
63
|
+
const onEnd = () => finish(
|
|
64
|
+
reject,
|
|
65
|
+
new Error("API Key input closed before completion."),
|
|
66
|
+
);
|
|
67
|
+
const onError = (error) => finish(reject, error);
|
|
68
|
+
const onKeypress = (text, key = {}) => {
|
|
69
|
+
if (key.ctrl && key.name === "c") {
|
|
70
|
+
finish(reject, new Error("Setup cancelled."));
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (key.name === "return" || key.name === "enter") {
|
|
74
|
+
finish(resolve, value);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (key.name === "backspace" || key.name === "delete") {
|
|
78
|
+
if (value.length > 0) {
|
|
79
|
+
value = value.slice(0, -1);
|
|
80
|
+
output.write("\b \b");
|
|
81
|
+
}
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (key.ctrl || key.meta) return;
|
|
85
|
+
const printable = [...(text ?? "")]
|
|
86
|
+
.filter((character) => character >= " " && character !== "\x7f")
|
|
87
|
+
.join("");
|
|
88
|
+
if (!printable) return;
|
|
89
|
+
value += printable;
|
|
90
|
+
output.write("*".repeat([...printable].length));
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
input.on("keypress", onKeypress);
|
|
94
|
+
input.once("end", onEnd);
|
|
95
|
+
input.once("error", onError);
|
|
96
|
+
if (!wasRaw) input.setRawMode(true);
|
|
97
|
+
input.resume();
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export async function resolveSetupConnection({
|
|
102
|
+
initialApiKey,
|
|
103
|
+
prompt,
|
|
104
|
+
verify,
|
|
105
|
+
report = () => {},
|
|
106
|
+
sleep = (milliseconds) => new Promise((resolve) =>
|
|
107
|
+
setTimeout(resolve, milliseconds)),
|
|
108
|
+
skipVerify = false,
|
|
109
|
+
maxKeyAttempts = DEFAULT_KEY_ATTEMPTS,
|
|
110
|
+
maxConnectionAttempts = DEFAULT_CONNECTION_ATTEMPTS,
|
|
111
|
+
}) {
|
|
112
|
+
const interactive = initialApiKey === undefined && typeof prompt === "function";
|
|
113
|
+
const keyAttempts = interactive ? maxKeyAttempts : 1;
|
|
114
|
+
let candidate = initialApiKey;
|
|
115
|
+
|
|
116
|
+
for (let keyAttempt = 1; keyAttempt <= keyAttempts; keyAttempt += 1) {
|
|
117
|
+
if (candidate === undefined) {
|
|
118
|
+
candidate = await prompt({ attempt: keyAttempt, maxAttempts: keyAttempts });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let apiKey;
|
|
122
|
+
try {
|
|
123
|
+
apiKey = validateApiKey(candidate);
|
|
124
|
+
report("[ok] API Key received; format is valid.");
|
|
125
|
+
} catch (error) {
|
|
126
|
+
if (!interactive || keyAttempt === keyAttempts) throw error;
|
|
127
|
+
report(`[retry] ${error.message}`);
|
|
128
|
+
candidate = undefined;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (skipVerify) return { apiKey, connection: null };
|
|
133
|
+
|
|
134
|
+
for (let connectionAttempt = 1;
|
|
135
|
+
connectionAttempt <= maxConnectionAttempts;
|
|
136
|
+
connectionAttempt += 1) {
|
|
137
|
+
const suffix = connectionAttempt > 1
|
|
138
|
+
? ` (attempt ${connectionAttempt}/${maxConnectionAttempts})`
|
|
139
|
+
: "";
|
|
140
|
+
report(`[3/5] Verifying the MCP connection${suffix}...`);
|
|
141
|
+
try {
|
|
142
|
+
const connection = await verify(apiKey);
|
|
143
|
+
report(`[ok] Connected; ${connection.tools.length} tools are available.`);
|
|
144
|
+
return { apiKey, connection };
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (error?.kind === "auth" && interactive) {
|
|
147
|
+
if (keyAttempt === keyAttempts) throw error;
|
|
148
|
+
report(`[retry] ${error.message} Enter the API Key again.`);
|
|
149
|
+
candidate = undefined;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
const canRetry = error?.kind === "transient"
|
|
153
|
+
&& connectionAttempt < maxConnectionAttempts;
|
|
154
|
+
if (!canRetry) throw error;
|
|
155
|
+
report(`[retry] ${error.message} Retrying the free connection check.`);
|
|
156
|
+
await sleep(500 * connectionAttempt);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
throw new Error("Could not verify the API Key.");
|
|
162
|
+
}
|