fix: Enable string-to-integer conversion for build_context depth parameter#259
Merged
Conversation
…meter Add support for passing string values to the depth parameter in the build_context MCP tool. The function now accepts both integer and string values, automatically converting valid numeric strings to integers while raising clear errors for invalid inputs. Changes: - Updated type annotation from Optional[int] to Optional[Union[int, str]] - Added string validation and conversion logic with proper error handling - Enhanced test coverage for both MCP and CLI interfaces - Maintains backward compatibility with existing integer parameters 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Joe P <joe@basicmemory.com>
phernandez
approved these changes
Aug 20, 2025
| async def build_context( | ||
| url: MemoryUrl, | ||
| depth: Optional[int] = 1, | ||
| depth: Optional[Union[int, str]] = 1, |
Member
There was a problem hiding this comment.
In my experience, adding more types to the mcp args will sometimes mess up tools like vscode, but lets give this a shot. It's more "correct". FYI, I prefer Optional[int | str] or even a type alias
type StringOrInt = int | str
depth: Optional[StringOrInt]
Contributor
Author
There was a problem hiding this comment.
I can try that. This one was weird. The tests that are in this PR actually don't catch the problem -- they pass even without the code change because the string is converted to an int without problems. But testing with the LLM having it "call build_context tool with depth = '3'" would fail just as it was reported in the bug.
Replace Union[int, str] with cleaner StringOrInt type alias using modern Python 3.12+ type statement syntax. This improves code readability while maintaining the same functionality for string-to-integer conversion in the depth parameter. Changes: - Add StringOrInt type alias using modern type statement - Update depth parameter annotation to use StringOrInt - Maintains all existing string conversion and validation logic 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Joe P <joe@basicmemory.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.
Summary
Changes Made
Optional[int]toOptional[Union[int, str]]Test Plan
Verification
The fix has been tested and verified to work correctly:
🤖 Generated with Claude Code