-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(mcp): skip non-dict models/columns/rels in list and describe #2547
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
Open
Bartok9
wants to merge
2
commits into
Canner:main
Choose a base branch
from
Bartok9:fix/mcp-skip-non-dict-models
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| """Guards for non-dict MDL rows in MCP list/describe helpers (mirrors mcp_server).""" | ||
|
|
||
|
|
||
| def list_models_payload(models): | ||
| result = [] | ||
| for model in models: | ||
| if not isinstance(model, dict): | ||
| continue | ||
| description = model.get("description") | ||
| if description is None: | ||
| description = (model.get("properties") or {}).get("description") | ||
| columns = model.get("columns") or [] | ||
| if not isinstance(columns, list): | ||
| columns = [] | ||
| result.append( | ||
| { | ||
| "name": model.get("name"), | ||
| "description": description, | ||
| "column_count": len(columns), | ||
| } | ||
| ) | ||
| return {"models": result} | ||
|
|
||
|
|
||
| def describe_columns(model): | ||
| columns = [] | ||
| for col in model.get("columns") or []: | ||
| if not isinstance(col, dict): | ||
| continue | ||
| columns.append({"name": col.get("name")}) | ||
| return columns | ||
|
|
||
|
|
||
| def test_list_skips_junk(): | ||
| out = list_models_payload( | ||
| [ | ||
| {"name": "a", "columns": [{"name": "id"}]}, | ||
| None, | ||
| "x", | ||
| {"name": "b", "columns": "bad"}, | ||
| ] | ||
| ) | ||
| assert out["models"][0]["column_count"] == 1 | ||
| assert out["models"][1]["column_count"] == 0 | ||
| assert len(out["models"]) == 2 | ||
|
|
||
|
|
||
| def test_describe_skips_non_dict_cols(): | ||
| cols = describe_columns({"columns": [None, {"name": "id"}, "x"]}) | ||
| assert cols == [{"name": "id"}] |
Oops, something went wrong.
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.
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.
🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win
Incomplete guards against scalar YAML values.
Based on the PR objectives to prevent failures from malformed context output, the current type checks are insufficient because they do not protect against truthy scalar types. If malformed YAML values for
columns,properties, orrelationshipsparse as integers or booleans,or []andor {}will fall back to those exact scalars, leading toTypeErrorduring iteration/membership tests orAttributeErrorwhen chaining.get(). Furthermore, unvalidated strings can cause unintended substring matches duringinchecks.core/wren/src/wren/mcp_server.py#L267-L278: Guardmodel.get("columns")as a list before iterating, and verifycol.get("properties")is a dict before calling.get("description").core/wren/src/wren/mcp_server.py#L233-L238: Verifymodel.get("properties")is a dict before calling.get("description").core/wren/src/wren/mcp_server.py#L280-L285: Guardload_relationships(ctx.project)andrel.get("models")as lists before iterating or performinginchecks.core/wren/tests/unit/test_mcp_model_guards.py#L9-L14: Mirror thepropertiesdict check in the test helper.core/wren/tests/unit/test_mcp_model_guards.py#L26-L30: Mirror thecolumnslist check in the test helper.🛡️ Proposed fixes
core/wren/src/wren/mcp_server.pycore/wren/tests/unit/test_mcp_model_guards.py📝 Committable suggestion
📍 Affects 2 files
core/wren/src/wren/mcp_server.py#L267-L278(this comment)core/wren/src/wren/mcp_server.py#L233-L238core/wren/src/wren/mcp_server.py#L280-L285core/wren/tests/unit/test_mcp_model_guards.py#L9-L14core/wren/tests/unit/test_mcp_model_guards.py#L26-L30🤖 Prompt for AI Agents