-
Notifications
You must be signed in to change notification settings - Fork 302
feat: Add tool_pre_invoke and tool_post_invoke plugin hooks #686
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
feat: Add tool_pre_invoke and tool_post_invoke plugin hooks #686
Conversation
Wow, awesome! We'll test soon! |
Signed-off-by: Shamsul Arefin <[email protected]>
Signed-off-by: Shamsul Arefin <[email protected]>
…cstring's Raises section. Signed-off-by: Shamsul Arefin <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
6c10096
to
11a42b2
Compare
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.
PR Review: Tool Hooks for Plugin System
Passed: make autoflake isort black pre-commit doctest test smoketest lint-web flake8 pylint
Branch: feature-pre-post-invoke-tool-hook
Test Results Summary
All tests and linters passed after rebasing on main:
- ✅ Doctest: 443 passed, 8 skipped
- ✅ Test Suite: 1307 passed, 10 skipped, 49 warnings
- ✅ Smoketest: Passed with TLS enabled
- ✅ Lint-web: No security vulnerabilities found
- ✅ Flake8: Clean (no issues)
- ✅ Pylint: 10.00/10 (only 2 TODO comments in sampling.py)
- ✅ Coverage: 80% overall
Key Features Added
1. Plugin Framework Tool Hooks
- Added
tool_pre_invoke
andtool_post_invoke
hooks to the plugin system - New payload types:
ToolPreInvokePayload
andToolPostInvokePayload
- Plugins can now intercept, modify, or block tool invocations
- Proper error handling with
PluginViolationError
for blocked requests
2. PII Filter Enhancements
- Extended PII detection to tool arguments and results
- Added intelligent nested data structure processing
- Support for recursive traversal of complex tool payloads
- Maintains detection statistics and metadata
3. Tool Service Integration
- Integrated plugin manager into tool service
- Pre-invoke hooks can modify tool arguments before execution
- Post-invoke hooks can filter/modify tool results
- Proper context passing with request IDs and server IDs
- Maintains passthrough headers functionality - properly integrated with new plugin hooks
Minor Issues Found
1. Debug Code Left In
- PII filter contains debug print statements that should be removed before merge
- Lines in
plugins/pii_filter/pii_filter.py
withprint()
calls
Recommendations for Merge
Must Fix Before Merge:
- Remove debug print statements from PII filter plugin
- Fix duplicate database field in Gateway model (remove duplicate at line 1124)
Nice to Have:
- Add integration tests specifically for tool hooks
- Add examples of tool hook usage in plugin documentation
- Consider adding performance benchmarks for plugin processing overhead
Files Changed Summary
Modified Core Files:
mcpgateway/services/tool_service.py
- Added plugin hook integration with passthrough headers supportmcpgateway/plugins/framework/base.py
- Added tool hook methodsmcpgateway/plugins/framework/manager.py
- Implemented tool hook orchestrationmcpgateway/plugins/framework/plugin_types.py
- New tool payload typesmcpgateway/plugins/framework/utils.py
- Added tool hook matchers
Modified Plugins:
plugins/pii_filter/pii_filter.py
- Extended with tool hook support (has debug prints)plugins/regex_filter/search_replace.py
- Minor improvements
Test Files Added:
tests/unit/mcpgateway/plugins/fixtures/configs/valid_tool_hooks.yaml
- Tool hook test fixture
Testing Command Sequence
# After rebase
git rebase origin/main
# Run all tests and linters
make doctest test smoketest lint-web flake8 pylint
# All passed successfully!
Code Quality Metrics
- Coverage: 80%
- Pylint Score: 10.00/10
- Security: No vulnerabilities found
- Performance: All tests complete in ~32 seconds
No Breaking Changes
✅ The passthrough headers feature is fully maintained and working correctly with the new plugin hooks.
Next Steps
- Remove debug print statements from PII filter
- Fix duplicate database field
- Update documentation for the new tool hooks feature
- Consider adding more comprehensive integration tests for the new tool hooks feature
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.
Nice work! I tested the examples and they work with a sample server.
Please address the change requests here.
It would be great to add documentation in the plugins and in the general docs for the tool hook usage, as well as having some specific tests for the tool hooks.
Working on a few logging and cleanup updates. |
Signed-off-by: Mihai Criveti <[email protected]>
* feat: add tool hooks to plugin system Signed-off-by: Shamsul Arefin <[email protected]> * feat: connect plugins to tool invoke, pii_filter improvements Signed-off-by: Shamsul Arefin <[email protected]> * Add PluginViolationError: If plugin blocks tool invocation. to the docstring's Raises section. Signed-off-by: Shamsul Arefin <[email protected]> * Apply linter fixes * Rebase and lint Signed-off-by: Mihai Criveti <[email protected]> * Update logging and docs Signed-off-by: Mihai Criveti <[email protected]> --------- Signed-off-by: Shamsul Arefin <[email protected]> Signed-off-by: Mihai Criveti <[email protected]> Co-authored-by: Shamsul Arefin <[email protected]> Co-authored-by: Mihai Criveti <[email protected]>
* feat: add tool hooks to plugin system Signed-off-by: Shamsul Arefin <[email protected]> * feat: connect plugins to tool invoke, pii_filter improvements Signed-off-by: Shamsul Arefin <[email protected]> * Add PluginViolationError: If plugin blocks tool invocation. to the docstring's Raises section. Signed-off-by: Shamsul Arefin <[email protected]> * Apply linter fixes * Rebase and lint Signed-off-by: Mihai Criveti <[email protected]> * Update logging and docs Signed-off-by: Mihai Criveti <[email protected]> --------- Signed-off-by: Shamsul Arefin <[email protected]> Signed-off-by: Mihai Criveti <[email protected]> Co-authored-by: Shamsul Arefin <[email protected]> Co-authored-by: Mihai Criveti <[email protected]>
* feat: add tool hooks to plugin system Signed-off-by: Shamsul Arefin <[email protected]> * feat: connect plugins to tool invoke, pii_filter improvements Signed-off-by: Shamsul Arefin <[email protected]> * Add PluginViolationError: If plugin blocks tool invocation. to the docstring's Raises section. Signed-off-by: Shamsul Arefin <[email protected]> * Apply linter fixes * Rebase and lint Signed-off-by: Mihai Criveti <[email protected]> * Update logging and docs Signed-off-by: Mihai Criveti <[email protected]> --------- Signed-off-by: Shamsul Arefin <[email protected]> Signed-off-by: Mihai Criveti <[email protected]> Co-authored-by: Shamsul Arefin <[email protected]> Co-authored-by: Mihai Criveti <[email protected]>
Summary
Implements
tool_pre_invoke
andtool_post_invoke
plugin hooks following the established pattern. This enables plugins to intercept, modify, and monitor tool invocations throughout the system. Closes #682Changes Made
Core Framework
ToolPreInvokePayload
,ToolPostInvokePayload
,ToolPreInvokeResult
,ToolPostInvokeResult
modelsHookType
enum withTOOL_PRE_INVOKE
andTOOL_POST_INVOKE
tool_pre_invoke()
andtool_post_invoke()
methodspre_tool_matches()
andpost_tool_matches()
for condition evaluationService Integration
invoke_tool()
workflowPluginViolationError
handling andfail_on_plugin_error
supportPlugin Updates
{'content': [{'text': '{"username": "[email protected]"}'}]}
Testing
valid_tool_hooks.yaml
configuration for tool-specific testingKey Features
Testing
Implements: #682