api2mcp 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -0
- package/dist/{chunk-ASNCNAYJ.mjs → chunk-6YWTIHKQ.mjs} +38 -12
- package/dist/chunk-6YWTIHKQ.mjs.map +1 -0
- package/dist/cli.js +42 -12
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +6 -2
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.mts +8 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +37 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
- package/dist/chunk-ASNCNAYJ.mjs.map +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -29,6 +29,8 @@ interface ApiSourceConfig {
|
|
|
29
29
|
toolPrefix?: string;
|
|
30
30
|
/** 工作模式:default(默认)或 ondemand(按需) */
|
|
31
31
|
mode?: Mode;
|
|
32
|
+
/** 固定参数(预填充到路径/查询参数中,不暴露给 LLM) */
|
|
33
|
+
fixedParams?: Record<string, string>;
|
|
32
34
|
}
|
|
33
35
|
/**
|
|
34
36
|
* 完整配置
|
|
@@ -45,6 +47,7 @@ declare const CliArgsSchema: z.ZodObject<{
|
|
|
45
47
|
baseUrl: z.ZodOptional<z.ZodString>;
|
|
46
48
|
timeout: z.ZodOptional<z.ZodNumber>;
|
|
47
49
|
headers: z.ZodOptional<z.ZodString>;
|
|
50
|
+
fixedParams: z.ZodOptional<z.ZodString>;
|
|
48
51
|
prefix: z.ZodOptional<z.ZodString>;
|
|
49
52
|
debug: z.ZodOptional<z.ZodBoolean>;
|
|
50
53
|
mode: z.ZodOptional<z.ZodEnum<["default", "ondemand"]>>;
|
|
@@ -54,6 +57,7 @@ declare const CliArgsSchema: z.ZodObject<{
|
|
|
54
57
|
baseUrl?: string | undefined;
|
|
55
58
|
timeout?: number | undefined;
|
|
56
59
|
headers?: string | undefined;
|
|
60
|
+
fixedParams?: string | undefined;
|
|
57
61
|
prefix?: string | undefined;
|
|
58
62
|
mode?: "default" | "ondemand" | undefined;
|
|
59
63
|
}, {
|
|
@@ -62,6 +66,7 @@ declare const CliArgsSchema: z.ZodObject<{
|
|
|
62
66
|
baseUrl?: string | undefined;
|
|
63
67
|
timeout?: number | undefined;
|
|
64
68
|
headers?: string | undefined;
|
|
69
|
+
fixedParams?: string | undefined;
|
|
65
70
|
prefix?: string | undefined;
|
|
66
71
|
mode?: "default" | "ondemand" | undefined;
|
|
67
72
|
}>;
|
|
@@ -74,6 +79,7 @@ interface EnvConfig {
|
|
|
74
79
|
API_BASE_URL?: string;
|
|
75
80
|
API_TIMEOUT?: string;
|
|
76
81
|
API_HEADERS?: string;
|
|
82
|
+
API_FIXED_PARAMS?: string;
|
|
77
83
|
DEBUG?: string;
|
|
78
84
|
}
|
|
79
85
|
|
|
@@ -258,11 +264,11 @@ interface GeneratedTool {
|
|
|
258
264
|
/**
|
|
259
265
|
* 从 OpenAPI 操作生成 MCP 工具
|
|
260
266
|
*/
|
|
261
|
-
declare function generateTool(operation: OpenApiOperation, components?: Record<string, OpenApiSchema>, toolPrefix?: string): GeneratedTool;
|
|
267
|
+
declare function generateTool(operation: OpenApiOperation, components?: Record<string, OpenApiSchema>, toolPrefix?: string, fixedParams?: Record<string, string>): GeneratedTool;
|
|
262
268
|
/**
|
|
263
269
|
* 批量生成工具
|
|
264
270
|
*/
|
|
265
|
-
declare function generateTools(operations: OpenApiOperation[], components?: Record<string, OpenApiSchema>, toolPrefix?: string): GeneratedTool[];
|
|
271
|
+
declare function generateTools(operations: OpenApiOperation[], components?: Record<string, OpenApiSchema>, toolPrefix?: string, fixedParams?: Record<string, string>): GeneratedTool[];
|
|
266
272
|
|
|
267
273
|
/**
|
|
268
274
|
* HTTP 客户端
|
package/dist/index.d.ts
CHANGED
|
@@ -29,6 +29,8 @@ interface ApiSourceConfig {
|
|
|
29
29
|
toolPrefix?: string;
|
|
30
30
|
/** 工作模式:default(默认)或 ondemand(按需) */
|
|
31
31
|
mode?: Mode;
|
|
32
|
+
/** 固定参数(预填充到路径/查询参数中,不暴露给 LLM) */
|
|
33
|
+
fixedParams?: Record<string, string>;
|
|
32
34
|
}
|
|
33
35
|
/**
|
|
34
36
|
* 完整配置
|
|
@@ -45,6 +47,7 @@ declare const CliArgsSchema: z.ZodObject<{
|
|
|
45
47
|
baseUrl: z.ZodOptional<z.ZodString>;
|
|
46
48
|
timeout: z.ZodOptional<z.ZodNumber>;
|
|
47
49
|
headers: z.ZodOptional<z.ZodString>;
|
|
50
|
+
fixedParams: z.ZodOptional<z.ZodString>;
|
|
48
51
|
prefix: z.ZodOptional<z.ZodString>;
|
|
49
52
|
debug: z.ZodOptional<z.ZodBoolean>;
|
|
50
53
|
mode: z.ZodOptional<z.ZodEnum<["default", "ondemand"]>>;
|
|
@@ -54,6 +57,7 @@ declare const CliArgsSchema: z.ZodObject<{
|
|
|
54
57
|
baseUrl?: string | undefined;
|
|
55
58
|
timeout?: number | undefined;
|
|
56
59
|
headers?: string | undefined;
|
|
60
|
+
fixedParams?: string | undefined;
|
|
57
61
|
prefix?: string | undefined;
|
|
58
62
|
mode?: "default" | "ondemand" | undefined;
|
|
59
63
|
}, {
|
|
@@ -62,6 +66,7 @@ declare const CliArgsSchema: z.ZodObject<{
|
|
|
62
66
|
baseUrl?: string | undefined;
|
|
63
67
|
timeout?: number | undefined;
|
|
64
68
|
headers?: string | undefined;
|
|
69
|
+
fixedParams?: string | undefined;
|
|
65
70
|
prefix?: string | undefined;
|
|
66
71
|
mode?: "default" | "ondemand" | undefined;
|
|
67
72
|
}>;
|
|
@@ -74,6 +79,7 @@ interface EnvConfig {
|
|
|
74
79
|
API_BASE_URL?: string;
|
|
75
80
|
API_TIMEOUT?: string;
|
|
76
81
|
API_HEADERS?: string;
|
|
82
|
+
API_FIXED_PARAMS?: string;
|
|
77
83
|
DEBUG?: string;
|
|
78
84
|
}
|
|
79
85
|
|
|
@@ -258,11 +264,11 @@ interface GeneratedTool {
|
|
|
258
264
|
/**
|
|
259
265
|
* 从 OpenAPI 操作生成 MCP 工具
|
|
260
266
|
*/
|
|
261
|
-
declare function generateTool(operation: OpenApiOperation, components?: Record<string, OpenApiSchema>, toolPrefix?: string): GeneratedTool;
|
|
267
|
+
declare function generateTool(operation: OpenApiOperation, components?: Record<string, OpenApiSchema>, toolPrefix?: string, fixedParams?: Record<string, string>): GeneratedTool;
|
|
262
268
|
/**
|
|
263
269
|
* 批量生成工具
|
|
264
270
|
*/
|
|
265
|
-
declare function generateTools(operations: OpenApiOperation[], components?: Record<string, OpenApiSchema>, toolPrefix?: string): GeneratedTool[];
|
|
271
|
+
declare function generateTools(operations: OpenApiOperation[], components?: Record<string, OpenApiSchema>, toolPrefix?: string, fixedParams?: Record<string, string>): GeneratedTool[];
|
|
266
272
|
|
|
267
273
|
/**
|
|
268
274
|
* HTTP 客户端
|
package/dist/index.js
CHANGED
|
@@ -163,6 +163,20 @@ function parseHeaders(headersStr) {
|
|
|
163
163
|
);
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
|
+
function parseFixedParams(paramsStr) {
|
|
167
|
+
if (!paramsStr) return void 0;
|
|
168
|
+
try {
|
|
169
|
+
const parsed = JSON.parse(paramsStr);
|
|
170
|
+
if (typeof parsed === "object" && parsed !== null) {
|
|
171
|
+
return parsed;
|
|
172
|
+
}
|
|
173
|
+
throw new Error("Fixed params must be a JSON object");
|
|
174
|
+
} catch (error) {
|
|
175
|
+
throw new ConfigurationError(
|
|
176
|
+
`Invalid fixedParams JSON: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
166
180
|
function loadFromEnv(env) {
|
|
167
181
|
const config = {};
|
|
168
182
|
if (env.OPENAPI_URL) {
|
|
@@ -180,6 +194,9 @@ function loadFromEnv(env) {
|
|
|
180
194
|
if (env.API_HEADERS) {
|
|
181
195
|
config.headers = parseHeaders(env.API_HEADERS);
|
|
182
196
|
}
|
|
197
|
+
if (env.API_FIXED_PARAMS) {
|
|
198
|
+
config.fixedParams = parseFixedParams(env.API_FIXED_PARAMS);
|
|
199
|
+
}
|
|
183
200
|
if (env.DEBUG) {
|
|
184
201
|
config.debug = env.DEBUG === "true" || env.DEBUG === "1";
|
|
185
202
|
}
|
|
@@ -198,6 +215,7 @@ function loadFromFile(workingDir = process.cwd()) {
|
|
|
198
215
|
baseUrl: parsed.baseUrl,
|
|
199
216
|
timeout: parsed.timeout,
|
|
200
217
|
headers: parsed.headers,
|
|
218
|
+
fixedParams: parsed.fixedParams,
|
|
201
219
|
toolPrefix: parsed.toolPrefix,
|
|
202
220
|
debug: parsed.debug,
|
|
203
221
|
mode: parsed.mode
|
|
@@ -225,6 +243,9 @@ function loadFromCli(args) {
|
|
|
225
243
|
if (args.headers) {
|
|
226
244
|
config.headers = parseHeaders(args.headers);
|
|
227
245
|
}
|
|
246
|
+
if (args.fixedParams) {
|
|
247
|
+
config.fixedParams = parseFixedParams(args.fixedParams);
|
|
248
|
+
}
|
|
228
249
|
if (args.prefix) {
|
|
229
250
|
config.toolPrefix = args.prefix;
|
|
230
251
|
}
|
|
@@ -246,6 +267,7 @@ function mergeConfigs(...configs) {
|
|
|
246
267
|
if (config.baseUrl !== void 0) merged.baseUrl = config.baseUrl;
|
|
247
268
|
if (config.timeout !== void 0) merged.timeout = config.timeout;
|
|
248
269
|
if (config.headers !== void 0) merged.headers = config.headers;
|
|
270
|
+
if (config.fixedParams !== void 0) merged.fixedParams = config.fixedParams;
|
|
249
271
|
if (config.toolPrefix !== void 0) merged.toolPrefix = config.toolPrefix;
|
|
250
272
|
if (config.debug !== void 0) merged.debug = config.debug;
|
|
251
273
|
if (config.mode !== void 0) merged.mode = config.mode;
|
|
@@ -502,11 +524,14 @@ Tags: ${operation.tags.join(", ")}`);
|
|
|
502
524
|
}
|
|
503
525
|
return parts.join("\n");
|
|
504
526
|
}
|
|
505
|
-
function buildParametersSchema(operation, refResolver) {
|
|
527
|
+
function buildParametersSchema(operation, refResolver, fixedParams) {
|
|
506
528
|
const shape = {};
|
|
507
529
|
if (operation.parameters) {
|
|
508
530
|
for (const param of operation.parameters) {
|
|
509
531
|
const paramName = param.name;
|
|
532
|
+
if (fixedParams && paramName in fixedParams) {
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
510
535
|
let paramSchema = convertSchema(param.schema, refResolver);
|
|
511
536
|
if (param.description) {
|
|
512
537
|
paramSchema = paramSchema.describe(param.description);
|
|
@@ -547,11 +572,11 @@ function buildParametersSchema(operation, refResolver) {
|
|
|
547
572
|
shape._baseUrl = import_zod2.z.string().url().optional().describe("API base URL (overrides the default). Example: https://api.example.com");
|
|
548
573
|
return shape;
|
|
549
574
|
}
|
|
550
|
-
function generateTool(operation, components, toolPrefix) {
|
|
575
|
+
function generateTool(operation, components, toolPrefix, fixedParams) {
|
|
551
576
|
const refResolver = createRefResolver(components);
|
|
552
577
|
const name = generateToolName(operation, toolPrefix);
|
|
553
578
|
const description = generateToolDescription(operation);
|
|
554
|
-
const parametersShape = buildParametersSchema(operation, refResolver);
|
|
579
|
+
const parametersShape = buildParametersSchema(operation, refResolver, fixedParams);
|
|
555
580
|
const inputSchema = import_zod2.z.object(parametersShape);
|
|
556
581
|
logger.debug(`Generated tool: ${name}`);
|
|
557
582
|
return {
|
|
@@ -561,11 +586,11 @@ function generateTool(operation, components, toolPrefix) {
|
|
|
561
586
|
operation
|
|
562
587
|
};
|
|
563
588
|
}
|
|
564
|
-
function generateTools(operations, components, toolPrefix) {
|
|
589
|
+
function generateTools(operations, components, toolPrefix, fixedParams) {
|
|
565
590
|
const tools = [];
|
|
566
591
|
const usedNames = /* @__PURE__ */ new Set();
|
|
567
592
|
for (const operation of operations) {
|
|
568
|
-
const tool = generateTool(operation, components, toolPrefix);
|
|
593
|
+
const tool = generateTool(operation, components, toolPrefix, fixedParams);
|
|
569
594
|
if (usedNames.has(tool.name)) {
|
|
570
595
|
let counter = 1;
|
|
571
596
|
let newName = `${tool.name}_${counter}`;
|
|
@@ -598,11 +623,11 @@ function groupParametersByLocation(parameters) {
|
|
|
598
623
|
}
|
|
599
624
|
return groups;
|
|
600
625
|
}
|
|
601
|
-
function buildRequest(operation, input, defaultHeaders = {}) {
|
|
626
|
+
function buildRequest(operation, input, defaultHeaders = {}, fixedParams = {}) {
|
|
602
627
|
const groupedParams = groupParametersByLocation(operation.parameters);
|
|
603
628
|
let path = operation.path;
|
|
604
629
|
for (const param of groupedParams.path) {
|
|
605
|
-
const value = input[param.name];
|
|
630
|
+
const value = input[param.name] ?? fixedParams[param.name];
|
|
606
631
|
if (value !== void 0) {
|
|
607
632
|
path = path.replace(`{${param.name}}`, String(value));
|
|
608
633
|
} else if (param.required) {
|
|
@@ -611,7 +636,7 @@ function buildRequest(operation, input, defaultHeaders = {}) {
|
|
|
611
636
|
}
|
|
612
637
|
const query = {};
|
|
613
638
|
for (const param of groupedParams.query) {
|
|
614
|
-
const value = input[param.name];
|
|
639
|
+
const value = input[param.name] ?? fixedParams[param.name];
|
|
615
640
|
if (value !== void 0) {
|
|
616
641
|
if (Array.isArray(value)) {
|
|
617
642
|
query[param.name] = value.map(String);
|
|
@@ -682,7 +707,7 @@ async function executeRequest(operation, input, config) {
|
|
|
682
707
|
);
|
|
683
708
|
}
|
|
684
709
|
try {
|
|
685
|
-
const built = buildRequest(operation, input, config.headers || {});
|
|
710
|
+
const built = buildRequest(operation, input, config.headers || {}, config.fixedParams || {});
|
|
686
711
|
let url = `${baseUrl}${built.path}`;
|
|
687
712
|
url = appendQueryString(url, built.query);
|
|
688
713
|
logger.info(`Executing: ${operation.method} ${url}`);
|
|
@@ -1713,7 +1738,7 @@ async function createServer(config) {
|
|
|
1713
1738
|
logger.info(`Mode: ${config.mode || "default"}`);
|
|
1714
1739
|
const server = new import_mcp.McpServer({
|
|
1715
1740
|
name: "api2mcp",
|
|
1716
|
-
version: "0.
|
|
1741
|
+
version: "0.6.0"
|
|
1717
1742
|
});
|
|
1718
1743
|
const openApiDoc = await parseOpenApi(config.openapiUrl);
|
|
1719
1744
|
const baseUrl = getBaseUrl(openApiDoc, config.baseUrl);
|
|
@@ -1724,7 +1749,8 @@ async function createServer(config) {
|
|
|
1724
1749
|
const tools = generateTools(
|
|
1725
1750
|
openApiDoc.operations,
|
|
1726
1751
|
openApiDoc.components?.schemas,
|
|
1727
|
-
config.toolPrefix
|
|
1752
|
+
config.toolPrefix,
|
|
1753
|
+
effectiveConfig.fixedParams
|
|
1728
1754
|
);
|
|
1729
1755
|
if (config.mode === "ondemand") {
|
|
1730
1756
|
const registry = createRegistry(tools, openApiDoc.components?.schemas);
|