Skip to main content
Glama

char-index-mcp (已归档)

此仓库已归档。 该项目已迁移至 char-index-skill,现作为 Claude Code Skill 插件提供,并继续支持 MCP 服务器。

未来对该 Skill 和 MCP 服务器 (char-index-mcp) 的更新将从新仓库发布。


一个提供基于字符级索引的字符串操作的模型上下文协议 (MCP) 服务器。非常适合需要精确字符定位的测试代码生成场景。

License: MIT PyPI Python

🎯 存在意义

大语言模型按 Token 生成文本,难以处理精确的字符计数。在生成有特定长度要求的测试代码或验证字符串位置时,你需要基于索引的精确工具。此 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 .

🔧 配置

Claude Desktop

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json

Windows: %APPDATA%\Claude\claude_desktop_config.json

使用 uvx (推荐)

{
  "mcpServers": {
    "char-index": {
      "command": "uvx",
      "args": ["char-index-mcp"]
    }
  }
}

使用 pip 安装

{
  "mcpServers": {
    "char-index": {
      "command": "char-index-mcp"
    }
  }
}

Claude Code

# 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

添加到 ~/.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

🎯 应用场景

  1. 测试代码生成:生成具有精确字符计数的字符串

  2. 数据处理:在精确位置分割/提取数据

  3. 文本格式化:在特定索引处插入/删除/替换

  4. 模式分析:查找并提取带有位置信息的模式匹配项

  5. 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 <thinking> and </thinking> tags"
# Claude can use extract_between_markers(text, "<thinking>", "</thinking>", 2)

🤝 贡献

欢迎贡献!请:

  1. Fork 本仓库

  2. 创建功能分支

  3. 为新功能添加测试

  4. 提交 Pull Request

📄 许可证

MIT 许可证 - 详情请参阅 LICENSE 文件

🔗 相关项目

📮 联系方式

如有问题、疑问或建议,请在 GitHub 上提交 Issue。


注意:这是第一个专门为基于索引的字符串操作而设计的 MCP 服务器。所有其他文本 MCP 服务器都侧重于计数、大小写转换或编码,而非精确的字符定位。

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/agent-hanju/char-index-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server