fix(mcp): coerce numeric tool args defensively in mcp_serve#21329
Merged
Conversation
Contributor
🔎 Lint report:
|
| Rule | Count |
|---|---|
invalid-argument-type |
3 |
First entries
run_agent.py:12365: [invalid-argument-type] invalid-argument-type: Argument to function `_is_oauth_token` is incorrect: Expected `str`, found `str | dict[Unknown, Unknown] | Any | ... omitted 3 union elements`
run_agent.py:6504: [invalid-argument-type] invalid-argument-type: Argument to function `build_anthropic_client` is incorrect: Expected `str`, found `str | dict[Unknown, Unknown] | Any | ... omitted 3 union elements`
run_agent.py:12368: [invalid-argument-type] invalid-argument-type: Argument to function `len` is incorrect: Expected `Sized`, found `(str & ~AlwaysFalsy) | (dict[Unknown, Unknown] & ~AlwaysFalsy) | (Any & ~AlwaysFalsy) | ... omitted 3 union elements`
✅ Fixed issues (3):
| Rule | Count |
|---|---|
invalid-argument-type |
3 |
First entries
run_agent.py:6504: [invalid-argument-type] invalid-argument-type: Argument to function `build_anthropic_client` is incorrect: Expected `str`, found `str | dict[Unknown | str, Unknown | str | dict[str, str]] | Any | ... omitted 3 union elements`
run_agent.py:12365: [invalid-argument-type] invalid-argument-type: Argument to function `_is_oauth_token` is incorrect: Expected `str`, found `str | dict[Unknown | str, Unknown | str | dict[str, str]] | Any | ... omitted 3 union elements`
run_agent.py:12368: [invalid-argument-type] invalid-argument-type: Argument to function `len` is incorrect: Expected `Sized`, found `(str & ~AlwaysFalsy) | (dict[Unknown | str, Unknown | str | dict[str, str]] & ~AlwaysFalsy) | (Any & ~AlwaysFalsy) | ... omitted 3 union elements`
Unchanged: 4001 pre-existing issues carried over.
Diagnostics are surfaced as warnings — this check never fails the build.
19 tasks
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.
Salvage of #21055 by @qWaitCrypto onto current main. Cherry-picked clean.
Summary
Hermes' MCP server (
mcp_serve.py) exposes several tools that annotate parameters asintbut then feed them directly into slicing (conversations[:limit]),min(timeout_ms, 300000), and bridge calls. External MCP clients are free to send"50",null, floats, or garbage — which crash at runtime withTypeError: slice indices must be integers or None.Add
_coerce_int(value, default, minimum, maximum)that converts to int with fallback to default, clamps to range, and apply it to the four external-facing tool parameters.Bug repro (verified live on main)
conversations[:"50"]TypeErrorconversations[:"bad"]TypeErrorconversations[:50.5]TypeErrormin(None, 300000)TypeErrorconversations[:None]Changes
mcp_serve.py: +19 lines for_coerce_inthelper, +8 lines applying it at 4 tool boundaries (conversations_list.limit,messages_read.limit,events_poll.after_cursor/limit,events_wait.after_cursor/timeout_ms).tests/test_mcp_serve.py: +101 lines of coercion regression coverage (5 tests).scripts/release.py: AUTHOR_MAP entry for @qWaitCrypto.Validation
scripts/run_tests.sh tests/test_mcp_serve.pyTypeErroron"50","bad",50.5_coerce_intwith 8 edge cases (strings, None, floats, out-of-range, negative)Closes #21055. @qWaitCrypto's authorship preserved via rebase-merge.