Skip to content

fix: 修复 MCP Server 获取 keywords 配置时 JSON 序列化错误#1137

Open
VanwaysAI wants to merge 1 commit into
sansan0:masterfrom
VanwaysAI:fix/json-serializable-pattern
Open

fix: 修复 MCP Server 获取 keywords 配置时 JSON 序列化错误#1137
VanwaysAI wants to merge 1 commit into
sansan0:masterfrom
VanwaysAI:fix/json-serializable-pattern

Conversation

@VanwaysAI
Copy link
Copy Markdown

问题描述

调用 get_current_config(section="keywords") 时,MCP Server 返回错误:

Object of type Pattern is not JSON serializable

原因是 word_groups 列表中包含 re.Pattern 对象(正则表达式),Python 的 json.dumps() 无法序列化它。

根因分析

_parse_word() 函数(trendradar/core/frequency.py)在解析正则表达式配置时返回:

{
    "word": pattern_str,
    "is_regex": True,
    "pattern": pattern,  # ← 这是 re.compile() 返回的 Pattern 对象
    "display_name": display_name,
}

修复方案

DataService 类中添加 _make_json_serializable() 辅助方法,在返回 word_groups 之前递归地将所有 re.Pattern 对象转换为字符串表示。

测试

修复后可以正常获取 keywords 配置:

mcp_trendradar_get_current_config(section="keywords")
# 返回: {"word_groups": [...], "total_groups": N}

…tring

When calling get_current_config(section="keywords"), the word_groups list
contains re.Pattern objects that cannot be serialized to JSON. This causes
MCP tools to fail with "Object of type Pattern is not JSON serializable".

Added _make_json_serializable() helper method that recursively converts
re.Pattern objects to their string representation before returning.

Fixes sansan0#1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant