Skip to content

Fix bug in listing operations#2173

Merged
crivetimihai merged 6 commits intomainfrom
fix-tool-listing
Jan 20, 2026
Merged

Fix bug in listing operations#2173
crivetimihai merged 6 commits intomainfrom
fix-tool-listing

Conversation

@madhav165
Copy link
Copy Markdown
Collaborator

@madhav165 madhav165 commented Jan 19, 2026

🐛 Bug-fix PR

Closes #2172


📌 Summary

Fixes a bug where a single entity with corrupted data would cause the entire listing operation to fail. When convert_*_to_read functions encountered invalid data (e.g., malformed JSON in auth_value, missing required fields), the exception would propagate up and return a 500 error instead of gracefully returning the valid entities.

🔁 Reproduction Steps

Issue: Parsing failures in entity conversion stops entire listing

  1. Have a tool/resource/prompt with corrupted data in the database (e.g., invalid JSON in auth_value field)
  2. Call any listing endpoint (/admin/tools, /tools/list, etc.)
  3. Before fix: 500 Internal Server Error - entire listing fails
  4. After fix: 200 OK - valid entities returned, corrupted entity skipped with error logged

🐞 Root Cause

The convert_*_to_read functions (e.g., convert_tool_to_read, convert_resource_to_read, convert_prompt_to_read) were called without exception handling in listing loops. When Pydantic validation failed for any single entity, the exception would bubble up and abort the entire operation.

Affected locations:

  • mcpgateway/services/tool_service.py - list_tools, list_server_tools, list_tools_for_user
  • mcpgateway/services/resource_service.py - resource listing methods
  • mcpgateway/services/prompt_service.py - prompt listing methods
  • mcpgateway/routers/admin.py - admin API endpoints

💡 Fix Description

Wrapped all convert_*_to_read calls in try-except blocks to handle conversion failures gracefully:

# Before
for tool in tools:
    result.append(convert_tool_to_read(tool))

# After
for tool in tools:
    try:
        result.append(convert_tool_to_read(tool))
    except Exception as e:
        logger.error(f"Failed to convert tool {tool.id} ({getattr(tool, 'name', 'unknown')}): {e}")
        continue

Key design points:

  • Failed conversions are logged with entity ID and name for debugging
  • Loop continues processing remaining valid entities
  • No change to return type or API contract
  • Added comprehensive unit and e2e tests to verify graceful handling

🧪 Verification

Check Command Status
Lint suite make lint pass
Unit tests make test pass

📐 MCP Compliance (if relevant)

  • Matches current MCP spec
  • No breaking change to MCP clients

✅ Checklist

  • Code formatted (make black isort pre-commit)
  • No secrets/credentials committed

@madhav165 madhav165 linked an issue Jan 19, 2026 that may be closed by this pull request
@madhav165 madhav165 changed the title Fix listing operation bug Fix bug in listing operations Jan 19, 2026
@crivetimihai crivetimihai self-assigned this Jan 19, 2026
@crivetimihai crivetimihai added this to the Release 1.0.0-BETA-2 milestone Jan 20, 2026
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
- Replace broad 'except Exception' with specific exceptions:
  ValidationError, ValueError, KeyError, TypeError, binascii.Error
- Use logger.exception() instead of logger.error() to include traceback
- Update e2e test to actually test corrupted data scenario by mocking
  convert_tool_to_read to fail for one tool while succeeding for others

This addresses concerns that broad exception catching could mask DB/session
errors or programming bugs. Now only data validation errors are caught
and logged, while unexpected errors will properly propagate.

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Add e2e tests verifying graceful error handling for:
- Resources listing
- Prompts listing
- Servers listing
- Gateways listing
- A2A agents listing

Each test creates entities, mocks the convert_*_to_read method to fail
for one entity, and verifies:
1. API returns 200 status
2. Valid entities are in the response
3. Corrupted entity is excluded from the response

This provides comprehensive test coverage for the graceful error
handling pattern across all entity types.

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
@crivetimihai crivetimihai merged commit f2a88fc into main Jan 20, 2026
50 checks passed
@crivetimihai crivetimihai deleted the fix-tool-listing branch January 20, 2026 08:56
kcostell06 pushed a commit to kcostell06/mcp-context-forge that referenced this pull request Feb 24, 2026
* Fix listing loops
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>

* Fix more calls to convert to read functions
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>

* Wrap all convert to read calls in try catch
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>

* Add additional tests
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>

* fix: Narrow exception handling to specific data validation errors

- Replace broad 'except Exception' with specific exceptions:
  ValidationError, ValueError, KeyError, TypeError, binascii.Error
- Use logger.exception() instead of logger.error() to include traceback
- Update e2e test to actually test corrupted data scenario by mocking
  convert_tool_to_read to fail for one tool while succeeding for others

This addresses concerns that broad exception catching could mask DB/session
errors or programming bugs. Now only data validation errors are caught
and logged, while unexpected errors will properly propagate.

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

* test: Add failure-path tests for all entity types

Add e2e tests verifying graceful error handling for:
- Resources listing
- Prompts listing
- Servers listing
- Gateways listing
- A2A agents listing

Each test creates entities, mocks the convert_*_to_read method to fail
for one entity, and verifies:
1. API returns 200 status
2. Valid entities are in the response
3. Corrupted entity is excluded from the response

This provides comprehensive test coverage for the graceful error
handling pattern across all entity types.

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

---------

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Co-authored-by: Mihai Criveti <crivetimihai@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Single entity parsing failure stops entire listing operation

2 participants