@infimind-next/ai-hive-mcp 0.3.0 → 0.3.1
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 +15 -0
- package/dist/api-client.d.ts +24 -0
- package/dist/api-client.js +74 -9
- package/dist/cli.js +2 -0
- package/dist/config.js +10 -2
- package/dist/index.js +0 -0
- package/dist/schemas.d.ts +2 -0
- package/dist/schemas.js +2 -0
- package/dist/server.d.ts +11 -2
- package/dist/server.js +82 -7
- package/dist/update-notifier.d.ts +20 -0
- package/dist/update-notifier.js +46 -0
- package/package.json +8 -9
package/README.md
CHANGED
|
@@ -30,6 +30,10 @@
|
|
|
30
30
|
- `generate_video`:视频预检或正式生成
|
|
31
31
|
- `get_generation_task`:查询生成任务进度和结果
|
|
32
32
|
|
|
33
|
+
### 认证素材模式
|
|
34
|
+
|
|
35
|
+
0.3.1 增加可选的 `get_material_mode`(查询资格与网页授权入口)、`quote_video_materials`(冻结素材并报价),以及 `generate_video` 的 `materialMode` / `materialQuoteId` 字段。这些工具不是生成前置要求;旧版 0.3.0 也可直接使用认证模型。实际发布版本以 npm registry 为准。用户必须登录网页明确授权,MCP 不能代签;确认报价后按认证版模型单价按生成时长计费,认证服务费已内嵌其中,不再另收素材服务费。
|
|
36
|
+
|
|
33
37
|
## 调用流程
|
|
34
38
|
|
|
35
39
|
图片和视频生成固定按 5 步走:
|
|
@@ -46,3 +50,14 @@
|
|
|
46
50
|
- 修正参数、余额或媒体问题后重试:必须使用新的 `clientRequestId`。
|
|
47
51
|
- 正式生成提示价格已更新:保持业务参数和原 `clientRequestId` 不变,重新预检获取最新 `pricingSnapshot` 后重试。
|
|
48
52
|
- 返回 `REQUEST_OUTCOME_UNKNOWN`:停止自动重试,联系管理员核查。
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
### 认证版的用户路径
|
|
56
|
+
|
|
57
|
+
用户提供普通素材即可,后台自动处理认证。使用对应认证模型与当前价格快照,先 dryRun 预检资格、素材和费用;通过后沿用标准版提交格式,认证字段和专用报价工具都可省略。收到任务 ID 后只查询原任务。
|
|
58
|
+
|
|
59
|
+
仅缺少授权、充值资格或余额时向用户说明必要操作,并提供[认证与充值入口](https://ai-hive.iclip.cn/certified-materials#guide);不擅自换模型提交。费用变化时重新确认,预检通过不代表内容审核通过。
|
|
60
|
+
|
|
61
|
+
更新信息仅在用户询问或与当前任务有关时介绍,不向用户转述内部参数、错误码、报价与重试机制。客户端可选升级提示默认静默,仅开启 `--verbose` 时输出维护诊断。技术维护记录见仓库 `docs/openapi/technical-changelog.md`。
|
|
62
|
+
|
|
63
|
+
`recoveryAction` 是给调用端的恢复指引,不要逐字转成用户待办。响应中断用原 `clientRequestId` 和相同参数恢复;明确失败后再次生成,或改变报价/输入时,使用新请求标识。结果未知时先核查,不自动重建付费任务。
|
package/dist/api-client.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { BearerTokenProvider } from "./auth-token-provider.js";
|
|
2
2
|
import { type McpClientInfo } from "./client-info.js";
|
|
3
|
+
import { type UpdateNotifier } from "./update-notifier.js";
|
|
3
4
|
type FetchImpl = typeof fetch;
|
|
4
5
|
type AiHiveApiClientInput = {
|
|
5
6
|
endpoint: string;
|
|
@@ -7,19 +8,42 @@ type AiHiveApiClientInput = {
|
|
|
7
8
|
timeoutMs: number;
|
|
8
9
|
fetchImpl?: FetchImpl;
|
|
9
10
|
clientInfoProvider?: () => McpClientInfo | undefined;
|
|
11
|
+
updateNotifier?: UpdateNotifier;
|
|
10
12
|
};
|
|
13
|
+
export declare class AiHiveApiError extends Error {
|
|
14
|
+
readonly status: number | undefined;
|
|
15
|
+
readonly details?: {
|
|
16
|
+
errorCode: string;
|
|
17
|
+
message: string;
|
|
18
|
+
helpUrl?: string;
|
|
19
|
+
actionUrl?: string;
|
|
20
|
+
retryable?: boolean;
|
|
21
|
+
recoveryAction?: string;
|
|
22
|
+
} | undefined;
|
|
23
|
+
constructor(status: number | undefined, message: string, details?: {
|
|
24
|
+
errorCode: string;
|
|
25
|
+
message: string;
|
|
26
|
+
helpUrl?: string;
|
|
27
|
+
actionUrl?: string;
|
|
28
|
+
retryable?: boolean;
|
|
29
|
+
recoveryAction?: string;
|
|
30
|
+
} | undefined);
|
|
31
|
+
}
|
|
11
32
|
export declare class AiHiveApiClient {
|
|
12
33
|
private readonly endpoint;
|
|
13
34
|
private readonly tokenProvider;
|
|
14
35
|
private readonly timeoutMs;
|
|
15
36
|
private readonly fetchImpl;
|
|
16
37
|
private readonly clientInfoProvider?;
|
|
38
|
+
private readonly updateNotifier?;
|
|
17
39
|
constructor(input: AiHiveApiClientInput);
|
|
18
40
|
private withTimeout;
|
|
19
41
|
private request;
|
|
20
42
|
private send;
|
|
21
43
|
private readResponse;
|
|
22
44
|
getUserInfo(): Promise<unknown>;
|
|
45
|
+
getMaterialMode(): Promise<unknown>;
|
|
46
|
+
quoteVideoMaterials(input: unknown): Promise<unknown>;
|
|
23
47
|
listModels(input: Record<string, unknown>): Promise<unknown>;
|
|
24
48
|
getModel(publicModelId: string, routingMode?: string): Promise<unknown>;
|
|
25
49
|
createMediaUpload(input: unknown): Promise<unknown>;
|
package/dist/api-client.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { encodeMcpClientInfo, MCP_CLIENT_INFO_HEADER, } from "./client-info.js";
|
|
2
|
-
|
|
2
|
+
import { LATEST_CLIENT_VERSION_HEADER, } from "./update-notifier.js";
|
|
3
|
+
export class AiHiveApiError extends Error {
|
|
3
4
|
status;
|
|
4
|
-
|
|
5
|
+
details;
|
|
6
|
+
constructor(status, message, details) {
|
|
5
7
|
super(message);
|
|
6
8
|
this.status = status;
|
|
9
|
+
this.details = details;
|
|
7
10
|
this.name = "AiHiveApiError";
|
|
8
11
|
}
|
|
9
12
|
}
|
|
@@ -41,8 +44,25 @@ function errorMessage(body) {
|
|
|
41
44
|
}
|
|
42
45
|
return "请求失败";
|
|
43
46
|
}
|
|
47
|
+
class AiHiveTimeoutError extends Error {
|
|
48
|
+
constructor(message) {
|
|
49
|
+
super(message);
|
|
50
|
+
this.name = "AiHiveTimeoutError";
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// timeoutError 被 request/putUpload 共用, 覆盖只读接口与大文件上传,
|
|
54
|
+
// 文案必须保持通用: 这里只描述状态, 付费调用专属的幂等恢复口径
|
|
55
|
+
// 在 chatText/requestGeneration 处用 PAID_TIMEOUT_MESSAGE 替换.
|
|
56
|
+
const GENERIC_TIMEOUT_MESSAGE = "等待响应超时,服务端可能仍在处理,请稍后重试";
|
|
57
|
+
// 付费调用超时后服务端可能仍在生成(最终成功则已扣费), MCP/OpenAPI 没有
|
|
58
|
+
// "查询会话结果"的能力, 唯一安全的恢复手段是幂等重调; 改参数重发会被当成
|
|
59
|
+
// 新请求再次扣费, 与"同 prompt 提交 4 次扣 4 次"的客诉同因.
|
|
60
|
+
const PAID_TIMEOUT_MESSAGE = "等待响应超时,服务端可能仍在生成,也可能已完成并扣费。请复用同一 clientRequestId 和相同业务参数重新调用,由服务端幂等去重并返回首次结果;修正参数后必须更换新的 clientRequestId";
|
|
44
61
|
function timeoutError() {
|
|
45
|
-
return new
|
|
62
|
+
return new AiHiveTimeoutError(GENERIC_TIMEOUT_MESSAGE);
|
|
63
|
+
}
|
|
64
|
+
function isTimeoutError(error) {
|
|
65
|
+
return error instanceof AiHiveTimeoutError;
|
|
46
66
|
}
|
|
47
67
|
function redactInternalPricing(value) {
|
|
48
68
|
if (Array.isArray(value)) {
|
|
@@ -67,12 +87,14 @@ export class AiHiveApiClient {
|
|
|
67
87
|
timeoutMs;
|
|
68
88
|
fetchImpl;
|
|
69
89
|
clientInfoProvider;
|
|
90
|
+
updateNotifier;
|
|
70
91
|
constructor(input) {
|
|
71
92
|
this.endpoint = input.endpoint.replace(/\/+$/, "");
|
|
72
93
|
this.tokenProvider = input.tokenProvider;
|
|
73
94
|
this.timeoutMs = input.timeoutMs;
|
|
74
95
|
this.fetchImpl = input.fetchImpl ?? fetch;
|
|
75
96
|
this.clientInfoProvider = input.clientInfoProvider;
|
|
97
|
+
this.updateNotifier = input.updateNotifier;
|
|
76
98
|
}
|
|
77
99
|
async withTimeout(operation) {
|
|
78
100
|
const controller = new AbortController();
|
|
@@ -117,6 +139,9 @@ export class AiHiveApiClient {
|
|
|
117
139
|
response = await this.send(path, init, signal, refreshedToken);
|
|
118
140
|
}
|
|
119
141
|
}
|
|
142
|
+
// 只看最终响应的版本头; 401 重试前的旧响应已废弃, 不重复参与判断.
|
|
143
|
+
// headers 用可选链访问, 兼容测试中的极简 fetch stub.
|
|
144
|
+
this.updateNotifier?.notifyIfOutdated(response.headers?.get(LATEST_CLIENT_VERSION_HEADER) ?? null);
|
|
120
145
|
return this.readResponse(response);
|
|
121
146
|
});
|
|
122
147
|
}
|
|
@@ -138,13 +163,42 @@ export class AiHiveApiClient {
|
|
|
138
163
|
async readResponse(response) {
|
|
139
164
|
const body = await response.json().catch(() => ({}));
|
|
140
165
|
if (!response.ok) {
|
|
141
|
-
|
|
166
|
+
const message = errorMessage(body);
|
|
167
|
+
const details = body &&
|
|
168
|
+
typeof body === "object" &&
|
|
169
|
+
typeof body.errorCode === "string" &&
|
|
170
|
+
body.errorCode.startsWith("ASSET_")
|
|
171
|
+
? {
|
|
172
|
+
errorCode: body.errorCode,
|
|
173
|
+
message,
|
|
174
|
+
retryable: body.retryable === true,
|
|
175
|
+
...(typeof body.recoveryAction === "string"
|
|
176
|
+
? { recoveryAction: body.recoveryAction }
|
|
177
|
+
: {}),
|
|
178
|
+
...(typeof body.helpUrl === "string"
|
|
179
|
+
? { helpUrl: body.helpUrl }
|
|
180
|
+
: {}),
|
|
181
|
+
...(typeof body.actionUrl === "string"
|
|
182
|
+
? { actionUrl: body.actionUrl }
|
|
183
|
+
: {}),
|
|
184
|
+
}
|
|
185
|
+
: undefined;
|
|
186
|
+
throw new AiHiveApiError(response.status, message, details);
|
|
142
187
|
}
|
|
143
188
|
return body;
|
|
144
189
|
}
|
|
145
190
|
getUserInfo() {
|
|
146
191
|
return this.request("/mcp/user-info");
|
|
147
192
|
}
|
|
193
|
+
getMaterialMode() {
|
|
194
|
+
return this.request("/mcp/material-mode");
|
|
195
|
+
}
|
|
196
|
+
quoteVideoMaterials(input) {
|
|
197
|
+
return this.request("/mcp/generation/video/material-quote", {
|
|
198
|
+
method: "POST",
|
|
199
|
+
body: JSON.stringify(input),
|
|
200
|
+
});
|
|
201
|
+
}
|
|
148
202
|
async listModels(input) {
|
|
149
203
|
const query = new URLSearchParams();
|
|
150
204
|
if (typeof input.modelType === "string") {
|
|
@@ -183,11 +237,19 @@ export class AiHiveApiClient {
|
|
|
183
237
|
method: "POST",
|
|
184
238
|
});
|
|
185
239
|
}
|
|
186
|
-
chatText(input) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
240
|
+
async chatText(input) {
|
|
241
|
+
try {
|
|
242
|
+
return await this.request("/mcp/chat/text", {
|
|
243
|
+
method: "POST",
|
|
244
|
+
body: JSON.stringify(input),
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
catch (error) {
|
|
248
|
+
if (isTimeoutError(error)) {
|
|
249
|
+
throw new Error(PAID_TIMEOUT_MESSAGE);
|
|
250
|
+
}
|
|
251
|
+
throw error;
|
|
252
|
+
}
|
|
191
253
|
}
|
|
192
254
|
generateImage(input) {
|
|
193
255
|
return this.requestGeneration("/mcp/generation/image", input);
|
|
@@ -203,6 +265,9 @@ export class AiHiveApiClient {
|
|
|
203
265
|
});
|
|
204
266
|
}
|
|
205
267
|
catch (error) {
|
|
268
|
+
if (isTimeoutError(error)) {
|
|
269
|
+
throw new Error(PAID_TIMEOUT_MESSAGE);
|
|
270
|
+
}
|
|
206
271
|
if (typeof input === "object" &&
|
|
207
272
|
input !== null &&
|
|
208
273
|
input.dryRun === true &&
|
package/dist/cli.js
CHANGED
|
@@ -295,8 +295,10 @@ async function runMcp(config, dependencies) {
|
|
|
295
295
|
try {
|
|
296
296
|
const resolved = await resolveServerAuth(config, dependencies.createStore(config.credentialsFile));
|
|
297
297
|
await dependencies.runServer({
|
|
298
|
+
...(config.verbose ? { verbose: true } : {}),
|
|
298
299
|
...resolved,
|
|
299
300
|
timeoutMs: config.timeoutMs,
|
|
301
|
+
webOrigin: config.webOrigin,
|
|
300
302
|
});
|
|
301
303
|
return 0;
|
|
302
304
|
}
|
package/dist/config.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { resolveWorkBuddyCredentialsFile } from "./workbuddy-credentials.js";
|
|
2
2
|
const DEFAULT_ENDPOINT = "https://ai-hive.iclip.cn/api";
|
|
3
3
|
const DEFAULT_WEB_ORIGIN = "https://ai-hive.iclip.cn";
|
|
4
|
+
// 文本对话由上游同步返回, 长推理模型实测可达 157s+ (gpt-6/claude-sonnet 系列).
|
|
5
|
+
// 旧默认 120s 会在服务端仍在生成时先断开客户端连接, 用户看到"超时失败"、
|
|
6
|
+
// 服务端却成功扣费. 现取 300s, 仍小于网关 600s 读超时, 留足余量.
|
|
7
|
+
const DEFAULT_TIMEOUT_MS = 300_000;
|
|
4
8
|
const CLI_COMMANDS = new Set([
|
|
5
9
|
"server",
|
|
6
10
|
"auth",
|
|
@@ -46,7 +50,9 @@ export function parseConfig(input) {
|
|
|
46
50
|
values.set(name, value);
|
|
47
51
|
index += 1;
|
|
48
52
|
}
|
|
49
|
-
const timeoutMs = Number.parseInt(values.get("timeout-ms") ??
|
|
53
|
+
const timeoutMs = Number.parseInt(values.get("timeout-ms") ??
|
|
54
|
+
input.env.AI_HIVE_MCP_TIMEOUT_MS ??
|
|
55
|
+
String(DEFAULT_TIMEOUT_MS), 10);
|
|
50
56
|
const endpointOverride = values.get("endpoint") ?? input.env.AI_HIVE_API_ENDPOINT;
|
|
51
57
|
return {
|
|
52
58
|
command,
|
|
@@ -58,7 +64,9 @@ export function parseConfig(input) {
|
|
|
58
64
|
key: values.get("key") ?? input.env.AI_HIVE_MCP_KEY ?? null,
|
|
59
65
|
credentialsFile: values.get("credentials-file") ??
|
|
60
66
|
resolveWorkBuddyCredentialsFile(input.env),
|
|
61
|
-
timeoutMs: Number.isFinite(timeoutMs) && timeoutMs > 0
|
|
67
|
+
timeoutMs: Number.isFinite(timeoutMs) && timeoutMs > 0
|
|
68
|
+
? timeoutMs
|
|
69
|
+
: DEFAULT_TIMEOUT_MS,
|
|
62
70
|
verbose: flags.has("verbose") || input.env.AI_HIVE_MCP_VERBOSE === "true",
|
|
63
71
|
};
|
|
64
72
|
}
|
package/dist/index.js
CHANGED
|
File without changes
|
package/dist/schemas.d.ts
CHANGED
|
@@ -94,6 +94,8 @@ export declare const generateImageShape: {
|
|
|
94
94
|
pricingSnapshot: z.ZodOptional<z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodArray<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>, "many">, z.ZodString, z.ZodNull]>>;
|
|
95
95
|
};
|
|
96
96
|
export declare const generateVideoShape: {
|
|
97
|
+
materialMode: z.ZodOptional<z.ZodEnum<["standard", "certified"]>>;
|
|
98
|
+
materialQuoteId: z.ZodOptional<z.ZodString>;
|
|
97
99
|
publicModelId: z.ZodString;
|
|
98
100
|
routingMode: z.ZodEnum<["COST_FIRST", "SPEED_FIRST", "SUCCESS_FIRST"]>;
|
|
99
101
|
prompt: z.ZodEffects<z.ZodString, string, string>;
|
package/dist/schemas.js
CHANGED
|
@@ -100,6 +100,8 @@ export const generateImageShape = {
|
|
|
100
100
|
pricingSnapshot: pricingSnapshotInputSchema,
|
|
101
101
|
};
|
|
102
102
|
export const generateVideoShape = {
|
|
103
|
+
materialMode: z.enum(["standard", "certified"]).optional(),
|
|
104
|
+
materialQuoteId: z.string().min(1).optional(),
|
|
103
105
|
publicModelId: z.string().min(1),
|
|
104
106
|
routingMode: routingModeSchema,
|
|
105
107
|
prompt: nonBlankStringSchema,
|
package/dist/server.d.ts
CHANGED
|
@@ -4,14 +4,14 @@ import { AiHiveApiClient } from "./api-client.js";
|
|
|
4
4
|
import { type BearerTokenProvider } from "./auth-token-provider.js";
|
|
5
5
|
import type { McpClientConfig } from "./config.js";
|
|
6
6
|
import { WorkBuddyCredentialStore } from "./workbuddy-credentials.js";
|
|
7
|
-
export declare const SERVER_VERSION = "0.3.
|
|
7
|
+
export declare const SERVER_VERSION = "0.3.1";
|
|
8
8
|
declare function content(value: unknown): {
|
|
9
9
|
content: {
|
|
10
10
|
type: "text";
|
|
11
11
|
text: string;
|
|
12
12
|
}[];
|
|
13
13
|
};
|
|
14
|
-
type ApiClientLike = Pick<AiHiveApiClient, "getUserInfo" | "listModels" | "getModel" | "createMediaUpload" | "completeMediaUpload" | "chatText" | "generateImage" | "generateVideo" | "getGenerationTask" | "putUpload">;
|
|
14
|
+
type ApiClientLike = Pick<AiHiveApiClient, "getUserInfo" | "getMaterialMode" | "quoteVideoMaterials" | "listModels" | "getModel" | "createMediaUpload" | "completeMediaUpload" | "chatText" | "generateImage" | "generateVideo" | "getGenerationTask" | "putUpload">;
|
|
15
15
|
export type ToolDefinition = {
|
|
16
16
|
name: string;
|
|
17
17
|
description: string;
|
|
@@ -24,14 +24,23 @@ export type ResolvedServerAuth = {
|
|
|
24
24
|
tokenProvider: BearerTokenProvider;
|
|
25
25
|
};
|
|
26
26
|
export declare function resolveServerAuth(config: McpClientConfig, store: WorkBuddyCredentialStore): Promise<ResolvedServerAuth>;
|
|
27
|
+
/**
|
|
28
|
+
* 与 @ai-hive/contracts 的 buildMcpServerInstructions 内容对应;
|
|
29
|
+
* mcp-client 独立发布不依赖 contracts, 修改时两边同步.
|
|
30
|
+
*/
|
|
31
|
+
export declare function buildServerInstructions(webOrigin?: string): string;
|
|
27
32
|
export declare function createServer(input: {
|
|
28
33
|
endpoint: string;
|
|
29
34
|
tokenProvider: BearerTokenProvider;
|
|
30
35
|
timeoutMs: number;
|
|
36
|
+
webOrigin?: string;
|
|
37
|
+
verbose?: boolean;
|
|
31
38
|
}): McpServer;
|
|
32
39
|
export declare function runServer(input: {
|
|
33
40
|
endpoint: string;
|
|
34
41
|
tokenProvider: BearerTokenProvider;
|
|
35
42
|
timeoutMs: number;
|
|
43
|
+
webOrigin?: string;
|
|
44
|
+
verbose?: boolean;
|
|
36
45
|
}): Promise<void>;
|
|
37
46
|
export {};
|
package/dist/server.js
CHANGED
|
@@ -2,12 +2,13 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
|
2
2
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
3
|
import { chatTextShape, generateImageShape, generateVideoShape, getGenerationTaskShape, getModelShape, listModelsShape, } from "./schemas.js";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
-
import { AiHiveApiClient } from "./api-client.js";
|
|
5
|
+
import { AiHiveApiClient, AiHiveApiError } from "./api-client.js";
|
|
6
6
|
import { normalizeApiEndpoint, StaticBearerTokenProvider, WorkBuddyTokenProvider, } from "./auth-token-provider.js";
|
|
7
7
|
import { readMcpClientInfo } from "./client-info.js";
|
|
8
|
+
import { createUpdateNotifier } from "./update-notifier.js";
|
|
8
9
|
import { uploadMediaFromPath } from "./local-media.js";
|
|
9
10
|
import { WorkBuddyOAuthClient } from "./workbuddy-oauth-client.js";
|
|
10
|
-
export const SERVER_VERSION = "0.3.
|
|
11
|
+
export const SERVER_VERSION = "0.3.1";
|
|
11
12
|
const DEPRECATED_ATTACHMENT_IDS_MESSAGE = "attachmentIds 已废弃,请使用 messages[].mediaIds";
|
|
12
13
|
const SNAPSHOT_PARSE_FAILURE_MESSAGE = "pricingSnapshot 格式不正确,应为 list_models 返回的快照对象,也可以省略该字段由客户端自动获取";
|
|
13
14
|
const SNAPSHOT_ARRAY_MISMATCH_MESSAGE = "pricingSnapshot 数组中没有匹配当前模型和 routingMode 的快照,请传入 list_models 返回的对应快照对象,或省略该字段由客户端自动获取";
|
|
@@ -100,11 +101,14 @@ async function resolvePricingSnapshot(client, modelType, args) {
|
|
|
100
101
|
}
|
|
101
102
|
// 幂等键语义写在每个付费工具的描述里, 让宿主在选型阶段就能看到重试规则.
|
|
102
103
|
const CLIENT_REQUEST_ID_NOTE = "付费调用应传入稳定 UUID 作为 clientRequestId 幂等键;仅传输中断、REQUEST_IN_PROGRESS 或刷新过期 pricingSnapshot 时复用原值和相同业务参数。余额、参数、媒体或已缓存的模型调用失败在修正后必须使用新的 clientRequestId;REQUEST_OUTCOME_UNKNOWN 必须停止自动重试并人工核查。";
|
|
104
|
+
// 长耗时提醒同样前置到工具描述: 宿主据此设置超时, 避免"本端超时先断、
|
|
105
|
+
// 服务端跑完仍扣费"被误读成失败扣款.
|
|
106
|
+
const LONG_RUNNING_NOTE = "该调用同步返回,长推理模型单次可能耗时 1-3 分钟,客户端超时应设为 300 秒以上;等待超时只代表本端不再等待,服务端可能仍在生成,也可能已完成并扣费,超时后复用同一 clientRequestId 和相同业务参数重新调用即可取回首次结果,修正参数后换新值。";
|
|
103
107
|
export function createToolDefinitions(client) {
|
|
104
108
|
return [
|
|
105
109
|
{
|
|
106
110
|
name: "get_user_info",
|
|
107
|
-
description: "
|
|
111
|
+
description: "查询当前用户、余额,以及官网地址和充值入口;余额不足时引导用户打开 rechargeUrl 完成充值后重试。",
|
|
108
112
|
schema: {},
|
|
109
113
|
handler: async () => content(await client().getUserInfo()),
|
|
110
114
|
},
|
|
@@ -137,7 +141,7 @@ export function createToolDefinitions(client) {
|
|
|
137
141
|
},
|
|
138
142
|
{
|
|
139
143
|
name: "chat_text",
|
|
140
|
-
description: `使用指定模型生成文本。${CLIENT_REQUEST_ID_NOTE}`,
|
|
144
|
+
description: `使用指定模型生成文本。${LONG_RUNNING_NOTE}${CLIENT_REQUEST_ID_NOTE}`,
|
|
141
145
|
schema: chatTextShape,
|
|
142
146
|
handler: async (args) => {
|
|
143
147
|
rejectDeprecatedAttachmentIds(args);
|
|
@@ -156,13 +160,43 @@ export function createToolDefinitions(client) {
|
|
|
156
160
|
},
|
|
157
161
|
{
|
|
158
162
|
name: "generate_video",
|
|
159
|
-
description:
|
|
163
|
+
description: `认证版接收普通素材,服务端按模型 ID 自动校验资格和准备素材。提交前先用 dryRun=true 做只读预检,不用真实付费提交试探资格。materialMode、materialQuoteId 和专用报价工具均为可选兼容能力;正式提交使用当前 pricingSnapshot 和稳定 clientRequestId。资格不足时明确说明差额或待授权事项,并在回复中展示返回的 helpUrl 或 actionUrl 为可点击链接;未返回链接时使用 https://ai-hive.iclip.cn/certified-materials#guide。保留用户指定模型,不擅自切换模型或继续付费提交。认证服务已包含在模型价格中;费用变化时重新确认。收到任务 ID 后查询原任务,不重复创建。dryRun 不创建任务、不冻结素材、不预占或扣费,也不执行供应商内容审核。${CLIENT_REQUEST_ID_NOTE}`,
|
|
160
164
|
schema: generateVideoShape,
|
|
161
165
|
handler: async (args) => {
|
|
162
166
|
const body = await resolvePricingSnapshot(client, "VIDEO", args);
|
|
163
167
|
return content(await client().generateVideo(body));
|
|
164
168
|
},
|
|
165
169
|
},
|
|
170
|
+
{
|
|
171
|
+
name: "get_material_mode",
|
|
172
|
+
description: "查询认证版服务状态、授权与累计充值资格。已满足条件时直接继续;仅未授权时请用户本人打开 actionUrl 阅读并同意,不得代替用户授权。累计充值资格与钱包余额分别检查。",
|
|
173
|
+
schema: {},
|
|
174
|
+
handler: async () => content(await client().getMaterialMode()),
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
name: "quote_video_materials",
|
|
178
|
+
description: "上传普通素材后,由调用端用与生成相同的模型、参数及媒体 ID 获取报价,包含参考图片、视频、首帧和尾帧。无需用户手动认证素材。认证服务已包含在模型价格中,不另收素材费;向用户说明必要费用,费用或计费条件变化时重新确认。此工具可选,返回 id 可用作 materialQuoteId;未提供报价时服务端自动准备。显式报价过期时保留原素材重新获取。",
|
|
179
|
+
schema: {
|
|
180
|
+
publicModelId: generateVideoShape.publicModelId,
|
|
181
|
+
routingMode: generateVideoShape.routingMode,
|
|
182
|
+
prompt: generateVideoShape.prompt,
|
|
183
|
+
imageMediaIds: generateVideoShape.imageMediaIds,
|
|
184
|
+
videoMediaIds: generateVideoShape.videoMediaIds,
|
|
185
|
+
audioMediaIds: generateVideoShape.audioMediaIds,
|
|
186
|
+
firstFrameMediaId: generateVideoShape.firstFrameMediaId,
|
|
187
|
+
lastFrameMediaId: generateVideoShape.lastFrameMediaId,
|
|
188
|
+
pricingSnapshot: generateVideoShape.pricingSnapshot,
|
|
189
|
+
params: generateVideoShape.params,
|
|
190
|
+
},
|
|
191
|
+
handler: async (args) => {
|
|
192
|
+
const body = await resolvePricingSnapshot(client, "VIDEO", {
|
|
193
|
+
...args,
|
|
194
|
+
dryRun: true,
|
|
195
|
+
});
|
|
196
|
+
delete body.dryRun;
|
|
197
|
+
return content(await client().quoteVideoMaterials(body));
|
|
198
|
+
},
|
|
199
|
+
},
|
|
166
200
|
{
|
|
167
201
|
name: "get_generation_task",
|
|
168
202
|
description: "查询图片或视频任务状态和结果。",
|
|
@@ -204,16 +238,57 @@ export async function resolveServerAuth(config, store) {
|
|
|
204
238
|
}),
|
|
205
239
|
};
|
|
206
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* 与 @ai-hive/contracts 的 buildMcpServerInstructions 内容对应;
|
|
243
|
+
* mcp-client 独立发布不依赖 contracts, 修改时两边同步.
|
|
244
|
+
*/
|
|
245
|
+
export function buildServerInstructions(webOrigin) {
|
|
246
|
+
const lines = [
|
|
247
|
+
"AI Hive 是统一的大模型调用平台,按钱包余额扣费。",
|
|
248
|
+
"dryRun 的 readyToGenerate=true 与 mediaValidation=VERIFIED 仅表示参数和媒体预检结果,未执行人像或内容审核,不保证生成成功。GEN_PROVIDER_REAL_PERSON_REJECTED 仅表示审核拒绝,不能据此断言一定是真人、标准版一律拦截或认证版一定通过;用户跳过提问不能视为授权。价格以当前报价和 pricingSnapshot 为准,不按历史倍数计算;余额相同不能单独证明未扣费。",
|
|
249
|
+
"认证版接收普通素材,服务端按模型 ID 自动校验资格和准备素材。提交前先用 dryRun=true 做只读预检,不用真实付费提交试探资格。materialMode、materialQuoteId 和专用报价工具均为可选兼容能力;正式提交使用当前 pricingSnapshot 和稳定 clientRequestId。资格不足时明确说明差额或待授权事项,并在回复中展示返回的 helpUrl 或 actionUrl 为可点击链接;未返回链接时使用 https://ai-hive.iclip.cn/certified-materials#guide。保留用户指定模型,不擅自切换模型或继续付费提交。认证服务已包含在模型价格中;费用变化时重新确认。收到任务 ID 后查询原任务,不重复创建。",
|
|
250
|
+
"latestChange、changelog 和升级信息仅在用户主动询问或与当前任务直接相关时介绍。对用户只说明实际能力、费用、限制、结果和必要操作,不转述字段名、错误码、报价 ID、幂等机制、版本适配或内部排查过程;技术字段和 recoveryAction 供调用端处理,不能替代用户操作说明。用户明确排查接入问题时可以解释所需技术细节。",
|
|
251
|
+
"先用 get_user_info 查询余额;返回里带官网地址和充值入口,余额不足时引导用户打开 rechargeUrl 充值后重试。",
|
|
252
|
+
"调用 chat_text、generate_image、generate_video 前,用 get_model 确认参数契约;正式调用必须传稳定的 clientRequestId 幂等键,修正参数后换新值。",
|
|
253
|
+
"chat_text 同步返回,长推理模型单次可能耗时 1-3 分钟,客户端超时应设为 300 秒以上;等待超时只代表本端不再等待,服务端可能仍在生成,也可能已完成并扣费,超时后复用同一 clientRequestId 和相同业务参数重新调用即可取回首次结果,修正参数后换新值。",
|
|
254
|
+
];
|
|
255
|
+
lines.push(webOrigin
|
|
256
|
+
? `产品更新见 ${webOrigin}/changelog。`
|
|
257
|
+
: "产品更新见官网更新日志页。");
|
|
258
|
+
return lines.join("\n");
|
|
259
|
+
}
|
|
207
260
|
export function createServer(input) {
|
|
208
|
-
|
|
261
|
+
// instructions 随 initialize 握手注入宿主上下文, 是裸 MCP 缺少技能时的基础知识来源.
|
|
262
|
+
const server = new McpServer({ name: "ai-hive", version: SERVER_VERSION }, { instructions: buildServerInstructions(input.webOrigin) });
|
|
263
|
+
const updateNotifier = createUpdateNotifier({
|
|
264
|
+
currentVersion: SERVER_VERSION,
|
|
265
|
+
enabled: input.verbose === true,
|
|
266
|
+
endpoint: input.endpoint,
|
|
267
|
+
});
|
|
209
268
|
const client = () => new AiHiveApiClient({
|
|
210
269
|
endpoint: input.endpoint,
|
|
211
270
|
tokenProvider: input.tokenProvider,
|
|
212
271
|
timeoutMs: input.timeoutMs,
|
|
213
272
|
clientInfoProvider: () => readMcpClientInfo(server),
|
|
273
|
+
updateNotifier,
|
|
214
274
|
});
|
|
215
275
|
for (const tool of createToolDefinitions(client)) {
|
|
216
|
-
server.tool(tool.name, tool.description, tool.schema,
|
|
276
|
+
server.tool(tool.name, tool.description, tool.schema, async (args) => {
|
|
277
|
+
try {
|
|
278
|
+
return await tool.handler(args);
|
|
279
|
+
}
|
|
280
|
+
catch (error) {
|
|
281
|
+
if (error instanceof AiHiveApiError && error.details)
|
|
282
|
+
return {
|
|
283
|
+
isError: true,
|
|
284
|
+
structuredContent: error.details,
|
|
285
|
+
content: [
|
|
286
|
+
{ type: "text", text: JSON.stringify(error.details) },
|
|
287
|
+
],
|
|
288
|
+
};
|
|
289
|
+
throw error;
|
|
290
|
+
}
|
|
291
|
+
});
|
|
217
292
|
}
|
|
218
293
|
return server;
|
|
219
294
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 与服务端 contracts 的 LATEST_CLIENT_VERSION_HEADER 保持一致;
|
|
3
|
+
* mcp-client 独立发布到 npm, 不依赖 @ai-hive/contracts, 常量需手工同步.
|
|
4
|
+
*/
|
|
5
|
+
export declare const LATEST_CLIENT_VERSION_HEADER = "x-ai-hive-latest-client-version";
|
|
6
|
+
export type UpdateNotifier = {
|
|
7
|
+
notifyIfOutdated(latestVersion: string | null): void;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* 仅显式开启维护诊断时, 按服务端响应头在 stderr 一次性提醒可选升级.
|
|
11
|
+
* 只写 stderr, 不碰 stdout 的 MCP 协议通道; 提醒过就不再重复, 避免刷屏.
|
|
12
|
+
*/
|
|
13
|
+
export declare function createUpdateNotifier(input: {
|
|
14
|
+
enabled?: boolean;
|
|
15
|
+
currentVersion: string;
|
|
16
|
+
endpoint: string;
|
|
17
|
+
write?: (line: string) => void;
|
|
18
|
+
}): UpdateNotifier;
|
|
19
|
+
/** 只比较 x.y.z 数字段; 非数字段按 0 处理, 不被预发布标签绊倒. */
|
|
20
|
+
export declare function compareVersions(a: string, b: string): number;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 与服务端 contracts 的 LATEST_CLIENT_VERSION_HEADER 保持一致;
|
|
3
|
+
* mcp-client 独立发布到 npm, 不依赖 @ai-hive/contracts, 常量需手工同步.
|
|
4
|
+
*/
|
|
5
|
+
export const LATEST_CLIENT_VERSION_HEADER = "x-ai-hive-latest-client-version";
|
|
6
|
+
/**
|
|
7
|
+
* 仅显式开启维护诊断时, 按服务端响应头在 stderr 一次性提醒可选升级.
|
|
8
|
+
* 只写 stderr, 不碰 stdout 的 MCP 协议通道; 提醒过就不再重复, 避免刷屏.
|
|
9
|
+
*/
|
|
10
|
+
export function createUpdateNotifier(input) {
|
|
11
|
+
const write = input.write ?? ((line) => console.error(line));
|
|
12
|
+
let notified = false;
|
|
13
|
+
return {
|
|
14
|
+
notifyIfOutdated(latestVersion) {
|
|
15
|
+
if (!input.enabled || notified || !latestVersion) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (compareVersions(latestVersion, input.currentVersion) <= 0) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
notified = true;
|
|
22
|
+
write(`[ai-hive-mcp] 新版本 ${latestVersion} 已发布(当前 ${input.currentVersion}),新工具和改进见更新日志:${changelogUrl(input.endpoint)}。升级命令:npm install -g @infimind-next/ai-hive-mcp@latest`);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** 只比较 x.y.z 数字段; 非数字段按 0 处理, 不被预发布标签绊倒. */
|
|
27
|
+
export function compareVersions(a, b) {
|
|
28
|
+
const pa = a.split(".").map((part) => Number.parseInt(part, 10) || 0);
|
|
29
|
+
const pb = b.split(".").map((part) => Number.parseInt(part, 10) || 0);
|
|
30
|
+
for (let index = 0; index < 3; index += 1) {
|
|
31
|
+
const diff = (pa[index] ?? 0) - (pb[index] ?? 0);
|
|
32
|
+
if (diff !== 0) {
|
|
33
|
+
return diff;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
/** API 与站点同域部署, 由 endpoint 推导更新日志页; 推导失败时回退正式环境. */
|
|
39
|
+
function changelogUrl(endpoint) {
|
|
40
|
+
try {
|
|
41
|
+
return `${new URL(endpoint).origin}/changelog`;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return "https://ai-hive.iclip.cn/changelog";
|
|
45
|
+
}
|
|
46
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infimind-next/ai-hive-mcp",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -12,13 +12,6 @@
|
|
|
12
12
|
"files": [
|
|
13
13
|
"dist"
|
|
14
14
|
],
|
|
15
|
-
"scripts": {
|
|
16
|
-
"build": "tsc -p tsconfig.json",
|
|
17
|
-
"dev": "tsc -p tsconfig.json --watch --preserveWatchOutput",
|
|
18
|
-
"lint": "eslint \"src/**/*.ts\"",
|
|
19
|
-
"prepack": "pnpm run build",
|
|
20
|
-
"test": "vitest run"
|
|
21
|
-
},
|
|
22
15
|
"dependencies": {
|
|
23
16
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
24
17
|
"file-type": "^21.3.4",
|
|
@@ -29,5 +22,11 @@
|
|
|
29
22
|
"typescript": "^5.0.0",
|
|
30
23
|
"typescript-eslint": "^8.0.0",
|
|
31
24
|
"vitest": "^3.0.0"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc -p tsconfig.json",
|
|
28
|
+
"dev": "tsc -p tsconfig.json --watch --preserveWatchOutput",
|
|
29
|
+
"lint": "eslint \"src/**/*.ts\"",
|
|
30
|
+
"test": "vitest run"
|
|
32
31
|
}
|
|
33
|
-
}
|
|
32
|
+
}
|