OutputSchema support in MCP tools
Add outputSchema support to MCP tools
Motivation and Context
Implemented changes in PR for outputSchema
Key implementation details:
- Added
outputSchemafield to theToolclass to store the JSON schema for tool outputs - Enhanced
func_metadatato extract return type annotations and generate JSON schemas - 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
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
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!