(args: Record<string, unknown>, ctx: ToolContext)
| 46 | } |
| 47 | |
| 48 | async execute(args: Record<string, unknown>, ctx: ToolContext): Promise<ToolResult> { |
| 49 | if (!this.client.isReady()) { |
| 50 | return { |
| 51 | content: `[MCP_UNAVAILABLE] MCP server '${this.client.name}' is not ready (state: ${this.client.status.state}, error: ${this.client.status.error ?? 'none'}). The agent cannot use this tool right now.`, |
| 52 | isError: true, |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | // Permission check (MCP tools go through the same engine as builtins) |
| 57 | const permReq = { |
| 58 | tool: this.name, |
| 59 | operation: this.name, |
| 60 | description: this.description, |
| 61 | }; |
| 62 | const decision = ctx.permissions.evaluate(permReq); |
| 63 | if (decision === 'deny') { |
| 64 | return { content: `[PERMISSION_DENIED] Blocked by policy: ${this.name}`, isError: true }; |
| 65 | } |
| 66 | if (decision === 'ask') { |
| 67 | const summary = this.summarizeArgs(args); |
| 68 | const answer = await ctx.askUser( |
| 69 | `Run MCP tool ${this.name}${summary ? `\n args: ${summary}` : ''}?`, |
| 70 | ['yes', 'no', 'always'], |
| 71 | ); |
| 72 | if (answer === 'no') { |
| 73 | return { content: `[USER_REJECTED] User declined MCP tool ${this.name}`, isError: true }; |
| 74 | } |
| 75 | if (answer === 'always') { |
| 76 | ctx.permissions.rememberDecision(permReq, 'allow', 'pattern'); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | try { |
| 81 | const result = await this.client.callTool(this.toolDef.name, args, ctx.signal); |
| 82 | const text = (result.content ?? []) |
| 83 | .map(b => { |
| 84 | if (b.type === 'text') return b.text; |
| 85 | if (b.type === 'image') return `[image ${b.mimeType}, ${b.data.length} chars base64]`; |
| 86 | if (b.type === 'resource') return `[resource ${b.resource.uri}${b.resource.text ? `: ${b.resource.text.slice(0, 200)}` : ''}]`; |
| 87 | return JSON.stringify(b); |
| 88 | }) |
| 89 | .join('\n'); |
| 90 | return { |
| 91 | content: text || '[empty result]', |
| 92 | isError: result.isError === true, |
| 93 | metadata: { mcpServer: this.client.name, mcpTool: this.toolDef.name }, |
| 94 | }; |
| 95 | } catch (e: any) { |
| 96 | logger.warn('MCP tool call failed', { tool: this.name, err: e.message }); |
| 97 | return { |
| 98 | content: `[MCP_ERROR] ${this.name}: ${e.message}`, |
| 99 | isError: true, |
| 100 | }; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private summarizeArgs(args: Record<string, unknown>): string { |
| 105 | const keys = Object.keys(args); |
nothing calls this directly
no test coverage detected