Repository navigation
fix(codegen): annotate optional tool parameters as Optional - #105
Open
vishalhabib99 wants to merge 1 commit into
Open
vishalhabib99 wants to merge 1 commit into
vishalhabib99 wants to merge 1 commit into
Conversation
Optional query and body parameters were emitted as `name: T = None`, so every generated MCP tool advertised `"default": null` on a type that excludes null. A client that sends that default explicitly gets an input validation error and the tool never runs. Emit `Optional[T] = None` instead (tool modules already import Optional). Adds a regression test that generates a tool module offline, registers it on FastMCP and calls it with each optional argument set to None.
vishalhabib99
requested review from
rehanthestar21 and
sriaradhyula
as code owners
October 23, 2026 19:24
This branch has not been deployed
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.
Description
The generator emits optional query and body parameters as
name: T = None. FastMCP turns that into an input schema with"default": nullon a type that doesn't allow null. A client that sends the advertised default explicitly gets an input validation error, and the tool never runs.Example from the committed
examples/argocdserver (fastmcp 2.11.1, built in memory, no network):In the committed examples, this affects 79 tools in argocd, 59 in argo-workflows, 28 in splunk and 6 in argo-rollouts.
Fix: emit
Optional[T] = Nonefor optional parameters, which also covers the dict-modebodyparameter. This touches 4 f-strings inmcp_codegen.py, at the 4 places that write= None.tools/tool.tplalready importsOptional, so the generated modules need no other change. This is the change requested in #42, which was closed as stale without a fix.Test:
test_optional_parameters_accept_explicit_nullgenerates a tool module offline from a small spec with optional query and body parameters, registers it onFastMCP, and calls it in memory with each optional argument set toNone. The API client is stubbed, so no LLM or HTTP call is made. The test fails onmain(param_limit=None rejected: Input validation error: None is not of type 'integer') and passes with this change. The assertion intest_extract_body_params_simpleis updated to match the new signature string.Suite: on
main, the suite gives 23 failed and 1 passed. With this change it gives 23 failed and 2 passed, and the 23 failures are the same. They are pre-existing and unrelated: thesetup_envfixture points atexamples/petstore/openapi_petstore.json, but the file isopenapi-petstore.json.test_cli_accepts_enable_slimalso fails on thegenerate-mcpsubcommand. The new test doesn't use that fixture, so it runs either way.Not included: the committed
examples/*/generate_codeare not regenerated. Running the current generator onmainoffline (no LLM) already gives a large diff against the committed argocd tools, unrelated to this change, so regenerating here would bury the fix. After this change, a fresh offline run onexamples/argocd/openapi-argocd.jsongives 0 tools with this issue, down from 79. Maintainers can regenerate the examples whenever suits them.Type of Change
Checklist
mcp_codegen.py, lines 188 and 1402, are untouched)