Codepage Bridge MCP

Codepage Bridge MCP

Encoding-transparent file tools for Claude Code and other MCP clients, converting project files between legacy codepages and Unicode for LLM operations.

Category
访问服务器

README

Codepage Bridge MCP

Encoding-transparent file tools for Claude Code and other MCP clients.

Codepage Bridge exposes Read, Grep, Edit, and Write over MCP while transparently converting project files between their on-disk legacy codepage and Unicode text for the LLM. The model sees normal Unicode; files are written back in the encoding selected by the nearest .encoding-rules.

It is designed for legacy codebases that still use GBK/GB2312, Big5, Shift-JIS, EUC-KR, Windows-1251, or other non-UTF-8 encodings.

Why

Claude Code's built-in file tools assume UTF-8 for normal text reads. In legacy projects this can lead to:

  • unreadable C/C++ comments and string literals;
  • searches that silently miss text;
  • edits that corrupt the original codepage;
  • accidental UTF-8 rewrites of GBK or other legacy files.

Codepage Bridge keeps encoding conversion below the model boundary:

legacy bytes on disk -> decode by .encoding-rules -> Unicode for the LLM
Unicode from the LLM -> strict encode by .encoding-rules -> legacy bytes on disk

If new text cannot be represented in the target encoding, the write fails instead of silently replacing characters with ?.

Features

  • Encoding-aware Read, Grep, Edit, and Write tools.
  • Project-level .encoding-rules with gitignore-like glob behavior.
  • The nearest .encoding-rules defines both the project root and active rules.
  • Last matching rule wins; !pattern resets matching files to strict UTF-8.
  • Basename patterns such as *.cpp match at every directory depth.
  • Strict UTF-8 fallback for files not matched by a rule.
  • GBK/GB2312, Big5, Shift-JIS, EUC-KR, Windows codepages, UTF-8, and UTF-16 support.
  • BOM and dominant line-ending preservation for edits.
  • Read-before-write protection and stale-write detection using byte hashes.
  • Atomic temporary-file writes and per-path write locks.
  • Symlink and project-root boundary checks.
  • Image, PDF, and Jupyter Notebook reading.
  • Grep output modes, context lines, glob/type filters, regex flags, and pagination.

Requirements

  • Node.js 20 or newer.
  • Claude Code or another MCP client with stdio server support.
  • Optional: Poppler commands pdfinfo and pdftoppm for PDF page rendering.

Installation

Clone and build:

git clone git@github.com:skyispainted/codepage-bridge-mcp.git
cd codepage-bridge-mcp
npm install
npm run build

The server entry point is:

dist/src/server.js

MCP Configuration

Claude Code user-level installation

Available in all projects:

claude mcp add --scope user codepage-bridge -- node /absolute/path/to/codepage-bridge-mcp/dist/src/server.js

Verify:

claude mcp get codepage-bridge
claude mcp list

Claude Code project-level installation

Create .mcp.json in the project root:

{
  "mcpServers": {
    "codepage-bridge": {
      "type": "stdio",
      "command": "node",
      "args": [
        "/absolute/path/to/codepage-bridge-mcp/dist/src/server.js"
      ]
    }
  }
}

Shared project MCP configurations may require approval the first time Claude Code opens the project.

Important: Disable the Built-in File Tools

Installing the MCP is not sufficient by itself. Claude Code may continue choosing its built-in Read, Grep, Edit, Write, or NotebookEdit tools, which bypass .encoding-rules.

Add the bridge tools to permissions.allow and the built-in tools to permissions.deny in ~/.claude/settings.json:

{
  "permissions": {
    "allow": [
      "mcp__codepage-bridge__Read",
      "mcp__codepage-bridge__Grep",
      "mcp__codepage-bridge__Edit",
      "mcp__codepage-bridge__Write"
    ],
    "deny": [
      "Read",
      "Grep",
      "Edit",
      "Write",
      "NotebookEdit"
    ]
  }
}

Merge these entries into your existing settings instead of replacing the entire file.

Recommended Claude instructions

Add this to the project CLAUDE.md, or to ~/.claude/CLAUDE.md for a global policy:

## File encoding policy

Use Codepage Bridge for all project file content operations:

- Read with `mcp__codepage-bridge__Read`.
- Search with `mcp__codepage-bridge__Grep`.
- Edit with `mcp__codepage-bridge__Edit`.
- Create or completely rewrite with `mcp__codepage-bridge__Write`.

Do not use built-in Read, Grep, Edit, Write, NotebookEdit, shell commands,
PowerShell commands, or scripts as substitutes for project file content access.
Glob may only be used to discover paths.

Do not manually transcode files or normalize line endings. `.encoding-rules`
is the source of truth.

Why both settings and instructions?

  • deny removes the unsafe built-in tools from the model's available tool list.
  • CLAUDE.md prevents the model from bypassing the bridge with shell commands or scripts.

.encoding-rules

Every project using Codepage Bridge must contain .encoding-rules at its root.

Example:

# Last matching rule wins
*.c gbk
*.cpp gbk
*.h gbk
legacy/**/*.txt windows-1251
assets/**/*.csv shift_jis
**/*.json utf8

# Cancel earlier matches and return to strict UTF-8
!SourceCode/generated/**

Syntax:

<glob-pattern> <encoding>

Rules:

  • Empty lines and lines beginning with # are ignored.
  • *, **, and ? use glob semantics.
  • Patterns without /, such as *.cpp, match basenames at every directory depth.
  • Patterns containing / are relative to the .encoding-rules directory.
  • The last matching rule wins.
  • !pattern cancels previous matches and selects strict UTF-8.
  • Files with no matching rule use strict UTF-8.
  • The nearest .encoding-rules is used; its directory is the allowed project root.

Use encoding labels accepted by WHATWG TextDecoder and iconv-lite, for example:

utf8
utf-16le
gbk
gb2312
gb18030
big5
shift_jis
euc-kr
windows-1251
windows-1252

Tools

Read

{
  "file_path": "C:\\project\\SourceCode\\Main.cpp",
  "offset": 1,
  "limit": 200,
  "pages": "1-5"
}
  • Text is decoded according to .encoding-rules and returned as Unicode.
  • Output uses numbered lines compatible with Claude Code workflows.
  • Files larger than 256 KiB require offset and limit.
  • Multiple ranged reads of the same unchanged file are merged by line coverage; once all lines have been returned, Edit and Write are authorized.
  • Ranges may be sequential, overlapping, out of order, or concurrently requested within the same MCP process. A file hash change invalidates accumulated coverage.
  • Supports PNG, JPEG, GIF, WebP, PDF pages, and Notebook cells.
  • Notebook reads do not accept text-line offset or limit.

Grep

{
  "pattern": "错误|失败",
  "path": "C:\\project",
  "glob": "**/*.log",
  "output_mode": "content",
  "-i": false,
  "-n": true,
  "-C": 2,
  "head_limit": 250,
  "offset": 0
}

Supported options:

  • output_mode: content, files_with_matches, or count.
  • glob and common type filters.
  • -i, -n, and -o.
  • -A, -B, -C, and context.
  • multiline.
  • head_limit and offset.

Each candidate file is decoded using its own nearest .encoding-rules. Explicit single-file decode failures are reported as errors rather than being misreported as zero matches.

Edit

{
  "file_path": "C:\\project\\SourceCode\\Main.cpp",
  "old_string": "旧文本",
  "new_string": "新文本",
  "replace_all": false
}
  • Existing files require a complete prior Read.
  • A partial read authorizes an edit when every line covered by the selected old_string match was actually returned to the model.
  • The rest of a large file does not need to be read when the target string is unique and fully contained in displayed lines.
  • With eplace_all: true, every matching range must have been read.
  • If a target was not read, the error reports the exact line range to request.
  • The file is re-read and hashed immediately before writing.
  • Multiple matches are rejected unless replace_all is true.
  • Straight/curly quote compatibility follows Claude Code edit behavior.
  • The original encoding, BOM, and dominant line ending are preserved.

Write

{
  "file_path": "C:\\project\\SourceCode\\NewFile.cpp",
  "content": "完整内容"
}
  • New files use the encoding selected by .encoding-rules.
  • Existing files require a complete prior Read.
  • Existing files retain encoding and BOM metadata.
  • Complete rewrites use the line endings supplied by the caller.

Safety Notes

  1. Commit .encoding-rules with the project. Without it, Codepage Bridge refuses access instead of guessing a project root.
  2. Test rules before large edits. Start with Read or Grep on representative nested files.
  3. Use basename globs for language-wide rules. *.cpp gbk applies at any depth; SourceCode/**/*.cpp gbk applies only below that path.
  4. Do not silently convert encodings. If text cannot be represented in the configured encoding, update the rule deliberately or choose representable text.
  5. Do not bypass the bridge. Shell tools, scripts, IDE formatters, and other MCP servers can still rewrite files using the wrong encoding.
  6. Keep the MCP process trusted. It has read/write access inside roots defined by .encoding-rules.
  7. Review generated diffs. Encoding preservation prevents byte-level corruption, but semantic edits still require review.
  8. PDF support is optional. Install Poppler or avoid PDF reads.
  9. Notebook editing is intentionally unavailable. Notebook reading is supported, but the MCP does not expose NotebookEdit.
  10. Windows network/device paths are rejected before I/O. This avoids unintended SMB access and credential leakage.

Development

npm install
npm run check
npm test
npm run build
npm start

Current automated coverage includes encoding rule precedence, nested basename matching, strict codecs, lossy-write rejection, stale-write protection, atomic writes, symlink boundaries, images, PDFs, Notebook reads, GBK read/edit/write flows, Grep modes, and MCP protocol registration.

License

MIT. See LICENSE.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选