Skip to content

Commit 0b0b478

Browse files
committed
Split the tools-mode sampling overloads for sound narrowing
The broadened two-overload shape let a caller passing optional tools/tool_choice variables infer CreateMessageResultWithTools while the runtime could return the plain result. Three overloads (neither, tools, tool_choice) keep literal calls exact and type unnarrowed optional arguments as the result union, which matches the runtime.
1 parent 557f36b commit 0b0b478

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

src/mcp/server/session.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,31 @@ async def create_message(
165165
stop_sequences: list[str] | None = None,
166166
metadata: dict[str, Any] | None = None,
167167
model_preferences: types.ModelPreferences | None = None,
168-
tools: list[types.Tool] | None = None,
168+
tools: list[types.Tool],
169169
tool_choice: types.ToolChoice | None = None,
170170
related_request_id: types.RequestId | None = None,
171171
) -> types.CreateMessageResultWithTools:
172-
"""Overload: With tools or tool_choice, returns array-capable content."""
172+
"""Overload: With tools, returns array-capable content."""
173+
...
174+
175+
@overload
176+
@deprecated("The sampling capability is deprecated as of 2026-07-28 (SEP-2577).", category=MCPDeprecationWarning)
177+
async def create_message(
178+
self,
179+
messages: list[types.SamplingMessage],
180+
*,
181+
max_tokens: int,
182+
system_prompt: str | None = None,
183+
include_context: types.IncludeContext | None = None,
184+
temperature: float | None = None,
185+
stop_sequences: list[str] | None = None,
186+
metadata: dict[str, Any] | None = None,
187+
model_preferences: types.ModelPreferences | None = None,
188+
tools: list[types.Tool] | None = None,
189+
tool_choice: types.ToolChoice,
190+
related_request_id: types.RequestId | None = None,
191+
) -> types.CreateMessageResultWithTools:
192+
"""Overload: With tool_choice, returns array-capable content."""
173193
...
174194

175195
@deprecated("The sampling capability is deprecated as of 2026-07-28 (SEP-2577).", category=MCPDeprecationWarning)

src/mcp/shared/peer.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,29 @@ async def sample(
115115
stop_sequences: list[str] | None = None,
116116
metadata: dict[str, Any] | None = None,
117117
model_preferences: ModelPreferences | None = None,
118-
tools: list[Tool] | None = None,
118+
tools: list[Tool],
119119
tool_choice: ToolChoice | None = None,
120120
meta: Meta | None = None,
121121
opts: CallOptions | None = None,
122122
) -> CreateMessageResultWithTools: ...
123+
@overload
124+
@deprecated("The sampling capability is deprecated as of 2026-07-28 (SEP-2577).", category=MCPDeprecationWarning)
125+
async def sample(
126+
self,
127+
messages: list[SamplingMessage],
128+
*,
129+
max_tokens: int,
130+
system_prompt: str | None = None,
131+
include_context: IncludeContext | None = None,
132+
temperature: float | None = None,
133+
stop_sequences: list[str] | None = None,
134+
metadata: dict[str, Any] | None = None,
135+
model_preferences: ModelPreferences | None = None,
136+
tools: list[Tool] | None = None,
137+
tool_choice: ToolChoice,
138+
meta: Meta | None = None,
139+
opts: CallOptions | None = None,
140+
) -> CreateMessageResultWithTools: ...
123141
@deprecated("The sampling capability is deprecated as of 2026-07-28 (SEP-2577).", category=MCPDeprecationWarning)
124142
async def sample(
125143
self,

0 commit comments

Comments
 (0)