code_convert
Convert code between programming languages. Specify source and target languages to translate your code accurately.
Instructions
将代码从一种编程语言转换为另一种语言。
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | ||
| from_language | Yes | ||
| to_language | Yes |
Implementation Reference
- Tool schema definition for 'code_convert' — defines name, description, and inputSchema with required fields: code, from_language, to_language.
types.Tool( name="code_convert", description="将代码从一种编程语言转换为另一种语言。", inputSchema={ "type": "object", "properties": { "code": {"type": "string"}, "from_language": {"type": "string"}, "to_language": {"type": "string"}, }, "required": ["code", "from_language", "to_language"], }, ), - Handler logic for 'code_convert' — constructs an LLM prompt to convert code from one language to another, preserving logic and using idiomatic style.
elif name == "code_convert": prompt = ( f"请将以下 {a['from_language']} 代码转换为 {a['to_language']}。\n" f"要求:保持逻辑完全一致,使用目标语言的惯用写法,只输出转换后的代码。\n\n" f"```{a['from_language'].lower()}\n{a['code']}\n```" ) - The full handle_code async function which dispatches based on tool name, including the code_convert branch (lines 145-150). Calls llm_call with the constructed prompt.
async def handle_code(name: str, arguments: dict) -> list[types.TextContent]: a = arguments if name == "code_explain": detail_map = {"brief": "用一两句话简短", "detailed": "非常详细地", "normal": ""} detail_str = detail_map.get(a.get("detail", "normal"), "") lang = a.get("language", "") or "(自动检测)" prompt = ( f"请{detail_str}解释以下 {lang} 代码的功能和逻辑," f"包括:整体作用、关键步骤、重要变量/函数的含义。\n\n" f"```\n{a['code']}\n```" ) elif name == "code_review": focus = a.get("focus", ["all"]) focus_str = "所有方面(Bug、安全、性能、代码风格)" if "all" in focus else "、".join(focus) lang = a.get("language", "") or "" prompt = ( f"请对以下 {lang} 代码进行代码审查,重点关注:{focus_str}。\n" f"请按以下格式输出:\n" f"1. 问题列表(严重程度 + 描述 + 修复建议)\n" f"2. 整体评分(1-10)\n" f"3. 改进后的代码(如有必要)\n\n" f"```\n{a['code']}\n```" ) elif name == "code_generate": style_map = { "simple": "简洁易读", "production": "生产级别(含完整错误处理、日志、类型注解)", } style_str = style_map.get(a.get("style", "simple"), "简洁易读") lang = a.get("language", "Python") prompt = ( f"请用 {lang} 编写以下功能的代码,风格要求:{style_str}。\n" f"只输出代码和必要的注释,不需要额外解释。\n\n" f"需求:{a['description']}" ) elif name == "code_convert": prompt = ( f"请将以下 {a['from_language']} 代码转换为 {a['to_language']}。\n" f"要求:保持逻辑完全一致,使用目标语言的惯用写法,只输出转换后的代码。\n\n" f"```{a['from_language'].lower()}\n{a['code']}\n```" ) elif name == "code_fix": err_part = f"\n\n错误信息:\n```\n{a['error_message']}\n```" if a.get("error_message") else "" lang = a.get("language", "") or "" prompt = ( f"请修复以下 {lang} 代码中的错误。\n" f"要求:输出修复后的完整代码,并在代码前简要说明修复了什么问题。\n\n" f"```\n{a['code']}\n```{err_part}" ) elif name == "code_docstring": style = a.get("style", "google") language = a.get("language", "Python") prompt = ( f"请为以下 {language} 代码生成 {style} 风格的文档注释(docstring)。\n" f"要求:为每个函数/类/方法添加完整的参数说明、返回值说明、异常说明(如有)。\n" f"只输出添加了文档注释的完整代码。\n\n" f"```\n{a['code']}\n```" ) else: raise ValueError(f"未知 code 工具: {name}") reply = await llm_call(prompt) return [types.TextContent(type="text", text=reply)] - src/onion_mcp_server/server.py:52-53 (registration)Registration of code_convert tool: iterates CODE_TOOLS and maps each tool name (including 'code_convert') to the handle_code handler in the _HANDLERS routing table.
for _t in CODE_TOOLS: _HANDLERS[_t.name] = handle_code - src/onion_mcp_server/server.py:73-90 (registration)The call_tool handler that looks up the tool name in _HANDLERS (which includes 'code_convert' -> handle_code) and executes it.
@server.call_tool() async def call_tool( name: str, arguments: dict, ) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: log.warning("call: %s %s", name, arguments) handler = _HANDLERS.get(name) if handler is None: raise ValueError( f"未知工具: {name}\n" f"可用工具: {', '.join(_HANDLERS.keys())}" ) try: return await handler(name, arguments) except Exception as e: log.error("工具 %s 失败: %s", name, e, exc_info=True) return [types.TextContent( type="text",