fix: 修复 MCP Server 获取 keywords 配置时 JSON 序列化错误#1137
Open
VanwaysAI wants to merge 1 commit into
Open
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题描述
调用
get_current_config(section="keywords")时,MCP Server 返回错误:原因是
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 配置: