gradio-app / gradio-app/toolsets
UnboundLocalError when using defer_loading with no base tools
- Dominant language
- Python
- Stars
- 14
- Forks
- 3
- PR merge metrics
- No merged PRs in 30d
Description
## Bug Description
When using `defer_loading=True` for **all** servers (no base tools loaded directly), the Gradio UI crashes with an `UnboundLocalError` because `make_click_handler` is defined inside a `for` loop that never executes when there are no base tools.
## Steps to Reproduce
```python
from toolsets import Server, Toolset
toolset = Toolset("Test")
# Add server with deferred loading (all tools deferred, no base tools)
toolset.add(
Server("https://example.com/mcp"),
defer_loading=True
)
# This crashes
toolset.launch(mcp_server=True)
```
## Error
```
File "...\toolsets\gradio_ui.py", line 111, in launch_gradio_ui
make_click_handler(search_tool_data["inputSchema"]),
^^^^^^^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'make_click_handler' where it is not associated with a value
```
## Root Cause
In `gradio_ui.py`, the `make_click_handler` function is defined inside the `for` loop at line 81:
```python
for tool_name, tool_data in toolset._tool_data.items():
# ...
def make_click_handler(schema):
return lambda: schema
# ...
```
When all tools are deferred, `toolset._tool_data` is empty, so the loop never executes and `make_click_handler` is never defined. Later, at line 111, the code tries to use `make_click_handler` for the deferred tools UI, causing the error.
## Suggested Fix
Move the `make_click_handler` definition **before** the `for` loop:
```python
# Define before the loop
def make_click_handler(schema):
return lambda: schema
for tool_name, tool_data in toolset._tool_data.items():
h = gr.HTML(...)
h.click(make_click_handler(tool_data["inputSchema"]), outputs=j)
```
## Environment
- toolsets version: 0.1.9
- Python: 3.14
- OS: Windows 11
## Workaround
Users can manually patch `gradio_ui.py` by moving the function definition outside the loop.
Contributor guide
Assessment
This issue has not been assessed yet.