Skip to content

Add Dynamic Tool System with Lua Integration#58

Merged
linw1995 merged 7 commits intomainfrom
feat/dynamic_tools
Aug 20, 2025
Merged

Add Dynamic Tool System with Lua Integration#58
linw1995 merged 7 commits intomainfrom
feat/dynamic_tools

Conversation

@linw1995
Copy link
Copy Markdown
Owner

Summary

This PR introduces a comprehensive dynamic tool system for the nvim-mcp server, enabling users to define custom tools through Neovim Lua configuration. The system is marked as experimental due to its evolving nature.

Key Features

  • HybridToolRouter: Combines static tools with dynamic connection-scoped tools
  • Lua Tool Integration: Users can define custom tools in their Neovim config
  • Connection-Scoped Tools: Tools are isolated per connection with automatic cleanup
  • JSON Schema Validation: Robust parameter validation for custom tools
  • MCP Helper Functions: Lua utilities for creating MCP-compatible responses

Architecture Changes

  • Added hybrid_router.rs for dynamic tool routing with lock-free concurrent access
  • Added lua_tools.rs for Lua custom tool discovery and execution
  • Enhanced core.rs with dynamic tool registration API
  • Updated resources system to expose tool-connection mappings
  • Comprehensive test coverage for all new functionality

Experimental Warning

⚠️ The dynamic tool system is marked as experimental and may change significantly in future versions.

* Add Lua dynamic tools system for connection-specific tool registration

- Add jsonschema dependency for tool parameter validation
- Implement lua_tools.rs module with LuaToolDefinition struct
- Add Lua tool registration/unregistration in HybridToolRouter
- Create comprehensive PRP documentation for feature design
- Extend nvim-mcp Lua plugin with custom_tools configuration
- Add MCP helper functions for creating tool responses
- Include JSON schema validation for tool parameters
- Support connection-scoped cleanup of Lua tools
- Fix unused variable warnings in integration tests

* Refactor Lua dynamic tools system to use trait-based architecture

- Replace function-based DynamicTool with trait-based implementation
- Implement DynamicTool trait for LuaToolConfig with cleaner async methods
- Remove unused handler creation functions and type aliases
- Simplify tool execution with direct trait method calls
- Clean up integration tests by removing complex multi-connection scenarios
- Update type aliases for better clarity (DynamicToolBox, ConnectionToolMap)
- Improve error handling in Lua tool execution
- Remove dead code and unused imports for cleaner codebase

* Upgrade jsonschema dependency to 0.32.1 and add input validation for dynamic tools

- Update jsonschema from 0.18.3 to 0.32.1 for improved JSON schema validation
- Add validate_input method to DynamicTool trait with automatic schema validation
- Integrate input validation into HybridToolRouter execution flow
- Add comprehensive tests for dynamic tool input validation scenarios
- Clean up Cargo.lock dependencies (remove unused anyhow, update bit-set/bit-vec)

* Update documentation for comprehensive Lua dynamic tools system

- Enhanced CLAUDE.md with detailed Lua tool integration documentation
- Added lua_tools.rs module overview and architecture details
- Updated dynamic tool system documentation with user-extensible features
- Added MCP helper functions and tool lifecycle management details
- Updated PRPs/README.md with Phase 6 development status
- Documented custom tool registration through Neovim configuration

* Enhance Lua dynamic tools validation and refactor trait implementation

- Make DynamicTool::validate_input method abstract for implementation-specific validation
- Add LuaToolValidator with comprehensive JSON schema validation and error handling
- Implement validator initialization in LuaToolConfig with proper error propagation
- Add enhanced validation in LuaToolConfig::validate_input with fallback support
- Remove generic validation logic from DynamicTool trait to allow custom implementations
- Add comprehensive test coverage for validation scenarios and edge cases
- Remove outdated HybridToolRouter tests that are now covered by integration tests

* Add tool list change notifications for MCP client updates

Enable tool_list_changed capability in server capabilities and implement
notification system for dynamic tool registration. When new Neovim
connections are established via connect or connect_tcp tools, the MCP
client is notified of tool list changes to update available tools.

This ensures MCP clients stay synchronized with dynamically registered
connection-specific tools and improves the user experience by providing
real-time tool availability updates.
Resolved merge conflicts by combining:
- Dynamic tool system features from feat/dynamic_tools
- Automatic connection and HTTP server features from main
- Updated dependencies and documentation to include both feature sets
- Store connection IDs from auto-connection process
- Add discover_and_register_lua_tools method to NeovimMcpServer
- Call Lua tool discovery after successful auto-connections
- Improve error handling for Lua tool setup failures
Modify HybridToolRouter's list_tools implementation to properly inject
connection_id parameter into dynamic tool schemas, ensuring dynamic tools
display correct parameter requirements in MCP clients.

Changes:
- Clone tool schema before modification to avoid shared state issues
- Inject connection_id parameter into schema properties
- Maintain proper schema structure for MCP protocol compliance
- Add From<&dyn DynamicTool> for Tool trait implementation with automatic connection_id injection
- Add From<&DynamicToolBox> for Tool trait implementation for boxed tools
- Replace manual Tool construction with trait-based conversion in list_tools methods
- Implement comprehensive test suite covering schema injection and edge cases
- Ensure connection_id parameter is properly injected without overwriting existing definitions
- Add experimental warnings to dynamic tool features across documentation
- Clarify stability guarantees for production use
- Update architecture notes to indicate experimental status
- Add warnings to tool registration resources and Lua integration
- Maintain existing functionality while setting proper expectations
- Fix markdownlint line length violations
@linw1995 linw1995 linked an issue Aug 20, 2025 that may be closed by this pull request
@linw1995 linw1995 self-assigned this Aug 20, 2025
@linw1995 linw1995 added the enhancement New feature or request label Aug 20, 2025
@linw1995 linw1995 merged commit 0227d53 into main Aug 20, 2025
1 check passed
@linw1995 linw1995 deleted the feat/dynamic_tools branch August 20, 2025 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Custom Tool Registration via Lua Plugin

1 participant