MCPcopy
hub / github.com/modelcontextprotocol/python-sdk / _handle_call_tool

Function _handle_call_tool

tests/shared/test_streamable_http.py:209–316  ·  view source on GitHub ↗
(ctx: ServerRequestContext[ServerState], params: CallToolRequestParams)

Source from the content-addressed store, hash-verified

207
208
209async def _handle_call_tool(ctx: ServerRequestContext[ServerState], params: CallToolRequestParams) -> CallToolResult:
210 name = params.name
211
212 # When the tool is called, send a notification to test GET stream
213 if name == "test_tool_with_standalone_notification":
214 await ctx.session.send_resource_updated(uri="http://test_resource")
215 return CallToolResult(content=[TextContent(type="text", text=f"Called {name}")])
216
217 elif name == "test_sampling_tool":
218 sampling_result = await ctx.session.create_message( # pyright: ignore[reportDeprecated]
219 messages=[
220 types.SamplingMessage(
221 role="user",
222 content=types.TextContent(type="text", text="Server needs client sampling"),
223 )
224 ],
225 max_tokens=100,
226 related_request_id=ctx.request_id,
227 )
228
229 assert sampling_result.content.type == "text"
230 return CallToolResult(
231 content=[
232 TextContent(
233 type="text",
234 text=f"Response from sampling: {sampling_result.content.text}",
235 )
236 ]
237 )
238
239 elif name == "wait_for_lock_with_notification":
240 await ctx.session.send_log_message( # pyright: ignore[reportDeprecated]
241 level="info",
242 data="First notification before lock",
243 logger="lock_tool",
244 related_request_id=ctx.request_id,
245 )
246
247 await ctx.lifespan_context.lock.wait()
248
249 await ctx.session.send_log_message( # pyright: ignore[reportDeprecated]
250 level="info",
251 data="Second notification after lock",
252 logger="lock_tool",
253 related_request_id=ctx.request_id,
254 )
255
256 return CallToolResult(content=[TextContent(type="text", text="Completed")])
257
258 elif name == "release_lock":
259 ctx.lifespan_context.lock.set()
260 return CallToolResult(content=[TextContent(type="text", text="Lock released")])
261
262 elif name == "tool_with_stream_close":
263 await ctx.session.send_log_message( # pyright: ignore[reportDeprecated]
264 level="info",
265 data="Before close",
266 logger="stream_close_tool",

Callers

nothing calls this directly

Calls 8

CallToolResultClass · 0.90
TextContentClass · 0.90
create_messageMethod · 0.80
send_log_messageMethod · 0.80
waitMethod · 0.80
send_resource_updatedMethod · 0.45
close_sse_streamMethod · 0.45

Tested by

no test coverage detected