MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / MCPToolWrapper

Class MCPToolWrapper

src/mcp/tool-wrapper.ts:14–116  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12 * to avoid collisions with builtins and across servers.
13 */
14export class MCPToolWrapper extends Tool<Record<string, unknown>> {
15 readonly name: string;
16 readonly description: string;
17 readonly isReadOnly: boolean;
18 readonly isDestructive: boolean;
19 readonly argsSchema = z.record(z.any()) as unknown as z.ZodType<Record<string, unknown>>;
20
21 constructor(
22 private client: MCPClient,
23 private toolDef: MCPToolDef,
24 /** When the server is declared non-destructive, allow auto-approval. */
25 serverDestructive: boolean = true,
26 ) {
27 super();
28 this.name = `mcp:${client.name}:${toolDef.name}`;
29 this.description = `[via MCP/${client.name}] ${toolDef.description ?? toolDef.name}`;
30 // Conservative default: treat MCP tools as potentially mutating unless server says otherwise.
31 this.isReadOnly = !serverDestructive;
32 this.isDestructive = serverDestructive;
33 }
34
35 /** Override schema() to use the MCP-provided JSON Schema directly (skip zod conversion). */
36 schema(): ToolSchema {
37 const inputSchema = this.toolDef.inputSchema ?? { type: 'object', properties: {} };
38 return {
39 type: 'function',
40 function: {
41 name: this.name,
42 description: this.description,
43 parameters: inputSchema as ToolSchema['function']['parameters'],
44 },
45 };
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 );

Callers

nothing calls this directly

Calls 1

recordMethod · 0.80

Tested by

no test coverage detected