Handle empty auth headers in tool conversion to avoid StopIteration#2649
Merged
crivetimihai merged 1 commit intomainfrom Feb 7, 2026
Merged
Handle empty auth headers in tool conversion to avoid StopIteration#2649crivetimihai merged 1 commit intomainfrom
crivetimihai merged 1 commit intomainfrom
Conversation
Member
|
Clean defensive fix — guarding against LGTM — ready to merge. |
Add guard check for empty decoded_auth_value before accessing dict keys in convert_tool_to_read for authheaders auth type. When auth_value decrypts to an empty dict, set auth to None instead of crashing. Closes #1430 Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com> Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
deb281e to
c80314e
Compare
crivetimihai
approved these changes
Feb 7, 2026
crivetimihai
approved these changes
Feb 7, 2026
kcostell06
pushed a commit
to kcostell06/mcp-context-forge
that referenced
this pull request
Feb 24, 2026
…ion (IBM#2649) Add guard check for empty decoded_auth_value before accessing dict keys in convert_tool_to_read for authheaders auth type. When auth_value decrypts to an empty dict, set auth to None instead of crashing. Closes IBM#1430 Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com> Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🐛 Bug-fix PR
Closes #1430
📌 Summary
This PR fixes a
StopIterationcrash in theToolService.convert_tool_to_readmethod that occurs when listing tools that haveauth_type="authheaders"but an empty or null authentication value in the database.The crash happens because the code assumes that if
auth_typeis "authheaders", thedecoded_auth_valuedictionary will contain at least one key. Whendecoded_auth_valueis empty (returned by decode_auth(None), callingnext(iter(decoded_auth_value))raisesStopIteration, causing a 500 Internal Server Error duringlist_toolsoperations.This fix makes the service robust against such malformed or incomplete data states by explicitly checking if
decoded_auth_valueis truthy before attempting to access its keys.🔁 Reproduction Steps
auth_type="authheaders".auth_valuetoNULLor an encrypted value that decrypts to{}.list_tools.StopIterationerror in the logs.A reproduction script
tests/reproduce_stop_iteration.pywas created to simulate this exact state.🐞 Root Cause
The issue was located in
mcpgateway/services/tool_service.pywithin theconvert_tool_to_readmethod.The
decode_authutility function returns an empty dictionary{}when the input isNone(which is the case for tools with nullauth_value. The code block forauthheadershandling blindly attempted to get the first key from this dictionary:💡 Fix Description
The fix involves adding a check to verify that
decoded_auth_valueis not empty before accessing it.If the auth value is empty, we effectively treat it as having no authentication configured (
tool_dict["auth"] = None), which prevents the crash and allows the tool to be listed, albeit without auth headers (which is correct since none exist).🧪 Verification
make lintmake testmake coverage📐 MCP Compliance (if relevant)
✅ Checklist
make black isort pre-commit)