char索引mcp(已存档)
此存储库已存档。 该项目已转移到 字符索引技能,现在作为Claude Code Skill插件提供,并继续支持MCP服务器。 技能和MCP服务器的未来更新(char-index-mcp)将从新存储库中发布。______________________________________________________________________
模型上下文协议(MCP)服务器提供 基于字符级索引的字符串操作非常适合在需要精确字符定位的情况下生成测试代码。
](https://smithery.ai/server/char-index-mcp)   
🎯 为什么存在
LLM逐个生成文本令牌,并努力进行精确的字符计数。当生成具有特定长度要求的测试代码或验证字符串位置时,您需要精确的基于索引的工具。这个MCP服务器解决了这个问题。
✨ 功能(12个工具)
🔍 字符和子字符串查找(4个工具)
find_nth_char-查找字符的第n次出现find_all_char_indices-查找角色的所有索引find_nth_substring-查找子字符串的第n次出现find_all_substring_indices-查找子字符串的所有出现
✂️ 拆分(1个工具)
split_at_indices-在多个位置拆分字符串
✏️ 字符串修改(3个工具)
insert_at_index-在特定位置插入文本delete_range-删除范围内的字符replace_range-用新文本替换范围
🛠️ 实用程序(3个工具)
find_regex_matches-查找与位置匹配的正则表达式模式extract_between_markers-提取两个标记之间的文本count_chars-字符统计(总数、字母、数字等)
📦 批处理(1个工具)
extract_substrings-提取一个或多个子字符串(统一工具)
🚀 安装
选项1:使用uvx(推荐)
无需安装!只需配置并运行:
# Test it works
uvx char-index-mcp --help选项2:来自PyPI
pip install char-index-mcp选项3:来源
git clone https://github.com/agent-hanju/char-index-mcp.git
cd char-index-mcp
pip install -e .🔧 配置
克劳德桌面
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
视窗: %APPDATA%\Claude\claude_desktop_config.json
使用uvx(推荐)
{
"mcpServers": {
"char-index": {
"command": "uvx",
"args": ["char-index-mcp"]
}
}
}使用pip安装
{
"mcpServers": {
"char-index": {
"command": "char-index-mcp"
}
}
}克劳德代码
# Using uvx (recommended)
claude mcp add char-index '{"command":"uvx","args":["char-index-mcp"]}'
# Using pip install
claude mcp add char-index '{"command":"char-index-mcp"}'光标
添加 ~/.cursor/mcp.json:
使用uvx(推荐)
{
"mcpServers": {
"char-index": {
"command": "uvx",
"args": ["char-index-mcp"]
}
}
}使用pip安装
{
"mcpServers": {
"char-index": {
"command": "char-index-mcp"
}
}
}📖 用法示例
查找角色
# Find 3rd occurrence of 'l'
find_nth_char("hello world", "l", 3) # Returns: 9
# Find all occurrences of 'l'
find_all_char_indices("hello world", "l") # Returns: [2, 3, 9]使用子环
# Find 2nd "hello"
find_nth_substring("hello hello world", "hello", 2) # Returns: 6
# Find all occurrences
find_all_substring_indices("hello hello world", "hello") # Returns: [0, 6]字符串操作
# Insert comma after "hello"
insert_at_index("hello world", 5, ",") # Returns: "hello, world"
# Delete " world"
delete_range("hello world", 5, 11) # Returns: "hello"
# Replace "world" with "Python"
replace_range("hello world", 6, 11, "Python") # Returns: "hello Python"拆分和提取
# Split at multiple positions
split_at_indices("hello world", [2, 5, 8]) # Returns: ["he", "llo", " wo", "rld"]
# Extract single character
extract_substrings("hello", [{"start": 1, "end": 2}])
# Returns: [{"start": 1, "end": 2, "substring": "e", "length": 1}]
# Batch extraction
extract_substrings("hello world", [
{"start": 0, "end": 5},
{"start": 6, "end": 11}
])
# Returns: [
# {"start": 0, "end": 5, "substring": "hello", "length": 5},
# {"start": 6, "end": 11, "substring": "world", "length": 5}
# ]模式匹配
# Find all numbers with their positions
find_regex_matches("test123abc456", r"\d+")
# Returns: [
# {"start": 4, "end": 7, "match": "123"},
# {"start": 10, "end": 13, "match": "456"}
# ]提取文本
# Extract content between markers
extract_between_markers("start[content]end", "[", "]", 1)
# Returns: {
# "content": "content",
# "content_start": 6,
# "content_end": 13,
# "full_start": 5,
# "full_end": 14
# }🧪 发展
# Clone the repository
git clone https://github.com/agent-hanju/char-index-mcp.git
cd char-index-mcp
# Install in development mode
pip install -e ".[dev]"
# Run tests
pytest
# Run with coverage
pytest --cov=char_index_mcp --cov-report=term-missing🎯 用例
- 测试代码生成:生成具有精确字符数的字符串
- 数据处理:在精确位置分割/提取数据
- 文本格式:在特定索引处插入/删除/替换
- 模式分析:查找并提取与位置匹配的模式
- LLM响应解析:按位置提取XML标记之间的内容
📝 示例:测试代码生成
# Ask Claude: "Generate a test string that's exactly 100 characters long"
# Claude can use count_chars() to verify the exact length
# Ask: "Find where the 5th comma is in this CSV line"
# Claude can use find_nth_char(csv_line, ",", 5)
# Ask: "Split this string at characters 10, 25, and 50"
# Claude can use split_at_indices(text, [10, 25, 50])
# Ask: "Extract the text between the 2nd and tags"
# Claude can use extract_between_markers(text, "", "", 2)🤝 贡献
欢迎投稿!拜托:
- 分叉存储库
- 创建要素分支
- 添加新功能的测试
- 提交拉取请求
📄 许可证
MIT许可证-有关详细信息,请参阅许可证文件
🔗 相关项目
📮 联系
有关问题、疑问或建议,请在GitHub上打开问题。
______________________________________________________________________
备注:这是第一个专门为基于索引的字符串操作设计的MCP服务器。所有其他文本MCP服务器都专注于计数、大小写转换或编码,而不是精确的字符定位。
