Skip to content

Commit 5bfdd29

Browse files
msukkariclaude
andcommitted
refactor(web): use try/finally pattern in registerMcpTool for tool_used event
Match the same pattern used in toVercelAITool — single captureEvent call in a finally block with a success boolean, instead of duplicated calls in try and catch. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2410480 commit 5bfdd29

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

packages/web/src/features/tools/adapters.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,23 @@ export function registerMcpTool<TName extends string, TShape extends z.ZodRawSha
5656
},
5757
},
5858
async (input) => {
59+
let success = true;
5960
try {
6061
const parsed = def.inputSchema.parse(input);
6162
const result = await def.execute(parsed, context);
62-
captureEvent('tool_used', {
63-
toolName: def.name,
64-
source: context.source ?? 'unknown',
65-
success: true,
66-
}, { distinctId: context.userId }).catch((error) => {
67-
logger.warn(`Failed to capture tool_used event for ${def.name}:`, error);
68-
});
6963
return { content: [{ type: "text" as const, text: result.output }] };
7064
} catch (error) {
65+
success = false;
66+
const message = error instanceof Error ? error.message : String(error);
67+
return { content: [{ type: "text" as const, text: `Tool "${def.name}" failed: ${message}` }], isError: true };
68+
} finally {
7169
captureEvent('tool_used', {
7270
toolName: def.name,
7371
source: context.source ?? 'unknown',
74-
success: false,
72+
success,
7573
}, { distinctId: context.userId }).catch((error) => {
7674
logger.warn(`Failed to capture tool_used event for ${def.name}:`, error);
7775
});
78-
const message = error instanceof Error ? error.message : String(error);
79-
return { content: [{ type: "text" as const, text: `Tool "${def.name}" failed: ${message}` }], isError: true };
8076
}
8177
},
8278
);

0 commit comments

Comments
 (0)