体验中心
体验中国电信星辰大模型效果
智能体广场支持将API转换为MCP,本文重点介绍API转换为MCP服务的的文件规范,并提供代码示例。
API 文件需要符合以下规范。
| 类型 | 说明 |
|---|---|
| 语法 | - 代码中不存在 Swagger/OpenAPI 3 语法错误。 - 代码中必须包含 Swagger/OpenAPI 3 元数据、title 和 version。 - 建议为每个 Opeartion 设置 operationId。 |
| 请求方法 | - 仅支持 GET、POST、PUT、DELETE、PATCH 。 - CONNECT、HEAD、OPTIONS 和 TRACE 等请求方法将被忽略。 |
| 请求类型 | - 支持 JSON(application/json)和 Form(application/x-www-form-urlencoded)内容类型。 - 不支持 application/xml、multipart/form-data 和 application/octet-stream 等内容类型。 |
| 请求参数 | - Swagger - 参数的位置为 path、query、header 或 formData 时,array 类型的参数将序列化为逗号分隔值(CSV)。 - 参数的数据类型不支持 file。 - OpenAPI 3.0 - 参数的位置为 path、query、header或cookie时,array 类型的参数将序列化为逗号分隔值(CSV)。 |
| 响应 | 仅支持解析 200、201 和 default,且仅解析1个。同时存在时,解析的顺序为 200> 201 > default。 |
JSON 格式的 POST 请求示例如下。
{
"swagger": "2.0",
"info": {
"title": "weather",
"version": "1.0.0"
},
"paths": {
"/weather/{city}": {
"get": {
"operationId": "weather",
"summary": "Check weather status。",
"consumes": ["application/json"],
"parameters": [
{
"name": "city",
"description": "City",
"type": "string",
"in": "path",
"required": true
},
{
"name": "body",
"in": "body",
"schema": {
"type": "object",
"properties": {
"date": {
"description": "Date",
"type": "string"
}
}
}
}
],
"responses": {
"200": {
"description": "Weather status",
"schema": {
"type": "object",
"properties": {
"weather": {
"description": "Weather",
"type": "string"
}
}
}
}
}
}
}
}
}
{
"openapi": "3.0.1",
"info": {
"title": "weather",
"version": "1.0.0"
},
"paths": {
"/weather/{city}": {
"get": {
"operationId": "weather",
"summary": "check the weather",
"parameters": [
{
"name": "city",
"description": "city",
"type": "string",
"schema": {
"type": "string"
},
"in": "path",
"required": true
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"date": {
"description": "Date",
"type": "string"
}
}
}
}
}
},
"responses": {
"200": {
"description": "weather",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"weather": {
"description": "weather",
"type": "string"
}
}
}
}
}
}
}
}
}
}
}