Skip to content

fix: refactor mcp with visitor#6545

Open
ravenac95 wants to merge 18 commits intomainfrom
ravenac95/refactor-mcp-with-visitor
Open

fix: refactor mcp with visitor#6545
ravenac95 wants to merge 18 commits intomainfrom
ravenac95/refactor-mcp-with-visitor

Conversation

@ravenac95
Copy link
Member

Bit of a major refactor to try to make this code more maintainable and correct. I have found some additional bugs since making this but I think we need to review this now. Otherwise it'll be too large a change for anyone to look at.

@vercel
Copy link

vercel bot commented Feb 5, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
kariba-network Ready Ready Preview, Comment Feb 5, 2026 5:01pm
oso-www Ready Ready Preview, Comment Feb 5, 2026 5:01pm

Request Review

@github-actions
Copy link

github-actions bot commented Feb 5, 2026

Code Review - High Signal Issues Found

Issue 1: Missing discriminator field for union types

File: warehouse/oso_mcp/oso_mcp/server/graphql/pydantic_generator.py
Method: handle_leave_union (around line 1210-1217 in the diff)
Description: When adding a union field to the parent context, the discriminator field is not being set.
Reason: Logic error - union fields require discriminator="typename__" in Pydantic for proper deserialization

The code currently does:

if not is_required:
    python_type = t.Optional[python_type]
    field_info = Field(default=None)
else:
    field_info = Field()

current_context = self.require_model_context("add union field")

But it should include the discriminator:

if not is_required:
    python_type = t.Optional[python_type]
    field_info = Field(default=None, discriminator="typename__")
else:
    field_info = Field(discriminator="typename__")

current_context = self.require_model_context("add union field")

This will cause runtime errors when trying to deserialize GraphQL responses containing union types because Pydantic won't know how to discriminate between the different union member types.

)
schema_start = fragment.type_condition
assert schema_start is not None, (
f"Schema does not have a {schema_start} type defined"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The assertion error message incorrectly references schema_start (which is a GraphQLObjectType object) instead of fragment.type_condition_name (which is the string name). This will cause a poorly formatted error message if the assertion fails.

The error message should be:

Suggested change
f"Schema does not have a {schema_start} type defined"
f"Schema does not have a {fragment.type_condition_name} type defined"

Comment on lines +660 to +662
field_info = Field(default=None)
else:
field_info = Field()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing discriminator parameter for union field. Pydantic requires discriminator="typename__" to deserialize union types correctly based on the __typename field from GraphQL responses. This will cause runtime failures when deserializing responses containing union types. See lines 584-586 where the discriminator is correctly set for comparison.

Comment on lines 513 to +515

fields[var_name] = (python_type, field_info)
current_context = self.current_context
match current_context:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-exhaustive pattern matching - if current_context is neither PydanticModelBuildContext nor UnionTypeBuildContext, the match silently does nothing. Add a default case to raise an error or use require_model_context() to ensure type safety and prevent silent data loss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code-review Triggers `/code-review:code-review` using Claude Code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant