结巴分词MCP
这是一个基于rjieba(Jieba的Rust实现)的Model Context Protocol (MCP) 服务器,提供高性能的中文分词服务。支持多种模式、词性标注、关键词提取,并且可以以STDIO和Streamable-HTTP两种方式部署。
MCP 服务配置
复制以下 JSON 到 OPClaw 或其他 MCP 客户端的配置文件中即可使用
{
"mcpServers": {
"jieba": {
"args": [
"mcp-jieba@latest",
"--transport",
"stdio"
],
"command": "uvx"
}
}
}
可用工具 (3 个)
该服务在 MCP 协议中暴露的工具,AI 可按需调用
tokenize 2 个参数 需填 1 项
Tokenize text(s) with jieba segmentation (exact or search mode). Args: text: `Union[str, List[str]]` A single string or a list of strings `["str1", "str2", ...]` to tokenize. mode: `str` Tokenization mode - "exact" for precise segmentation (default) or "search" for search engine mode. Returns: A dictionary where keys are indices (as strings) and values are lists of tokens. Example: {0: ["token1", "token2", ...], 1: [...]}
必填参数:text
tag 1 个参数 需填 1 项
Perform POS tagging on text(s) with jieba. Args: text: `Union[str, List[str]]` A single string or a list of strings `["str1", "str2", ...]` to tag. Returns: A dictionary where keys are indices (as strings) and values are dicts of word-flag pairs. Example: {0: {"word1": "flag1", "word2": "flag2", ...}, 1: {...}} The flags follow ICTCLAS POS tagging conventions: ```json {"a": "形容词", "b": "区别词", "c": "连词", "d": "副词", "e": "叹词", "g": "语素字", "h": "前接成分", "i": "习用语", "j": "简称", "k": "后接成分", "m": "数词", "n": "普通名词", "nd": "方位名词", "nh": "人名", "ni": "机构名", "nl": "处所名词", "ns": "地名", "nt": "时间词", "nz": "其他专名", "o": "拟声词", "p": "介词", "q": "量词", "r": "代词", "u": "助词", "v": "动词", "wp": "标点符号", "ws": "字符串", "x": "非语素字", "y": "语气词", "z": "状态词"} ```
必填参数:text
extract_keywords 2 个参数 需填 1 项
Extract keywords from text(s) using BM25-adpt algorithm with numpy. Each input string is treated as an independent corpus, split into sentences for analysis. Args: text: `Union[str, List[str]]` A single string or a list of strings `["str1", "str2", ...]` to extract keywords from. top_k: `int` Number of top keywords to extract per document (default 3). Returns: A dictionary where keys are indices (as strings) and values are lists of keywords. Example: {0: ["keyword1", "keyword2", "keyword3", ...], 1: [...]}
必填参数:text
服务介绍
MCP Jieba Server
这是一个基于 rjieba (Rust implementation of Jieba) 的 Model Context Protocol (MCP) 服务器,提供高性能的中文分词服务。
功能特性
- 高性能分词: 使用 Rust 编写的底层引擎。
- 多模式支持: 支持精确模式 (
exact) 和搜索引擎模式 (search)。 - 词性标注: 支持 ICTCLAS 兼容的词性标注。
- 关键词提取: 基于 BM25 算法的关键词提取。
- 批量处理: 支持单字符串或字符串数组输入,返回 JSON 格式结果。
- 双模部署:
- STDIO: 适用于本地开发和 Claude Context/Cherry Studio/VS Code 集成。
- Streamable-HTTP: 适用于远程部署(如 ModelScope)。
安装
使用 pip / uv / pipx
# 使用 pip
pip install .
# 使用 uv
uv pip install .
使用方法
1. 本地运行 (STDIO)
直接运行模块即可启动 STDIO 服务器:
uvx mcp-jieba --transport=stdio
或者在 Claude Context/Cherry Studio/VS Code 的 MCP 配置中添加:
{
"mcpServers": {
"jieba": {
"args": ["mcp-jieba@latest","--transport=stdio"],
"command": "uvx"
}
}
}
2. 远程部署 (Streamable-HTTP)
使用命令行参数启动 HTTP 服务器:
python -m mcp_jieba.server
Streamable-HTTP 端点地址: http://localhost:3001/mcp
ModelScope 部署
在 ModelScope 创建 Space 时,选择 Python 环境,并使用以下启动命令:
uvx mcp-jieba@latest
当前 pyproject.toml 已经包含所有依赖。
开发与测试
目前项目的单元测试尚不完善。建议使用 MCP Inspector 进行交互式测试和调试。
bunx @modelcontextprotocol/inspector uvx mcp-jieba@latest
工具说明
tokenize
对文本进行分词。
| 项目 | 描述 |
|---|---|
| 参数 | text (required): 待分词的文本,可以是单个字符串或字符串数组。mode (optional): 分词模式,可选 "exact" (默认) 或 "search"。 |
| 返回 | JSON 对象,键为输入数组的索引(字符串格式),值为分词结果数组。 |
示例:
- 输入:
text=["我爱北京天安门"], mode="exact" - 输出:
{"0": ["我", "爱", "北京", "天安门"]}
tag
对文本进行词性标注,标注类型符合ICTCLAS标准。
| 项目 | 描述 |
|---|---|
| 参数 | text (required): 待标注的文本,可以是单个字符串或字符串数组。 |
| 返回 | JSON 对象,键为输入数组的索引,值为单词-词性对的列表。 |
示例:
- 输入:
text=["我爱北京天安门"] - 输出:
{"0": [{"word": "我", "flag": "r"}, {"word": "爱", "flag": "v"}, ...]}
extract_keywords
使用向量化的针对关键词BM25-ADPT算法提取关键词。
| 项目 | 描述 |
|---|---|
| 参数 | text (required): 待提取的文本,可以是单个字符串或字符串数组。top_k (optional): 每个文档提取的关键词数量 (默认 3)。 |
| 返回 | JSON 对象,键为输入数组的索引,值为关键词列表。 |
示例:
- 输入:
text=["我爱北京天安门"], top_k=2 - 输出:
{"0": ["天安门", "北京"]}
鸣谢
- @messense/jieba-rs
- @messense/rjieba-py
- BM25-ADPT https://doi.org/10.1145/2063576.2063871
- GitHub Copilot