Skip to main content
Glama

export_openapi

Export API documentation from Apifox projects to OpenAPI format. Use summary mode to review structure before importing new APIs, or full mode for complete specifications.

Instructions

查看 Apifox 接口文档:从 Apifox 项目导出接口信息。重要:在导入新接口前,务必先使用 summary 模式查看现有目录结构和接口列表,以便将新接口放入合适的目录中,保持项目结构一致性。

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
modeNo导出模式:summary=仅导出目录结构和接口列表(推荐,节省上下文),full=导出完整的 OpenAPI 规范。默认为 summary
oasVersionNoOpenAPI 规范版本,默认为 3.0(仅 full 模式有效)
exportFormatNo导出格式,默认为 JSON(仅 full 模式有效)
pathFilterNo路径过滤器,只导出匹配的接口路径(支持前缀匹配),如 "/api/user" 只导出用户相关接口

Implementation Reference

  • MCP server handler for the export_openapi tool. Handles input parameters, calls ApifoxClient.exportOpenApi, applies path filtering, and formats output in summary or full mode.
    case 'export_openapi': {
      const { mode, oasVersion, exportFormat, pathFilter } = args as {
        mode?: 'summary' | 'full';
        oasVersion?: '2.0' | '3.0' | '3.1';
        exportFormat?: 'JSON' | 'YAML';
        pathFilter?: string;
      };
    
      const exportMode = mode || 'summary';
    
      const result = await apifoxClient.exportOpenApi({
        oasVersion: oasVersion || '3.0',
        exportFormat: exportFormat || 'JSON'
      });
    
      // 路径过滤
      let filteredPaths = result.paths;
      if (pathFilter) {
        filteredPaths = {};
        Object.keys(result.paths).forEach(path => {
          if (path.startsWith(pathFilter)) {
            filteredPaths[path] = result.paths[path];
          }
        });
      }
    
      // Summary 模式:只返回目录结构和接口列表
      if (exportMode === 'summary') {
        let resultText = '✅ 接口文档概览(Summary 模式)\n\n';
    
        // 统计信息
        const pathsCount = Object.keys(filteredPaths).length;
        const totalCount = Object.keys(result.paths).length;
    
        resultText += `📊 统计信息:\n`;
        resultText += `  - 项目标题: ${result.info?.title || '未命名'}\n`;
        if (pathFilter) {
          resultText += `  - 过滤条件: ${pathFilter}\n`;
          resultText += `  - 匹配接口: ${pathsCount} / ${totalCount}\n`;
        } else {
          resultText += `  - 总接口数: ${pathsCount}\n`;
        }
        resultText += '\n';
    
        // 按路径前缀分组(模拟目录结构)
        const groups: { [key: string]: any[] } = {};
        Object.keys(filteredPaths).forEach(path => {
          const pathObj = filteredPaths[path];
          const methods = Object.keys(pathObj).filter(m => m !== 'parameters');
    
          methods.forEach(method => {
            const operation = pathObj[method];
            const tags = operation.tags || ['未分类'];
            const tag = tags[0] || '未分类';
    
            if (!groups[tag]) {
              groups[tag] = [];
            }
    
            groups[tag].push({
              path,
              method: method.toUpperCase(),
              summary: operation.summary || '无描述'
            });
          });
        });
    
        // 显示目录结构
        resultText += '📁 目录结构和接口列表:\n\n';
        const sortedGroups = Object.keys(groups).sort();
    
        sortedGroups.forEach(groupName => {
          resultText += `📂 ${groupName}\n`;
          groups[groupName].forEach(api => {
            resultText += `  └─ [${api.method}] ${api.path}\n`;
            resultText += `     ${api.summary}\n`;
          });
          resultText += '\n';
        });
    
        resultText += '\n💡 提示:\n';
        resultText += '  - 导入新接口时,请参考上述目录结构\n';
        resultText += '  - 将相关接口放入对应的目录(使用 tags 字段)\n';
        resultText += '  - 保持接口路径命名风格一致\n';
        if (!pathFilter) {
          resultText += '  - 如需查看特定接口详情,使用 pathFilter 参数过滤\n';
          resultText += '  - 如需完整规范,使用 mode: "full"\n';
        }
    
        return {
          content: [
            {
              type: 'text',
              text: resultText
            }
          ]
        };
      }
    
      // Full 模式:返回完整 OpenAPI 规范
      let resultText = '✅ OpenAPI 规范导出成功(Full 模式)\n\n';
    
      const pathsCount = Object.keys(filteredPaths).length;
      const schemasCount = result.components?.schemas ? Object.keys(result.components.schemas).length : 0;
    
      resultText += `📊 导出统计:\n`;
      resultText += `  - OpenAPI 版本: ${result.openapi || result.swagger}\n`;
      resultText += `  - 项目标题: ${result.info?.title || '未命名'}\n`;
      resultText += `  - 接口数量: ${pathsCount}\n`;
      resultText += `  - 数据模型数量: ${schemasCount}\n`;
    
      if (pathsCount > 0) {
        resultText += '\n📋 接口列表:\n';
        const paths = Object.keys(filteredPaths);
        paths.slice(0, 10).forEach(path => {
          const methods = Object.keys(filteredPaths[path]).filter(m => m !== 'parameters');
          resultText += `  - ${path} [${methods.join(', ').toUpperCase()}]\n`;
        });
        if (paths.length > 10) {
          resultText += `  ... 还有 ${paths.length - 10} 个接口\n`;
        }
      }
    
      // 如果有过滤,返回过滤后的规范
      let exportedSpec = result;
      if (pathFilter) {
        exportedSpec = {
          ...result,
          paths: filteredPaths
        };
      }
    
      return {
        content: [
          {
            type: 'text',
            text: resultText
          },
          {
            type: 'text',
            text: `\n📄 完整 OpenAPI 规范:\n\`\`\`json\n${JSON.stringify(exportedSpec, null, 2)}\n\`\`\``
          }
        ]
      };
    }
  • Input schema and metadata definition for the export_openapi tool in the tools registry.
    {
      name: 'export_openapi',
      description: '查看 Apifox 接口文档:从 Apifox 项目导出接口信息。重要:在导入新接口前,务必先使用 summary 模式查看现有目录结构和接口列表,以便将新接口放入合适的目录中,保持项目结构一致性。',
      inputSchema: {
        type: 'object',
        properties: {
          mode: {
            type: 'string',
            enum: ['summary', 'full'],
            description: '导出模式:summary=仅导出目录结构和接口列表(推荐,节省上下文),full=导出完整的 OpenAPI 规范。默认为 summary'
          },
          oasVersion: {
            type: 'string',
            enum: ['2.0', '3.0', '3.1'],
            description: 'OpenAPI 规范版本,默认为 3.0(仅 full 模式有效)'
          },
          exportFormat: {
            type: 'string',
            enum: ['JSON', 'YAML'],
            description: '导出格式,默认为 JSON(仅 full 模式有效)'
          },
          pathFilter: {
            type: 'string',
            description: '路径过滤器,只导出匹配的接口路径(支持前缀匹配),如 "/api/user" 只导出用户相关接口'
          }
        }
      }
    }
  • ApifoxClient helper method implementing the core export logic by calling the Apifox /export-openapi API endpoint.
    async exportOpenApi(options?: {
      oasVersion?: '2.0' | '3.0' | '3.1';
      exportFormat?: 'JSON' | 'YAML';
      scope?: {
        type?: 'ALL' | 'SELECTED_FOLDERS' | 'SELECTED_ENDPOINTS';
        excludedByTags?: string[];
      };
      options?: {
        includeApifoxExtensionProperties?: boolean;
        addFoldersToTags?: boolean;
      };
    }): Promise<any> {
      const response = await this.client.post(
        `/v1/projects/${this.projectId}/export-openapi`,
        {
          oasVersion: options?.oasVersion || '3.0',
          exportFormat: options?.exportFormat || 'JSON',
          ...(options?.scope && { scope: options.scope }),
          ...(options?.options && { options: options.options })
        }
      );
      return response.data;
    }
  • src/index.ts:211-213 (registration)
    Registration of the tools list (including export_openapi) via MCP ListToolsRequestSchema handler.
    server.setRequestHandler(ListToolsRequestSchema, async () => {
      return { tools };
    });

Tool Definition Quality

Score is being calculated. Check back soon.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Warren-W/apifox-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server