api2mcp 0.3.2 → 0.4.0-beta.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/dist/index.d.mts CHANGED
@@ -9,6 +9,10 @@ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
9
9
  * API 请求头配置
10
10
  */
11
11
  type ApiHeaders = Record<string, string>;
12
+ /**
13
+ * 工作模式
14
+ */
15
+ type Mode = 'default' | 'ondemand';
12
16
  /**
13
17
  * 单个 API 源配置
14
18
  */
@@ -23,6 +27,8 @@ interface ApiSourceConfig {
23
27
  headers?: ApiHeaders;
24
28
  /** 工具名前缀 */
25
29
  toolPrefix?: string;
30
+ /** 工作模式:default(默认)或 ondemand(按需) */
31
+ mode?: Mode;
26
32
  }
27
33
  /**
28
34
  * 完整配置
@@ -41,6 +47,7 @@ declare const CliArgsSchema: z.ZodObject<{
41
47
  headers: z.ZodOptional<z.ZodString>;
42
48
  prefix: z.ZodOptional<z.ZodString>;
43
49
  debug: z.ZodOptional<z.ZodBoolean>;
50
+ mode: z.ZodOptional<z.ZodEnum<["default", "ondemand"]>>;
44
51
  }, "strip", z.ZodTypeAny, {
45
52
  debug?: boolean | undefined;
46
53
  url?: string | undefined;
@@ -48,6 +55,7 @@ declare const CliArgsSchema: z.ZodObject<{
48
55
  timeout?: number | undefined;
49
56
  headers?: string | undefined;
50
57
  prefix?: string | undefined;
58
+ mode?: "default" | "ondemand" | undefined;
51
59
  }, {
52
60
  debug?: boolean | undefined;
53
61
  url?: string | undefined;
@@ -55,6 +63,7 @@ declare const CliArgsSchema: z.ZodObject<{
55
63
  timeout?: number | undefined;
56
64
  headers?: string | undefined;
57
65
  prefix?: string | undefined;
66
+ mode?: "default" | "ondemand" | undefined;
58
67
  }>;
59
68
  type CliArgs = z.infer<typeof CliArgsSchema>;
60
69
  /**
@@ -290,6 +299,208 @@ declare function parseOpenApi(source: string): Promise<ParsedOpenApiDoc>;
290
299
  */
291
300
  declare function getBaseUrl(doc: ParsedOpenApiDoc, overrideUrl?: string): string | undefined;
292
301
 
302
+ /**
303
+ * Registry 类型定义
304
+ */
305
+
306
+ /**
307
+ * API 条目 - 存储在 Registry 中的 API 信息
308
+ */
309
+ interface ApiEntry {
310
+ /** 唯一标识符(通常是 operationId 或生成的 ID) */
311
+ id: string;
312
+ /** 工具名称(带前缀) */
313
+ name: string;
314
+ /** HTTP 方法 */
315
+ method: string;
316
+ /** API 路径 */
317
+ path: string;
318
+ /** 简短描述 */
319
+ summary?: string;
320
+ /** 详细描述 */
321
+ description?: string;
322
+ /** 标签 */
323
+ tags?: string[];
324
+ /** 是否废弃 */
325
+ deprecated?: boolean;
326
+ /** 原始 OpenAPI 操作定义 */
327
+ operation: OpenApiOperation;
328
+ /** 组件 Schema(用于参数解析) */
329
+ components?: Record<string, OpenApiSchema>;
330
+ }
331
+ /**
332
+ * 搜索选项
333
+ */
334
+ interface SearchOptions {
335
+ /** 搜索关键词 */
336
+ query: string;
337
+ /** 搜索范围:name、summary、description、path */
338
+ searchIn?: ('name' | 'summary' | 'description' | 'path')[];
339
+ /** 最大返回数量 */
340
+ limit?: number;
341
+ }
342
+ /**
343
+ * 列表选项
344
+ */
345
+ interface ListOptions {
346
+ /** 页码(从 1 开始) */
347
+ page?: number;
348
+ /** 每页数量 */
349
+ pageSize?: number;
350
+ /** 按标签过滤 */
351
+ tag?: string;
352
+ }
353
+ /**
354
+ * 搜索结果项
355
+ */
356
+ interface SearchResultItem {
357
+ /** API ID */
358
+ id: string;
359
+ /** 工具名称 */
360
+ name: string;
361
+ /** HTTP 方法 */
362
+ method: string;
363
+ /** API 路径 */
364
+ path: string;
365
+ /** 简短描述 */
366
+ summary?: string;
367
+ /** 匹配字段 */
368
+ matchedFields: string[];
369
+ /** 匹配度分数(0-1) */
370
+ score: number;
371
+ }
372
+ /**
373
+ * 列表结果项
374
+ */
375
+ interface ListItem {
376
+ /** API ID */
377
+ id: string;
378
+ /** 工具名称 */
379
+ name: string;
380
+ /** HTTP 方法 */
381
+ method: string;
382
+ /** API 路径 */
383
+ path: string;
384
+ /** 简短描述 */
385
+ summary?: string;
386
+ /** 标签 */
387
+ tags?: string[];
388
+ /** 是否废弃 */
389
+ deprecated?: boolean;
390
+ }
391
+ /**
392
+ * 分页列表结果
393
+ */
394
+ interface ListResult {
395
+ /** 当前页码 */
396
+ page: number;
397
+ /** 每页数量 */
398
+ pageSize: number;
399
+ /** 总数量 */
400
+ total: number;
401
+ /** 总页数 */
402
+ totalPages: number;
403
+ /** API 列表 */
404
+ items: ListItem[];
405
+ }
406
+ /**
407
+ * API 详情
408
+ */
409
+ interface ApiDetail extends ApiEntry {
410
+ /** 参数 Schema(JSON Schema 格式) */
411
+ parameterSchema?: Record<string, unknown>;
412
+ /** 请求体 Schema(JSON Schema 格式) */
413
+ requestBodySchema?: Record<string, unknown>;
414
+ /** 响应 Schema */
415
+ responseSchemas?: Record<string, {
416
+ description?: string;
417
+ schema?: Record<string, unknown>;
418
+ }>;
419
+ }
420
+ /**
421
+ * Registry 统计信息
422
+ */
423
+ interface RegistryStats {
424
+ /** API 总数 */
425
+ totalApis: number;
426
+ /** 标签列表 */
427
+ tags: string[];
428
+ /** 按方法统计 */
429
+ byMethod: Record<string, number>;
430
+ /** 按标签统计 */
431
+ byTag: Record<string, number>;
432
+ }
433
+
434
+ /**
435
+ * API 注册表
436
+ * 用于存储、搜索和管理 API
437
+ */
438
+
439
+ /**
440
+ * API 注册表实现
441
+ */
442
+ declare class ApiRegistry {
443
+ private apis;
444
+ private nameIndex;
445
+ private tagIndex;
446
+ /**
447
+ * 注册 API
448
+ */
449
+ register(entry: ApiEntry): void;
450
+ /**
451
+ * 批量注册 API
452
+ */
453
+ registerAll(entries: ApiEntry[]): void;
454
+ /**
455
+ * 获取单个 API
456
+ */
457
+ get(id: string): ApiEntry | undefined;
458
+ /**
459
+ * 通过名称获取 API
460
+ */
461
+ getByName(name: string): ApiEntry | undefined;
462
+ /**
463
+ * 检查 API 是否存在
464
+ */
465
+ has(id: string): boolean;
466
+ /**
467
+ * 搜索 API
468
+ */
469
+ search(options: SearchOptions): SearchResultItem[];
470
+ /**
471
+ * 分页列出 API
472
+ */
473
+ list(options?: ListOptions): ListResult;
474
+ /**
475
+ * 获取所有标签
476
+ */
477
+ getTags(): string[];
478
+ /**
479
+ * 获取 API 详情
480
+ */
481
+ getDetail(id: string): ApiDetail | undefined;
482
+ /**
483
+ * 获取统计信息
484
+ */
485
+ getStats(): RegistryStats;
486
+ /**
487
+ * 获取 API 数量
488
+ */
489
+ get size(): number;
490
+ /**
491
+ * 构建参数 Schema
492
+ */
493
+ private buildParameterSchema;
494
+ /**
495
+ * 构建请求体 Schema
496
+ */
497
+ private buildRequestBodySchema;
498
+ /**
499
+ * 构建响应 Schema
500
+ */
501
+ private buildResponseSchemas;
502
+ }
503
+
293
504
  /**
294
505
  * MCP 服务器
295
506
  */
@@ -323,10 +534,27 @@ declare class ToolManager {
323
534
  * 批量注册工具
324
535
  */
325
536
  registerTools(tools: GeneratedTool[]): void;
537
+ /**
538
+ * 获取工具
539
+ */
540
+ getTool(name: string): GeneratedTool | undefined;
541
+ /**
542
+ * 通过 operationId 获取工具
543
+ */
544
+ getToolByOperationId(operationId: string): GeneratedTool | undefined;
326
545
  /**
327
546
  * 执行工具
328
547
  */
329
548
  private executeTool;
549
+ /**
550
+ * 通过 operation 直接执行(用于 ondemand 模式)
551
+ */
552
+ executeByOperation(operation: OpenApiOperation, args: Record<string, unknown>): Promise<{
553
+ content: Array<{
554
+ type: 'text';
555
+ text: string;
556
+ }>;
557
+ }>;
330
558
  /**
331
559
  * 获取所有已注册的工具名称
332
560
  */
@@ -335,8 +563,188 @@ declare class ToolManager {
335
563
  * 获取工具数量
336
564
  */
337
565
  getToolCount(): number;
566
+ /**
567
+ * 获取配置
568
+ */
569
+ getConfig(): Config;
338
570
  }
339
571
 
572
+ /**
573
+ * api_detail 工具
574
+ * 获取 API 详情
575
+ */
576
+
577
+ /**
578
+ * 输入参数 Schema
579
+ */
580
+ declare const apiDetailSchema: z.ZodObject<{
581
+ id: z.ZodString;
582
+ }, "strip", z.ZodTypeAny, {
583
+ id: string;
584
+ }, {
585
+ id: string;
586
+ }>;
587
+ type ApiDetailInput = z.infer<typeof apiDetailSchema>;
588
+ /**
589
+ * 工具定义
590
+ */
591
+ declare const apiDetailTool: {
592
+ name: string;
593
+ description: string;
594
+ inputSchema: z.ZodObject<{
595
+ id: z.ZodString;
596
+ }, "strip", z.ZodTypeAny, {
597
+ id: string;
598
+ }, {
599
+ id: string;
600
+ }>;
601
+ };
602
+ /**
603
+ * 执行 api_detail 工具
604
+ */
605
+ declare function executeApiDetail(registry: ApiRegistry, input: ApiDetailInput): string;
606
+
607
+ /**
608
+ * api_execute 工具
609
+ * 直接执行 API 调用
610
+ */
611
+
612
+ /**
613
+ * 输入参数 Schema
614
+ */
615
+ declare const apiExecuteSchema: z.ZodObject<{
616
+ operationId: z.ZodString;
617
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
618
+ _baseUrl: z.ZodOptional<z.ZodString>;
619
+ }, "strip", z.ZodTypeAny, {
620
+ operationId: string;
621
+ _baseUrl?: string | undefined;
622
+ parameters?: Record<string, unknown> | undefined;
623
+ }, {
624
+ operationId: string;
625
+ _baseUrl?: string | undefined;
626
+ parameters?: Record<string, unknown> | undefined;
627
+ }>;
628
+ type ApiExecuteInput = z.infer<typeof apiExecuteSchema>;
629
+ /**
630
+ * 工具定义
631
+ */
632
+ declare const apiExecuteTool: {
633
+ name: string;
634
+ description: string;
635
+ inputSchema: z.ZodObject<{
636
+ operationId: z.ZodString;
637
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
638
+ _baseUrl: z.ZodOptional<z.ZodString>;
639
+ }, "strip", z.ZodTypeAny, {
640
+ operationId: string;
641
+ _baseUrl?: string | undefined;
642
+ parameters?: Record<string, unknown> | undefined;
643
+ }, {
644
+ operationId: string;
645
+ _baseUrl?: string | undefined;
646
+ parameters?: Record<string, unknown> | undefined;
647
+ }>;
648
+ };
649
+ /**
650
+ * 执行 api_execute 工具
651
+ */
652
+ declare function executeApiExecute(registry: ApiRegistry, config: Config, input: ApiExecuteInput): Promise<string>;
653
+
654
+ /**
655
+ * api_list 工具
656
+ * 分页浏览所有 API
657
+ */
658
+
659
+ /**
660
+ * 输入参数 Schema
661
+ */
662
+ declare const apiListSchema: z.ZodObject<{
663
+ page: z.ZodDefault<z.ZodNumber>;
664
+ pageSize: z.ZodDefault<z.ZodNumber>;
665
+ tag: z.ZodOptional<z.ZodString>;
666
+ }, "strip", z.ZodTypeAny, {
667
+ page: number;
668
+ pageSize: number;
669
+ tag?: string | undefined;
670
+ }, {
671
+ page?: number | undefined;
672
+ pageSize?: number | undefined;
673
+ tag?: string | undefined;
674
+ }>;
675
+ type ApiListInput = z.infer<typeof apiListSchema>;
676
+ /**
677
+ * 工具定义
678
+ */
679
+ declare const apiListTool: {
680
+ name: string;
681
+ description: string;
682
+ inputSchema: z.ZodObject<{
683
+ page: z.ZodDefault<z.ZodNumber>;
684
+ pageSize: z.ZodDefault<z.ZodNumber>;
685
+ tag: z.ZodOptional<z.ZodString>;
686
+ }, "strip", z.ZodTypeAny, {
687
+ page: number;
688
+ pageSize: number;
689
+ tag?: string | undefined;
690
+ }, {
691
+ page?: number | undefined;
692
+ pageSize?: number | undefined;
693
+ tag?: string | undefined;
694
+ }>;
695
+ };
696
+ /**
697
+ * 执行 api_list 工具
698
+ */
699
+ declare function executeApiList(registry: ApiRegistry, input: ApiListInput): string;
700
+
701
+ /**
702
+ * api_search 工具
703
+ * 模糊搜索 API
704
+ */
705
+
706
+ /**
707
+ * 输入参数 Schema
708
+ */
709
+ declare const apiSearchSchema: z.ZodObject<{
710
+ query: z.ZodString;
711
+ searchIn: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodEnum<["name", "summary", "description", "path"]>, "many">>>;
712
+ limit: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
713
+ }, "strip", z.ZodTypeAny, {
714
+ query: string;
715
+ searchIn: ("path" | "description" | "name" | "summary")[];
716
+ limit: number;
717
+ }, {
718
+ query: string;
719
+ searchIn?: ("path" | "description" | "name" | "summary")[] | undefined;
720
+ limit?: number | undefined;
721
+ }>;
722
+ type ApiSearchInput = z.infer<typeof apiSearchSchema>;
723
+ /**
724
+ * 工具定义
725
+ */
726
+ declare const apiSearchTool: {
727
+ name: string;
728
+ description: string;
729
+ inputSchema: z.ZodObject<{
730
+ query: z.ZodString;
731
+ searchIn: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodEnum<["name", "summary", "description", "path"]>, "many">>>;
732
+ limit: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
733
+ }, "strip", z.ZodTypeAny, {
734
+ query: string;
735
+ searchIn: ("path" | "description" | "name" | "summary")[];
736
+ limit: number;
737
+ }, {
738
+ query: string;
739
+ searchIn?: ("path" | "description" | "name" | "summary")[] | undefined;
740
+ limit?: number | undefined;
741
+ }>;
742
+ };
743
+ /**
744
+ * 执行 api_search 工具
745
+ */
746
+ declare function executeApiSearch(registry: ApiRegistry, input: ApiSearchInput): string;
747
+
340
748
  /**
341
749
  * 错误类定义
342
750
  */
@@ -375,4 +783,4 @@ interface Logger {
375
783
  }
376
784
  declare const logger: Logger;
377
785
 
378
- export { Api2McpError, type ApiHeaders, type ApiSourceConfig, type Config, ConfigurationError, type GeneratedTool, HttpError, type HttpResponse, type OpenApiOperation, type OpenApiParameter, OpenApiParseError, type OpenApiSchema, type ParsedOpenApiDoc, ToolExecutionError, ToolManager, convertSchema, createRefResolver, createServer, executeRequest, formatResponse, generateTool, generateTools, getBaseUrl, loadConfig, logger, parseOpenApi, startServer };
786
+ export { Api2McpError, type ApiDetail, type ApiDetailInput, type ApiEntry, type ApiExecuteInput, type ApiHeaders, type ApiListInput, ApiRegistry, type ApiSearchInput, type ApiSourceConfig, type Config, ConfigurationError, type GeneratedTool, HttpError, type HttpResponse, type ListItem, type ListOptions, type ListResult, type Mode, type OpenApiOperation, type OpenApiParameter, OpenApiParseError, type OpenApiSchema, type ParsedOpenApiDoc, type RegistryStats, type SearchOptions, type SearchResultItem, ToolExecutionError, ToolManager, apiDetailSchema, apiDetailTool, apiExecuteSchema, apiExecuteTool, apiListSchema, apiListTool, apiSearchSchema, apiSearchTool, convertSchema, createRefResolver, createServer, executeApiDetail, executeApiExecute, executeApiList, executeApiSearch, executeRequest, formatResponse, generateTool, generateTools, getBaseUrl, loadConfig, logger, parseOpenApi, startServer };