Skip to main content
Glama
base.ts1.31 kB
/** * 工具处理器基类 */ import type { ToolHandler, JSONSchema, ExecutionContext } from '../core/types.js'; export abstract class BaseToolHandler<TArgs = any, TResult = any> implements ToolHandler<TArgs, TResult> { abstract readonly name: string; abstract readonly description: string; abstract readonly inputSchema: JSONSchema; abstract execute(args: TArgs, context: ExecutionContext): Promise<TResult>; /** * 默认的参数验证(子类可覆盖) */ validate(args: any): args is TArgs { // 基础验证:检查必填字段 if (this.inputSchema.required) { for (const field of this.inputSchema.required) { if (!(field in args)) { throw new Error(`Missing required field: ${field}`); } } } return true; } /** * 包装执行,添加日志和错误处理 */ async safeExecute(args: any, context: ExecutionContext): Promise<TResult> { context.logger.debug(`Executing tool: ${this.name}`, args); try { this.validate(args); const result = await this.execute(args, context); context.logger.debug(`Tool ${this.name} completed successfully`); return result; } catch (error) { context.logger.error(`Tool ${this.name} failed:`, error); throw error; } } }

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/porkll/siyuan-mcp'

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