@@ -1039,12 +1039,7 @@ For more control, you can use the low-level server implementation directly. This
1039
1039
1040
1040
<!-- snippet-source examples/snippets/servers/lowlevel/lifespan.py -->
1041
1041
``` python
1042
- """ Low-level server example showing lifespan management.
1043
-
1044
- This example demonstrates how to use the lifespan API to manage
1045
- server startup and shutdown, including resource initialization
1046
- and cleanup.
1047
-
1042
+ """
1048
1043
Run from the repository root:
1049
1044
uv run examples/snippets/servers/lowlevel/lifespan.py
1050
1045
"""
@@ -1160,13 +1155,9 @@ The lifespan API provides:
1160
1155
1161
1156
<!-- snippet-source examples/snippets/servers/lowlevel/basic.py -->
1162
1157
``` python
1163
- """ Basic low-level server example.
1164
-
1165
- This example demonstrates the low-level server API with minimal setup,
1166
- showing how to implement basic prompts using the raw protocol handlers.
1167
-
1158
+ """
1168
1159
Run from the repository root:
1169
- uv run examples/snippets/servers/lowlevel/basic.py
1160
+ uv run examples/snippets/servers/lowlevel/basic.py
1170
1161
"""
1171
1162
1172
1163
import asyncio
@@ -1198,7 +1189,7 @@ async def handle_get_prompt(name: str, arguments: dict[str, str] | None) -> type
1198
1189
if name != " example-prompt" :
1199
1190
raise ValueError (f " Unknown prompt: { name} " )
1200
1191
1201
- arg1_value = arguments.get(" arg1" , " default" ) if arguments else " default "
1192
+ arg1_value = ( arguments or {}) .get(" arg1" , " default" )
1202
1193
1203
1194
return types.GetPromptResult(
1204
1195
description = " Example prompt" ,
@@ -1243,11 +1234,7 @@ The low-level server supports structured output for tools, allowing you to retur
1243
1234
1244
1235
<!-- snippet-source examples/snippets/servers/lowlevel/structured_output.py -->
1245
1236
``` python
1246
- """ Low-level server example showing structured output support.
1247
-
1248
- This example demonstrates how to use the low-level server API to return
1249
- structured data from tools, with automatic validation against output schemas.
1250
-
1237
+ """
1251
1238
Run from the repository root:
1252
1239
uv run examples/snippets/servers/lowlevel/structured_output.py
1253
1240
"""
@@ -1268,20 +1255,22 @@ async def list_tools() -> list[types.Tool]:
1268
1255
""" List available tools with structured output schemas."""
1269
1256
return [
1270
1257
types.Tool(
1271
- name = " calculate " ,
1272
- description = " Perform mathematical calculations " ,
1258
+ name = " get_weather " ,
1259
+ description = " Get current weather for a city " ,
1273
1260
inputSchema = {
1274
1261
" type" : " object" ,
1275
- " properties" : {" expression " : {" type" : " string" , " description" : " Math expression " }},
1276
- " required" : [" expression " ],
1262
+ " properties" : {" city " : {" type" : " string" , " description" : " City name " }},
1263
+ " required" : [" city " ],
1277
1264
},
1278
1265
outputSchema = {
1279
1266
" type" : " object" ,
1280
1267
" properties" : {
1281
- " result" : {" type" : " number" },
1282
- " expression" : {" type" : " string" },
1268
+ " temperature" : {" type" : " number" , " description" : " Temperature in Celsius" },
1269
+ " condition" : {" type" : " string" , " description" : " Weather condition" },
1270
+ " humidity" : {" type" : " number" , " description" : " Humidity percentage" },
1271
+ " city" : {" type" : " string" , " description" : " City name" },
1283
1272
},
1284
- " required" : [" result " , " expression " ],
1273
+ " required" : [" temperature " , " condition " , " humidity " , " city " ],
1285
1274
},
1286
1275
)
1287
1276
]
@@ -1290,19 +1279,21 @@ async def list_tools() -> list[types.Tool]:
1290
1279
@server.call_tool ()
1291
1280
async def call_tool (name : str , arguments : dict[str , Any]) -> dict[str , Any]:
1292
1281
""" Handle tool calls with structured output."""
1293
- if name == " calculate" :
1294
- expression = arguments[" expression" ]
1295
- try :
1296
- # WARNING: eval() is dangerous! Use a safe math parser in production
1297
- result = eval (expression)
1298
- structured = {" result" : result, " expression" : expression}
1299
-
1300
- # low-level server will validate structured output against the tool's
1301
- # output schema, and automatically serialize it into a TextContent block
1302
- # for backwards compatibility with pre-2025-06-18 clients.
1303
- return structured
1304
- except Exception as e:
1305
- raise ValueError (f " Calculation error: { str (e)} " )
1282
+ if name == " get_weather" :
1283
+ city = arguments[" city" ]
1284
+
1285
+ # Simulated weather data - in production, call a weather API
1286
+ weather_data = {
1287
+ " temperature" : 22.5 ,
1288
+ " condition" : " partly cloudy" ,
1289
+ " humidity" : 65 ,
1290
+ " city" : city, # Include the requested city
1291
+ }
1292
+
1293
+ # low-level server will validate structured output against the tool's
1294
+ # output schema, and additionally serialize it into a TextContent block
1295
+ # for backwards compatibility with pre-2025-06-18 clients.
1296
+ return weather_data
1306
1297
else :
1307
1298
raise ValueError (f " Unknown tool: { name} " )
1308
1299
0 commit comments