Skip to content

fix: correct get_more_tools schema, harden truncation, and capture intent#25

Merged
naji247 merged 5 commits intomainfrom
fix/misc-schema-truncation-fixes
Feb 27, 2026
Merged

fix: correct get_more_tools schema, harden truncation, and capture intent#25
naji247 merged 5 commits intomainfrom
fix/misc-schema-truncation-fixes

Conversation

@naji247
Copy link
Member

@naji247 naji247 commented Feb 27, 2026

Summary

  • Schema fix: Make get_more_tools context parameter a required string instead of nullable/optional, preventing Pydantic from generating anyOf: [string, null] schemas that confuse LLM clients. Adds GET_MORE_TOOLS_SCHEMA constant and forces correct schema post-registration in community FastMCP.
  • DRY refactor: Extract _ensure_context_parameter() helper in community tool manager to eliminate duplicated context parameter injection logic across patch_existing_tools and patch_add_tool_fn.
  • Truncation robustness: Fix a crash when truncation reduced depth to 0, causing model_validate() to fail on string markers replacing dict-typed fields. Introduces MIN_DEPTH, breadth reduction as a fallback, and fresh model_dump() per pass to avoid compounding artifacts.
  • Intent capture: get_more_tools calls now capture user_intent from the context argument in published events, matching the behavior of regular tool calls with context enabled.

Test plan

  • Run full test suite: uv run pytest
  • Verify schema tests pass for all three server types (official, community, community v3): uv run pytest tests/test_tool_context.py tests/community/test_community_report_missing.py -v
  • Verify truncation regression tests pass: uv run pytest tests/test_truncation.py -v
  • Verify user intent capture test passes: uv run pytest tests/test_tool_context.py::TestUserIntentCaptureInEvents -v

naji247 and others added 3 commits February 26, 2026 21:54
…tent

- Make get_more_tools context parameter a required string instead of
  nullable/optional to prevent anyOf schema generation by Pydantic
- Extract _ensure_context_parameter() helper in community tool manager
  to DRY up duplicated schema injection logic
- Fix truncation depth=0 crash by introducing MIN_DEPTH and breadth
  reduction as a fallback; each pass now starts from a fresh model_dump
- Capture user_intent from context on get_more_tools calls in events
- Add schema validation, megabyte string, and many-keys regression tests
# Always start from a fresh dump to avoid compounding artifacts
truncated_dict = _truncate_value(
truncated_dict, max_depth=depth, max_string_bytes=string_bytes,
event.model_dump(),
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't feed in entire event here, since that can truncate the protected fields (resource name, timestamp, etc.) in case another field is too big. Added unit test for this.

await fastmcp_server.call_tool("get_more_tools", {})
assert False, "Expected ToolError for missing required context parameter"
except Exception:
pass # Expected: context is required
Copy link
Member

Choose a reason for hiding this comment

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

Should probably check the correct type of exception returned. Something like pytest.raises(Exception, match=...) with a regex

Truncation now targets only TRUNCATABLE_FIELDS (parameters, response,
error, etc.) instead of blindly truncating the entire event dict. This
prevents metadata like event_type, resource_name, session_id, and
actor_id from being mangled under extreme payload pressure.

Also hardens breadth reduction floor (10 -> 1) and tightens test
assertions for get_more_tools error cases using pytest.raises.
Older FastMCP versions use JSON Schema validation messages (e.g.
"'context' is a required property") while newer versions use Pydantic
messages ("Missing required argument"). Use version-agnostic patterns
like (?i)required and (?i)string to match both.
@naji247 naji247 merged commit 606ff12 into main Feb 27, 2026
37 checks passed
@naji247 naji247 deleted the fix/misc-schema-truncation-fixes branch February 27, 2026 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants