Commit efb14ce
Python: Support structuredContent in MCP tool results and fix sampling options type (#4763)
* Support MCP sampling tools capability (#4625)
Forward systemPrompt, tools, and toolChoice from MCP sampling requests
to the chat client's get_response() call. Also advertise the
sampling.tools capability to MCP servers when a client is configured.
- Pass SamplingCapability with tools support to ClientSession
- Convert systemPrompt to instructions in options
- Convert MCP Tool objects to FunctionTool instances for options
- Map MCP ToolChoice.mode to tool_choice in options
- Add tests for all new behaviors and update existing sampling tests
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Fix #4625: Support MCP sampling tool with proper typing and structured content
- Fix mypy error by typing sampling callback options as ChatOptions[None]
instead of dict[str, Any], and importing ChatOptions from _types
- Handle structuredContent from CallToolResult in _parse_tool_result_from_mcp,
serializing it as JSON text Content when present
- Add tests for structuredContent parsing (with and without regular content)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Fix lint: add author to TODO comment
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Address review feedback for #4625: remove default=str, add edge-case tests
- Remove default=str from json.dumps for structuredContent to fail fast
on non-JSON-serializable values instead of silently converting
- Add test for non-JSON-serializable structuredContent (TypeError)
- Add tests for empty systemPrompt ('') and empty tools list ([]) edge
cases in sampling callback
- Expand TODO comment noting list[Content] return type constraint for
future result_type support
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Sanitize sampling callback error to avoid leaking internals (#4625)
Log exception details at DEBUG level instead of including them in the
ErrorData message returned to the MCP server, which may be untrusted.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Address review feedback for #4625: move params to options, restore error info
- Remove stale TODO comment about response_format (ChatOptions already has it)
- Restore {ex} in sampling callback error message for useful debugging info
- Set structuredContent as additional_property on Content for structured access
- Move temperature, max_tokens, stop into options dict (not top-level kwargs)
- Only set temperature when provided (not all models support it)
- Add tests for generation params in options and temperature omission
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Fix MCP sampling callback and structured content error handling (#4625)
- Guard max_tokens like temperature: only set when not None, so options
can properly evaluate to None when all params are absent
- Wrap json.dumps of structuredContent in try/except to fall back to
str() for non-serializable values instead of propagating TypeError
- Extract test_connect_sampling_capabilities_with_client into its own
test function so pytest can discover it independently
- Add test for max_tokens=None omission from options
- Update structured content non-serializable test to expect fallback
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Address review feedback for #4625: review comment fixes
* Fix MCP and Azure validation regressions
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent dd3d085 commit efb14ce
5 files changed
Lines changed: 409 additions & 7 deletions
File tree
- python/packages
- core
- agent_framework
- tests/core
- openai
- agent_framework_openai
- tests/openai
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
22 | 26 | | |
23 | 27 | | |
24 | 28 | | |
| |||
640 | 644 | | |
641 | 645 | | |
642 | 646 | | |
| 647 | + | |
643 | 648 | | |
644 | 649 | | |
645 | 650 | | |
646 | 651 | | |
647 | 652 | | |
648 | 653 | | |
649 | 654 | | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
650 | 661 | | |
651 | 662 | | |
652 | 663 | | |
| |||
657 | 668 | | |
658 | 669 | | |
659 | 670 | | |
| 671 | + | |
660 | 672 | | |
661 | 673 | | |
662 | 674 | | |
| |||
733 | 745 | | |
734 | 746 | | |
735 | 747 | | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
736 | 770 | | |
737 | 771 | | |
738 | 772 | | |
739 | | - | |
740 | | - | |
741 | | - | |
| 773 | + | |
742 | 774 | | |
743 | 775 | | |
| 776 | + | |
744 | 777 | | |
745 | 778 | | |
746 | 779 | | |
| |||
0 commit comments