Skip to main content
Glama
pansin
by pansin
base-tool.ts1.51 kB
/** * 基础工具类 */ import { Tool } from '@modelcontextprotocol/sdk/types.js'; import { Page } from 'puppeteer'; import { ToolExecutionError } from '../errors/index.js'; import { ToolExecutionContext } from '../interfaces/mcp.js'; export abstract class BaseTool { protected name: string; protected description: string; protected schema: any; constructor(name: string, description: string, schema: any) { this.name = name; this.description = description; this.schema = schema; } /** * 获取MCP工具定义 */ getMCPTool(): Tool { return { name: this.name, description: this.description, inputSchema: this.schema, execute: this.execute.bind(this) }; } /** * 执行工具 */ async execute(args: any, context: ToolExecutionContext): Promise<any> { try { const result = await this.run(args, context); return result; } catch (error) { if (error instanceof ToolExecutionError) { throw error; } throw new ToolExecutionError( `执行工具 ${this.name} 失败: ${(error as Error).message}`, error as Error ); } } /** * 运行工具(由子类实现) */ protected abstract run(args: any, context: ToolExecutionContext): Promise<any>; /** * 验证页面是否可用 */ protected validatePage(page: Page | null): asserts page is Page { if (!page) { throw new ToolExecutionError('没有活动页面'); } } }

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/pansin/browserMCP'

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