Skip to main content
Glama

yapi_interface_add

Add new API interfaces to YAPI documentation by specifying path, method, parameters, and response details for automated API management.

Instructions

新增接口

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlNoYAPI 接口页面 URL(可选),如 https://yapi.xxx.com/project/1009/interface/api/108375,会自动解析出项目 ID、接口 ID 等参数
projectNo项目 ID(可选,不传则使用默认项目)
catidYes接口分类 ID
titleYes接口名称
pathYes接口路径,如:/api/user
methodYes请求方法,如:GET、POST、PUT、DELETE
descNo接口描述(可选)
statusNo接口状态:done(已完成)、undone(未完成)
req_paramsNo路径参数
req_queryNo查询参数
req_headersNo请求头
req_body_typeNo请求体类型:form、json、file、raw
req_body_formNo表单类型的请求体
req_body_otherNo其他类型的请求体(JSON 字符串)
res_body_typeNo返回数据类型:json、raw
res_bodyNo返回数据(JSON 字符串)

Implementation Reference

  • Tool registration and input schema definition for 'yapi_interface_add' - defines the tool name, description, and all input parameters including catid, title, path, method, and optional fields like desc, status, req_params, req_query, req_headers, request body types, and response body configuration.
    { name: "yapi_interface_add", description: "新增接口", inputSchema: { type: "object", properties: { url: urlParamDef, project: { type: ["number", "string"], description: projectParamDesc, }, catid: { type: "number", description: "接口分类 ID", }, title: { type: "string", description: "接口名称", }, path: { type: "string", description: "接口路径,如:/api/user", }, method: { type: "string", description: "请求方法,如:GET、POST、PUT、DELETE", }, desc: { type: "string", description: "接口描述(可选)", }, status: { type: "string", description: "接口状态:done(已完成)、undone(未完成)", enum: ["done", "undone"], }, req_params: { type: "array", description: "路径参数", items: { type: "object", properties: { name: { type: "string" }, desc: { type: "string" }, }, }, }, req_query: { type: "array", description: "查询参数", items: { type: "object", properties: { name: { type: "string" }, desc: { type: "string" }, required: { type: "string" }, }, }, }, req_headers: { type: "array", description: "请求头", items: { type: "object", properties: { name: { type: "string" }, value: { type: "string" }, }, }, }, req_body_type: { type: "string", description: "请求体类型:form、json、file、raw", enum: ["form", "json", "file", "raw"], }, req_body_form: { type: "array", description: "表单类型的请求体", items: { type: "object", properties: { name: { type: "string" }, type: { type: "string" }, desc: { type: "string" }, required: { type: "string" }, }, }, }, req_body_other: { type: "string", description: "其他类型的请求体(JSON 字符串)", }, res_body_type: { type: "string", description: "返回数据类型:json、raw", enum: ["json", "raw"], }, res_body: { type: "string", description: "返回数据(JSON 字符串)", }, }, required: ["catid", "title", "path", "method"], }, },
  • Handler implementation for 'yapi_interface_add' - extracts project parameter and passes remaining arguments to YAPI API endpoint '/api/interface/add' using POST method via yapiRequestWithFallback.
    case "yapi_interface_add": { const { project: _, ...restArgs } = args; result = await yapiRequestWithFallback( "/api/interface/add", "POST", () => restArgs, args.project ); break; }
  • Helper function yapiRequestWithFallback - wraps YAPI API calls with automatic token fallback; tries specified project first, or iterates through all configured projects until a valid token is found.
    async function yapiRequestWithFallback( endpoint: string, method: "GET" | "POST", buildParams: (project: ProjectConfig) => Record<string, unknown> | undefined, specifiedProject?: unknown ): Promise<unknown> { // 如果明确指定了项目,直接使用该项目 if (specifiedProject !== undefined && specifiedProject !== null && specifiedProject !== "") { const project = getProjectConfig(specifiedProject); return yapiRequest(endpoint, method, buildParams(project), project.token); } if (projects.length === 0) { throw new Error( "未配置任何项目,请设置 YAPI_PROJECTS 环境变量,格式: projectId1:token1,projectId2:token2" ); } // 依次尝试所有项目的 token for (const project of projects) { try { const result = await yapiRequest( endpoint, method, buildParams(project), project.token ); if (!isTokenError(result)) { return result; } } catch { // 请求失败,尝试下一个项目 continue; } } throw new Error( `所有已配置项目的 token 均无法访问此接口,已尝试项目: ${projects.map((p) => p.id).join(", ")}` ); }
  • Helper function yapiRequest - core HTTP request wrapper that handles GET and POST requests to YAPI API endpoints, adds token parameter, sets appropriate headers, and returns parsed JSON response.
    async function yapiRequest( endpoint: string, method: "GET" | "POST" = "GET", params?: Record<string, unknown>, token?: string ): Promise<unknown> { if (!YAPI_BASE_URL) { throw new Error("YAPI_BASE_URL 环境变量未配置"); } if (!token) { throw new Error("token 未提供"); } const url = new URL(endpoint, YAPI_BASE_URL); const options: RequestInit = { method, headers: { "Content-Type": "application/json", }, }; if (method === "GET") { // GET 请求时将参数添加到 URL if (params) { Object.entries(params).forEach(([key, value]) => { if (value !== undefined && value !== null) { url.searchParams.append(key, String(value)); } }); } url.searchParams.append("token", token); } else { // POST 请求时将参数放在 body 中 url.searchParams.append("token", token); if (params) { options.body = JSON.stringify(params); } } const response = await fetch(url.toString(), options); if (!response.ok) { throw new Error(`YAPI 请求失败: ${response.status} ${response.statusText}`); } return response.json(); }

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/zhhbinn/yapi-mcp'

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