Skip to content

fix(mcp-lambda-handler): exclude optional params from required list#3780

Open
mikegc-aws wants to merge 3 commits into
mainfrom
fix/mcp-lambda-handler-optional-params-required
Open

fix(mcp-lambda-handler): exclude optional params from required list#3780
mikegc-aws wants to merge 3 commits into
mainfrom
fix/mcp-lambda-handler-optional-params-required

Conversation

@mikegc-aws

@mikegc-aws mikegc-aws commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Optional parameters (Optional[T] types or params with default values) were unconditionally added to the required list in the tool JSON schema. This caused MCP clients/LLMs to treat every parameter as mandatory, prompting users for values that should be optional.

Changes

  • Check for Union[X, None] (i.e. Optional[T]) type hints before adding to required
  • Check for parameters with default values via inspect.signature
  • Added tests: test_tool_decorator_required_vs_optional_params and test_tool_decorator_all_required_params
  • Updated existing test_tool_decorator_optional_type_hints to assert required == []
  • Added a demo script showing correct schema generation

Before (broken)

@mcp.tool()
def search(query: str, limit: Optional[int] = 10):
    ...
# required: ['query', 'limit']  <-- limit should NOT be required

After (fixed)

@mcp.tool()
def search(query: str, limit: Optional[int] = 10):
    ...
# required: ['query']  <-- correct

Fixes #1119

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.

Optional parameters (Optional[T] types or params with default values)
were unconditionally added to the 'required' list in the tool JSON
schema. This caused MCP clients/LLMs to treat every parameter as
mandatory, prompting users for values that should be optional.

Now checks for Union[X, None] type hints and inspect.Parameter defaults
before adding to required.

Fixes #1119
@codecov

codecov Bot commented Jun 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.93%. Comparing base (0a6e457) to head (eeebcfa).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3780   +/-   ##
=======================================
  Coverage   92.93%   92.93%           
=======================================
  Files         987      987           
  Lines       82364    82368    +4     
  Branches    13043    13044    +1     
=======================================
+ Hits        76545    76549    +4     
  Misses       3508     3508           
  Partials     2311     2311           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

(mcp-lambda-handler): (adds optional tool parameters to required parameters list)

1 participant