Skip to content

fix(tools): accept explicit null for optional str arguments - #236

Merged
lyonzin merged 1 commit into
lyonzin:masterfrom
vishalhabib99:fix/null-optional-args
Oct 13, 2026
Merged

lyonzin merged 1 commit into
lyonzin:masterfrom
vishalhabib99:fix/null-optional-args

Conversation

@vishalhabib99

@vishalhabib99 vishalhabib99 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Three optional tool arguments are annotated str = None: search_knowledge(category), list_documents(category) and add_from_url(title). The tool schema advertises "default": null on a field whose type is only string, so a client that sends that default back (some agent frameworks fill in every optional argument) gets a validation error, and the tool never runs:

list_documents {"category": null}
  -> Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]

This PR annotates them Optional[str] = None, the style the same functions already use for include_folders / exclude_folders. Callers that omit the argument see no change.

No issue filed. It's a three-line fix, so I opened the PR directly.

Type of change

  • feat — new feature
  • fix — bug fix
  • docs — documentation only
  • refactor — no behavior change
  • perf — performance improvement
  • test — adding or improving tests
  • chore — tooling, deps, CI
  • BREAKING CHANGE (explain in Migration section below)

What changed

  • mcp_server/server.py: category: str = None → Optional[str] = None on search_knowledge and list_documents, and the same change for title on add_from_url.
  • tests/test_tool_null_defaults.py: lists the registered MCP tools and checks that every argument whose schema default is null accepts null through the SDK's argument model. It fails on master (['search_knowledge.category', 'list_documents.category', 'add_from_url.title']) and passes here.
  • CHANGELOG.md: added an entry under ### Unreleased → Fixed.

Why

Checked on mcp 2.3.0, offline: 3 of the 5 null-default arguments reject null on master, and 0 do with this change. The other two were already Optional[List[str]]. I also ran await mcp.call_tool("list_documents", {"category": None}). On master it raises the error above. With this change it returns the normal {"status": "success", "filter": "all", ...} response, the same as omitting category.


7 Pillars Quality Gate

1. Security

  • No new secrets, tokens, or credentials in the diff
  • No new use of eval, exec, subprocess shell=True, pickle.loads on untrusted input, or arbitrary deserialization
  • New dependencies (if any) reviewed: N/A, none added
  • Path traversal, command injection, and SSRF surfaces: N/A, no new I/O code

2. Stability

  • All existing tests pass on Linux, Windows and macOS × Python 3.11/3.12/3.13. I ran macOS / 3.12 only, and CI will cover the rest of the matrix.
  • New behavior has a regression test
  • Coverage does not regress (adds a test, no new untested code)
  • No tests were skipped, deleted, or marked xfail

3. Memory leak

  • N/A for all four items: annotation-only change, no new objects, caches or state

4. Versatility

  • Platform-neutral (annotation change)
  • Optional[str] works on 3.11 through 3.13 and is already imported in server.py
  • No hardcoded paths, locales, or encodings
  • N/A: no parser touched

5. Scalability

  • No new algorithms
  • N/A: search, index and embed code paths are unchanged
  • No perf regression expected
  • No new shared mutable state

Performance impact: N/A. Only type annotations changed in server.py, and no runtime code path differs except that null is now accepted where it used to raise.

6. Versioning

  • Version files untouched (not a release PR)
  • Not a breaking change
  • Entry under ### Unreleased in CHANGELOG.md (scripts/check_changelog.py → [OK] CHANGELOG updated under Unreleased (type: fix))
  • Public API surface: scripts/check_api_surface.py --check → [OK] No breaking changes to public API surface. Parameter names are unchanged, and test_backwards_compat.py passes.

7. Quality

  • ruff check passes on the changed files
  • ruff format --check passes on the changed files
  • Type hints: N/A, no new public functions
  • Docstrings: the new test module and test function have them
  • Complexity: N/A
  • No dead code
  • Small PR: 3 changed lines + 1 test + 1 changelog bullet

Migration / Breaking changes

N/A

Test plan

  • pytest tests/ passed locally: 1251 passed, 14 skipped (macOS, Python 3.12, mcp 2.3.0, HF_HUB_OFFLINE=1)
  • pre-commit run --all-files: I didn't run it. I ran the ruff, api-surface and changelog checks it covers on the changed files instead, and all were clean.
  • Manual smoke test: mcp.call_tool("list_documents", {"category": None}) errors on master and succeeds here

Documentation

  • README: N/A (no user-facing docs change)
  • docs/: N/A
  • Added entry to ### Unreleased in CHANGELOG.md

Reviewer checklist

  • Reviewed line-by-line
  • Verified the 7 pillars CI status checks are green
  • Verified no obvious adversarial implications
  • Approved performance impact

Found with mcp-doctor's none_default_type check, then reproduced as above.

By submitting this PR I confirm I read CONTRIBUTING.md and agree to the Code of Conduct.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Fixed validation errors when explicitly passing null for optional category or title inputs in knowledge search, document listing, and URL-based document addition.

search_knowledge(category), list_documents(category) and
add_from_url(title) were annotated `str = None`. The tool schema then
advertised "default": null on a string-only field, and the MCP SDK's
argument model rejected {"category": null} before the tool ran.
Annotate them Optional[str], like the other optional arguments.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vishalhabib99
vishalhabib99 requested a review from lyonzin as a code owner October 6, 2026 23:02
@coderabbitai

coderabbitai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c4cc0978-0801-41af-9268-f4bc210d5963
📥 Commits

Reviewing files that changed from the base of the PR and between df9cccb and 59e6555.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • mcp_server/server.py
  • tests/test_tool_null_defaults.py

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.


📝 Walkthrough

Walkthrough

Three tool parameters now use Optional[str] annotations while retaining their None defaults. A test checks that tool argument models accept explicit None values for arguments with null schema defaults.

Changes

Nullable tool arguments

Layer / File(s) Summary
Nullable parameter annotations and validation
mcp_server/server.py, tests/test_tool_null_defaults.py, CHANGELOG.md
search_knowledge.category, list_documents.category, and add_from_url.title use Optional[str]. A test checks explicit None values against tool argument models with null schema defaults. The changelog records the change.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 59e65

Explicit null follows the documented optional-filter and automatic-title behavior. No actionable merge-blocking issue is established; the change is ready for normal checks.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: optional string arguments now accept explicit null values.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@vishalhabib99

Copy link
Copy Markdown
Contributor Author

Hi @lyonzin, just checking this is still on your radar. It still merges cleanly: three annotations go from str = None to Optional[str] = None, matching what include_folders / exclude_folders already use, plus a test and a CHANGELOG line. No rush, and I'm glad to change anything.

@lyonzin
lyonzin merged commit 0717e3c into lyonzin:master Oct 13, 2026
37 checks passed
@lyonzin lyonzin mentioned this pull request Oct 25, 2026
1 task done
lyonzin added a commit that referenced this pull request Oct 25, 2026
Patch release. Fixes the startup crash on every fresh install and every
new data directory since v4.10.0 (#248, #252), makes state-file writes
robust on Windows and stops reindex_documents from reporting a refused
run as started (#249). Also ships #236 and the multi-arch Docker image
from #242.

- pyproject.toml: 4.11.0 -> 4.11.1
- mcp_server/__init__.py: __version__ -> 4.11.1
- npm/package.json: 4.11.0 -> 4.11.1
- CHANGELOG.md: Unreleased promoted to v4.11.1 with highlights and credits
- .github/api-surface-baseline.json: regenerated with --snapshot. Additive
  only: 8 modules and 5 methods added since the 2026-08-10 snapshot, none
  removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
lyonzin pushed a commit that referenced this pull request Oct 25, 2026
search_knowledge(category), list_documents(category) and
add_from_url(title) were annotated `str = None`. The tool schema then
advertised "default": null on a string-only field, and the MCP SDK's
argument model rejected {"category": null} before the tool ran.
Annotate them Optional[str], like the other optional arguments.
lyonzin added a commit that referenced this pull request Oct 25, 2026
Patch release. Fixes the startup crash on every fresh install and every
new data directory since v4.10.0 (#248, #252), makes state-file writes
robust on Windows and stops reindex_documents from reporting a refused
run as started (#249). Also ships #236 and the multi-arch Docker image
from #242.

- pyproject.toml: 4.11.0 -> 4.11.1
- mcp_server/__init__.py: __version__ -> 4.11.1
- npm/package.json: 4.11.0 -> 4.11.1
- CHANGELOG.md: Unreleased promoted to v4.11.1 with highlights and credits
- .github/api-surface-baseline.json: regenerated with --snapshot. Additive
  only: 8 modules and 5 methods added since the 2026-08-10 snapshot, none
  removed.
lyonzin added a commit that referenced this pull request Nov 6, 2026
…'s collision suffix and path validation

search_knowledge and list_documents treat category="" like an omitted argument (MCP clients send empty strings for optional arguments, see #236), now documented and pinned by a test; add_from_url's saved name gains an 8/16-hex URL-hash suffix when the shorter name holds another file, so clients should use the returned filepath; its category becomes part of the path and must stay inside documents_dir (review on #276).
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