(ctx: ServerRequestContext[ServerState], params: CallToolRequestParams)
| 207 | |
| 208 | |
| 209 | async 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", |
nothing calls this directly
no test coverage detected