名称: MCP错误代码映射器 描述: 将应用程序错误映射到MCP错误代码,并提供适当的消息、错误类型和恢复建议。 允许工具: 读取、写入、编辑、Bash、Glob、Grep
MCP错误代码映射器
将应用程序错误映射到MCP错误代码并进行适当处理。
功能
- 将错误映射到MCP错误代码
- 创建结构化错误响应
- 生成错误文档
- 实现错误恢复提示
- 配置错误序列化
- 创建错误处理工具
使用场景
在以下情况下调用此技能:
- 将应用程序错误映射到MCP错误
- 创建结构化错误响应
- 为工具实现错误处理
- 生成错误文档
输入参数
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| language | 字符串 | 是 | 目标语言 |
| errors | 数组 | 是 | 错误定义 |
| includeRecovery | 布尔值 | 否 | 包含恢复提示 |
错误结构
{
"errors": [
{
"name": "FileNotFound",
"mcpCode": -32002,
"message": "文件未找到: {path}",
"recovery": "检查文件是否存在以及路径是否正确"
},
{
"name": "InvalidInput",
"mcpCode": -32602,
"message": "无效输入: {reason}",
"recovery": "检查输入参数并重试"
}
]
}
生成模式
TypeScript错误映射器
import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js';
// 标准MCP错误代码
export const MCP_ERROR_CODES = {
PARSE_ERROR: -32700,
INVALID_REQUEST: -32600,
METHOD_NOT_FOUND: -32601,
INVALID_PARAMS: -32602,
INTERNAL_ERROR: -32603,
// 自定义代码 (-32000 到 -32099)
FILE_NOT_FOUND: -32001,
PERMISSION_DENIED: -32002,
RESOURCE_NOT_FOUND: -32003,
VALIDATION_ERROR: -32004,
} as const;
// 带恢复提示的错误
interface McpErrorWithRecovery extends McpError {
data?: {
recovery?: string;
details?: unknown;
};
}
// 错误工厂函数
export function fileNotFoundError(path: string): McpErrorWithRecovery {
return {
code: MCP_ERROR_CODES.FILE_NOT_FOUND,
message: `文件未找到: ${path}`,
data: {
recovery: '检查文件是否存在以及路径是否正确',
details: { path },
},
};
}
export function invalidParamsError(reason: string): McpErrorWithRecovery {
return {
code: MCP_ERROR_CODES.INVALID_PARAMS,
message: `无效参数: ${reason}`,
data: {
recovery: '检查输入参数并重试',
},
};
}
// 错误处理包装器
export function handleToolError(error: unknown): McpErrorWithRecovery {
if (error instanceof Error) {
// 映射已知错误类型
if (error.message.includes('ENOENT')) {
return fileNotFoundError(error.message);
}
if (error.message.includes('EACCES')) {
return {
code: MCP_ERROR_CODES.PERMISSION_DENIED,
message: `权限拒绝: ${error.message}`,
data: { recovery: '检查文件权限' },
};
}
// 回退到内部错误
return {
code: MCP_ERROR_CODES.INTERNAL_ERROR,
message: error.message,
};
}
return {
code: MCP_ERROR_CODES.INTERNAL_ERROR,
message: '发生意外错误',
};
}
Python错误映射器
from enum import IntEnum
from typing import Any, Optional
from dataclasses import dataclass
class McpErrorCode(IntEnum):
PARSE_ERROR = -32700
INVALID_REQUEST = -32600
METHOD_NOT_FOUND = -32601
INVALID_PARAMS = -32602
INTERNAL_ERROR = -32603
# 自定义代码
FILE_NOT_FOUND = -32001
PERMISSION_DENIED = -32002
RESOURCE_NOT_FOUND = -32003
@dataclass
class McpError(Exception):
code: int
message: str
recovery: Optional[str] = None
details: Optional[Any] = None
def to_dict(self) -> dict:
result = {'code': self.code, 'message': self.message}
if self.recovery or self.details:
result['data'] = {}
if self.recovery:
result['data']['recovery'] = self.recovery
if self.details:
result['data']['details'] = self.details
return result
def file_not_found_error(path: str) -> McpError:
return McpError(
code=McpErrorCode.FILE_NOT_FOUND,
message=f'文件未找到: {path}',
recovery='检查文件是否存在以及路径是否正确',
details={'path': path}
)
def handle_tool_error(error: Exception) -> McpError:
error_msg = str(error)
if 'FileNotFoundError' in type(error).__name__:
return file_not_found_error(error_msg)
if 'PermissionError' in type(error).__name__:
return McpError(
code=McpErrorCode.PERMISSION_DENIED,
message=f'权限拒绝: {error_msg}',
recovery='检查文件权限'
)
return McpError(
code=McpErrorCode.INTERNAL_ERROR,
message=error_msg
)
工作流程
- 定义错误 - 列出应用程序错误
- 映射代码 - 分配MCP错误代码
- 创建消息 - 用户友好的消息
- 添加恢复 - 恢复建议
- 生成处理器 - 错误处理代码
- 文档化错误 - 错误文档
目标流程
- mcp工具实现
- mcp服务器安全加固
- 错误处理用户反馈