{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "DSH Connector Provider",
  "description": "dsh-connectors 内置 provider 的唯一声明格式。新增 provider 时在 providers/<id>/provider.json 中提供此结构。",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "id", "name", "category", "summary", "server", "auth"],
  "properties": {
    "$schema": {
      "type": "string",
      "description": "编辑器提示用，可省略；插件运行时忽略。"
    },
    "schemaVersion": {
      "const": 1,
      "description": "provider 契约版本，当前固定为 1。"
    },
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9-]*$",
      "description": "全局唯一，必须与目录名一致；只允许小写字母、数字和连字符。"
    },
    "name": {
      "type": "string",
      "minLength": 1
    },
    "vendor": {
      "type": "string",
      "minLength": 1
    },
    "category": {
      "enum": ["开发工具", "项目管理", "数据库", "知识检索", "办公协作", "安全运维", "数据分析", "其他"]
    },
    "summary": {
      "type": "string",
      "minLength": 1,
      "description": "卡片上展示的一句话说明。"
    },
    "description": {
      "type": "string",
      "minLength": 1
    },
    "examples": {
      "type": "array",
      "maxItems": 6,
      "items": {
        "type": "string",
        "minLength": 1
      }
    },
    "translations": {
      "type": "object",
      "description": "按语言提供的可选文案（例如 en）。缺失字段回退到顶层 summary/description/examples。",
      "additionalProperties": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1
          },
          "vendor": {
            "type": "string",
            "minLength": 1
          },
          "summary": {
            "type": "string",
            "minLength": 1
          },
          "description": {
            "type": "string",
            "minLength": 1
          },
          "examples": {
            "type": "array",
            "maxItems": 6,
            "items": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        "additionalProperties": false
      }
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      }
    },
    "featured": {
      "type": "boolean"
    },
    "order": {
      "type": "integer",
      "minimum": 0,
      "description": "目录排序，越小越靠前；缺省为 100。"
    },
    "instance": {
      "type": "object",
      "additionalProperties": false,
      "required": ["label"],
      "description": "需要用户填写或选择 MCP 服务地址的 provider；存在时 server.url 不应设置。提供 presets 时界面改为从预置地址中选择。",
      "properties": {
        "label": {
          "type": "string",
          "minLength": 1
        },
        "noun": {
          "type": "string",
          "minLength": 1,
          "description": "界面上对这项配置的叫法，默认「地址」；例如企查查用「数据域」。"
        },
        "placeholder": {
          "type": "string",
          "minLength": 1
        },
        "hint": {
          "type": "string",
          "minLength": 1
        },
        "help": {
          "$ref": "#/$defs/help"
        },
        "presets": {
          "type": "array",
          "minItems": 1,
          "description": "预置地址列表；存在时用户从列表中选择，不再手填 URL。",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["label", "url"],
            "properties": {
              "label": {
                "type": "string",
                "minLength": 1
              },
              "url": {
                "type": "string",
                "pattern": "^https://",
                "description": "必须是 https 地址。"
              },
              "hint": {
                "type": "string",
                "minLength": 1
              }
            }
          }
        }
      }
    },
    "server": {
      "type": "object",
      "additionalProperties": false,
      "required": ["transport"],
      "properties": {
        "serverName": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9_-]*$",
          "description": "MCP 工具前缀 mcp__<serverName>__；缺省等于 provider id，必须全局唯一。"
        },
        "transport": {
          "enum": ["streamable-http", "sse"],
          "description": "远程传输：Streamable HTTP 或旧版 SSE（SSE 由插件内置桥接进程转成 stdio）；本地 stdio 通过自定义 mcpServers 导入。"
        },
        "url": {
          "type": "string",
          "minLength": 1,
          "description": "不带 instance 时必须是 https 地址；带 instance 时不要设置，完整地址由用户填写。"
        }
      }
    },
    "auth": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type"],
          "properties": {
            "type": {
              "const": "none"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "label"],
          "properties": {
            "type": {
              "const": "bearer"
            },
            "label": {
              "type": "string",
              "minLength": 1
            },
            "help": {
              "$ref": "#/$defs/help"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "header", "label"],
          "properties": {
            "type": {
              "const": "header"
            },
            "header": {
              "type": "string",
              "minLength": 1
            },
            "prefix": {
              "type": "string"
            },
            "label": {
              "type": "string",
              "minLength": 1
            },
            "help": {
              "$ref": "#/$defs/help"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "flow", "registration"],
          "properties": {
            "type": {
              "const": "oauth2"
            },
            "flow": {
              "const": "pkce"
            },
            "registration": {
              "oneOf": [
                {
                  "const": "dcr"
                },
                {
                  "const": "user"
                },
                {
                  "type": "object",
                  "additionalProperties": false,
                  "required": ["clientId"],
                  "properties": {
                    "clientId": {
                      "type": "string",
                      "minLength": 1
                    }
                  }
                }
              ],
              "description": "dcr 自动注册；user 由用户在面板里填写应用 AppKey/SecretKey；对象形式为静态 clientId。"
            },
            "endpoints": {
              "type": "object",
              "additionalProperties": false,
              "required": ["authorization", "token"],
              "description": "显式授权与令牌端点；设置后跳过 OAuth 元数据发现。registration=user 时必填。",
              "properties": {
                "authorization": {
                  "type": "string",
                  "format": "uri"
                },
                "token": {
                  "type": "string",
                  "format": "uri"
                }
              }
            },
            "authorizeParams": {
              "type": "object",
              "additionalProperties": {
                "type": "string"
              },
              "description": "附加到授权 URL 的固定参数，例如百度网盘的 qrcode=1。"
            },
            "tokenMethod": {
              "enum": ["POST", "GET"],
              "description": "换取与刷新令牌的 HTTP 方法，默认 POST。"
            },
            "pkce": {
              "type": "boolean",
              "description": "是否使用 PKCE，默认 true；不支持 PKCE 的服务设为 false。"
            },
            "scopes": {
              "type": "array",
              "items": {
                "type": "string",
                "minLength": 1
              }
            },
            "clientName": {
              "type": "string",
              "minLength": 1
            },
            "help": {
              "$ref": "#/$defs/help"
            }
          }
        }
      ]
    }
  },
  "allOf": [
    {
      "if": {
        "required": ["instance"]
      },
      "then": {
        "properties": {
          "server": {
            "not": {
              "required": ["url"]
            }
          }
        }
      },
      "else": {
        "properties": {
          "server": {
            "required": ["url"],
            "properties": {
              "url": {
                "pattern": "^https://"
              }
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "help": {
      "type": "object",
      "additionalProperties": false,
      "required": ["url", "label"],
      "properties": {
        "url": {
          "type": "string",
          "format": "uri",
          "pattern": "^https://"
        },
        "label": {
          "type": "string",
          "minLength": 1
        },
        "hint": {
          "type": "string",
          "minLength": 1
        }
      }
    }
  }
}
