-
Notifications
You must be signed in to change notification settings - Fork 157
refactor: consolidate tool handler utilities #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Extract common patterns from tool handlers into shared utilities to reduce duplication and improve maintainability. Changes: - Add src/utils/tool-handler-helpers.ts with shared utilities: - getEffectiveSourceId(): Normalize source ID handling - validateReadonlySQL(): Validate SQL against readonly constraints - trackToolRequest(): Centralize request tracking logic - withRequestTracking(): HOF wrapper (available for future use) - Refactor custom-tool-handler.ts to use new utilities - Refactor execute-sql.ts to use new utilities - Refactor search-objects.ts to use new utilities Benefits: - Each handler reduced by ~20-30 lines - Centralizes cross-cutting concerns (tracking, validation) - Handlers focus on core logic - Sets pattern for future tool handlers All tests passing (70/70 tool tests). Zero behavior changes. Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR refactors tool handler utilities by extracting common patterns into a shared tool-handler-helpers.ts file. The goal is to reduce code duplication across tool handlers (custom-tool-handler.ts, execute-sql.ts, and search-objects.ts) by centralizing source ID normalization, request tracking, and SQL validation logic.
Key Changes:
- Created new utility functions for common patterns:
getEffectiveSourceId(),validateReadonlySQL(),trackToolRequest(), andwithRequestTracking() - Refactored three tool handlers to use the new shared utilities
- Reduced code duplication by ~20-30 lines per handler
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/utils/tool-handler-helpers.ts | New utility file with shared helper functions for tool handlers (source ID normalization, readonly validation, request tracking) |
| src/tools/custom-tool-handler.ts | Refactored to use getEffectiveSourceId(), validateReadonlySQL(), and trackToolRequest() utilities |
| src/tools/execute-sql.ts | Refactored to use getEffectiveSourceId() and trackToolRequest() utilities (kept custom readonly validation) |
| src/tools/search-objects.ts | Refactored to use getEffectiveSourceId() and trackToolRequest() utilities |
Address all three issues from code review:
1. Preserve READONLY_VIOLATION error code
- Replace validateReadonlySQL() with two separate functions:
- isAllowedInReadonlyMode(): Check if SQL is allowed
- createReadonlyViolationMessage(): Format error message
- Keep error handling inline to preserve error code
2. Remove redundant readonly parameter
- New functions don't need the boolean flag
- Validation only called when readonly=true
3. Remove unused import
- Removed getEffectiveSourceId from custom-tool-handler.ts
- Not needed since sourceId comes from toolConfig
All tests passing (70/70). Build successful. True zero behavior changes.
Generated with Claude Code (https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
Address code review feedback on type safety and clarity: 1. Fix RequestMetadata type safety - Change sql from optional (sql?) to required (sql: string) - Aligns with Request interface in requestStore - All callers already provide sql, preventing runtime errors 2. Simplify isAllowedInReadonlyMode wrapper - Use re-export instead of wrapper function - Reduces indirection while maintaining clear naming - No added value from thin wrapper All tests passing (70/70). Build successful. Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Extract common patterns from tool handlers into shared utilities to reduce duplication and improve maintainability.
Changes:
Add src/utils/tool-handler-helpers.ts with shared utilities:
Refactor custom-tool-handler.ts to use new utilities
Refactor execute-sql.ts to use new utilities
Refactor search-objects.ts to use new utilities
Benefits:
All tests passing (70/70 tool tests). Zero behavior changes.
Generated with Claude Code (https://claude.com/claude-code)