Skip to main content

MCP provider wrapping the Niutrans translation API

Project description

MCP 翻译服务

基于小牛翻译(Niutrans)API 的 MCP Provider,提供文字翻译工具和语种目录资源,方便在 Cursor/mcp-cli 等客户端中引用。

快速开始

通过 PyPI 安装

pip install mcp-translation-text
mcp-translation-text  # 直接启动 Provider

若希望使用 uv 管理环境:

uv tool install mcp-translation-text
mcp-translation-text

本地源码运行

  1. 克隆或下载本项目。
  2. 复制环境变量示例并填写 Key:
    Copy-Item .env.example .env
    
  3. 安装依赖并启动:
    • Windows
      scripts\start.ps1
      
    • macOS/Linux
      chmod +x scripts/start.sh
      ./scripts/start.sh
      
    脚本会:
    • .env(默认)加载 NIUTRANS_API_KEY 等变量;
    • 使用 uv 创建 .venv 并安装项目依赖;
    • 通过 uv run python server.py 启动 MCP Provider。

环境变量

  • NIUTRANS_API_KEY(必填):小牛翻译开放平台提供的 API Key。
  • NIUTRANS_API_URL(可选):自定义接口地址,默认为 https://api.niutrans.com/NiuTransServer/translation

MCP 客户端配置示例

若已通过 PyPI 安装,可在 mcp.json 中写:

{
  "mcpServers": {
    "translation": {
      "type": "stdio",
      "command": "mcp-translation-text",
      "env": {
        "NIUTRANS_API_KEY": "${env.NIUTRANS_API_KEY}",
        "NIUTRANS_API_URL": "${env.NIUTRANS_API_URL}"
      }
    }
  }
}

若仍使用源码目录,请将 command 指向虚拟环境中的 python.exe 并设置 cwd,如:

{
  "translation": {
    "type": "stdio",
    "command": "E:\\MCP\\.venv\\Scripts\\python.exe",
    "args": ["server.py"],
    "cwd": "E:\\MCP",
    "env": {
      "NIUTRANS_API_KEY": "${env.NIUTRANS_API_KEY}",
      "NIUTRANS_API_URL": "${env.NIUTRANS_API_URL}"
    }
  }
}

启动 Cursor 后执行 ListTools 即可看到 translate_text,同时支持 ListResources 读取 language://catalog

可用功能

工具:translate_text

  • 参数
    • text:待翻译内容。
    • source:源语言代码或别名(会通过本地映射表规范化)。
    • target:目标语言代码或别名。
  • 返回
    {
      "source": "zh",
      "target": "en",
      "original_text": "你好",
      "translated_text": "Hello",
      "raw": { ... 小牛原始响应 ... }
    }
    

资源:language://catalog

提供所有可用语种及别名,示例如下:

{
  "total": 655,
  "languages": [
    {"code": "zh", "zh": "中文(简体)", "en": "Chinese (Simplified)"},
    {"code": "en", "zh": "英语", "en": "English"}
    // ... 其余省略 ...
  ],
  "aliases": {
    "zhongwenjianti": "zh",
    "english": "en"
    // ... 其余省略 ...
  }
}

推荐在客户端的 LLM 中先读取该资源,完成语种描述到代码的映射后,再调用 translate_text

调试与常见问题

  • 缺少 API Key:启动时报 缺少环境变量 NIUTRANS_API_KEY,请确认已在 .env 或系统环境中设置。
  • 语种不支持translate_text 会校验语种代码/别名,若报错请检查是否使用了 language://catalog 中列出的值。
  • 路径或依赖问题:脚本依赖 uv,请先安装 pip install uv 或参考 uv 文档
  • 命令名称:通过 PyPI 安装后,可直接运行 mcp-translation-text;若 global PATH 中找不到,记得激活虚拟环境或使用 python -m mcp_translation_text
  • 发布/升级包
    python -m build
    twine upload dist/*
    

目录结构(关键文件)

E:\MCP
├── pyproject.toml
├── server.py                # 入口包装,确保 python server.py 可运行
├── src/
│   └── translation_server.py
├── scripts/
│   ├── start.ps1
│   └── start.sh
├── .env.example
├── LICENSE
└── README.md

发布后,用户只需设置 Niutrans API Key,即可通过 mcp-translation-text 直接加载该 Provider。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_translation_text-1.0.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_translation_text-1.0.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file mcp_translation_text-1.0.0.tar.gz.

File metadata

  • Download URL: mcp_translation_text-1.0.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.5

File hashes

Hashes for mcp_translation_text-1.0.0.tar.gz
Algorithm Hash digest
SHA256 88870a31f51b3cf9ee018c516d88d14f16775b4b4c534502297541295ac8fbe0
MD5 02aeed8506123590960148ebe6498452
BLAKE2b-256 cc3502cea85f7a295afdbc9e11112b417fd5fde69eb61ee10678e96190413907

See more details on using hashes here.

File details

Details for the file mcp_translation_text-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mcp_translation_text-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b21dcbf59e343bbac13c569e8033faeaf680d2ba8c17d18284d5332f3964d17f
MD5 ecf436ab06adec42bd4a423ac023f978
BLAKE2b-256 bac44902717869ff8e83899489cf8ada15dbc204aab4767fe30ae5ca87c93296

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page