python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

OutputSchema support in MCP tools

Open bzsurbhi opened this issue 9 months ago • 1 comments

Add outputSchema support to MCP tools

Motivation and Context

Implemented changes in PR for outputSchema

Key implementation details:

  1. Added outputSchema field to the Tool class to store the JSON schema for tool outputs
  2. Enhanced func_metadata to extract return type annotations and generate JSON schemas
  3. Graceful error handling when schema generation fails (logs warning but doesn't break functionality)

How Has This Been Tested?

Added tests with tools using various return types including:

  • Simple types (string, int, bool)
  • Complex types (Pydantic models)
  • None return types
  • Collection types (lists, dictionaries)

Breaking Changes

No breaking changes. The outputSchema is optional and backward compatible with existing tools.

Types of changes

  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [x] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)
  • [ ] Documentation update

Checklist

  • [x] I have read the MCP Documentation
  • [x] My code follows the repository's style guidelines
  • [x] New and existing tests pass locally
  • [x] I have added appropriate error handling
  • [x] I have added or updated documentation as needed

Additional context

bzsurbhi avatar May 07 '25 18:05 bzsurbhi

Looks like I missed this merge request, I'm happy for maintainers to cherry pick from https://github.com/modelcontextprotocol/python-sdk/pull/685 the bits you want to keep, if you want any help in cherry picking let me know

davemssavage avatar May 24 '25 10:05 davemssavage

Apologies for the time it took to get to this and thank you for your contribution.

I believe this would have already been implemented as part of https://github.com/modelcontextprotocol/python-sdk/pull/1005 which adds structured output support.

I'm therefore closing this PR but do feel free to re-open if there's a misunderstanding or you were intending to implement something different!

felixweinberger avatar Sep 05 '25 13:09 felixweinberger