feat(core): 改进API响应处理支持纯文本JSON解析
当API返回content-type为text/plain但实际内容是JSON格式时, 现在会尝试解析为JSON对象而不是直接返回文本, 提高API响应的兼容性 fix(create_mcp): 移除硬编码的输出schema定义 移除了工具注册中硬编码的outputSchema定义, 让系统使用更灵活的配置方式 chore(release): 更新版本号至0.2.4
This commit is contained in:
@@ -940,10 +940,28 @@ class ApiClient:
|
||||
"status_code": response.status_code,
|
||||
}
|
||||
else:
|
||||
# 对于非JSON响应,记录前100个字符
|
||||
# 对于非JSON响应,尝试解析为JSON(某些API返回text/plain但内容是JSON)
|
||||
if not response.text:
|
||||
return {
|
||||
"status": "success",
|
||||
"data": "",
|
||||
"status_code": response.status_code,
|
||||
}
|
||||
|
||||
response_preview = response.text[:100] + "..." if len(response.text) > 100 else response.text
|
||||
logger.info(f"文本响应预览: {response_preview}")
|
||||
|
||||
# 尝试解析为JSON
|
||||
text = response.text.strip()
|
||||
if text and (text.startswith('{') or text.startswith('[')):
|
||||
try:
|
||||
json_response = json.loads(text)
|
||||
logger.info("文本响应成功解析为JSON")
|
||||
return json_response
|
||||
except (json.JSONDecodeError, ValueError) as e:
|
||||
logger.debug(f"文本响应JSON解析失败: {e}")
|
||||
|
||||
# 返回原始文本
|
||||
return {
|
||||
"status": "success",
|
||||
"data": response.text,
|
||||
|
||||
@@ -395,24 +395,12 @@ class ApiToolPlugin(ToolPlugin):
|
||||
tool_name = tool_config["interfaceName"] # 工具名称(拼音格式)
|
||||
logger.debug(f"注册工具: {tool_name}")
|
||||
|
||||
# 定义输出 Schema(先写死一个测试)
|
||||
output_schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"code": {"type": "integer", "description": "响应状态码,0表示成功"},
|
||||
"message": {"type": "string", "description": "响应消息"},
|
||||
"data": {"type": "object", "description": "响应数据"}
|
||||
},
|
||||
"required": ["code", "message"]
|
||||
}
|
||||
|
||||
# 创建MCP工具定义
|
||||
tools.append(
|
||||
types.Tool(
|
||||
name=tool_name, # 工具名称
|
||||
description=tool_config["schema_description"], # 工具描述(包含参数说明)
|
||||
inputSchema=tool_config["schema"], # 输入参数的JSON Schema
|
||||
outputSchema=output_schema, # 输出参数的JSON Schema
|
||||
name=tool_name,
|
||||
description=tool_config["schema_description"],
|
||||
inputSchema=tool_config["schema"],
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "lzwcai-mcp-api-converter"
|
||||
version = "0.2.1"
|
||||
version = "0.2.4"
|
||||
description = "基于FastMCP框架的动态API工具服务器,自动将企业业务API配置转换为MCP协议工具,支持多种传输方式、企业认证和参数验证,为AI助手提供标准化的业务接口访问能力。"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
|
||||
Reference in New Issue
Block a user