@@ -1054,12 +1054,7 @@ For more control, you can use the low-level server implementation directly. This
1054
1054
1055
1055
<!-- snippet-source examples/snippets/servers/lowlevel/lifespan.py -->
1056
1056
``` python
1057
- """ Low-level server example showing lifespan management.
1058
-
1059
- This example demonstrates how to use the lifespan API to manage
1060
- server startup and shutdown, including resource initialization
1061
- and cleanup.
1062
-
1057
+ """
1063
1058
Run from the repository root:
1064
1059
uv run examples/snippets/servers/lowlevel/lifespan.py
1065
1060
"""
@@ -1175,13 +1170,9 @@ The lifespan API provides:
1175
1170
1176
1171
<!-- snippet-source examples/snippets/servers/lowlevel/basic.py -->
1177
1172
``` python
1178
- """ Basic low-level server example.
1179
-
1180
- This example demonstrates the low-level server API with minimal setup,
1181
- showing how to implement basic prompts using the raw protocol handlers.
1182
-
1173
+ """
1183
1174
Run from the repository root:
1184
- uv run examples/snippets/servers/lowlevel/basic.py
1175
+ uv run examples/snippets/servers/lowlevel/basic.py
1185
1176
"""
1186
1177
1187
1178
import asyncio
@@ -1213,7 +1204,7 @@ async def handle_get_prompt(name: str, arguments: dict[str, str] | None) -> type
1213
1204
if name != " example-prompt" :
1214
1205
raise ValueError (f " Unknown prompt: { name} " )
1215
1206
1216
- arg1_value = arguments.get(" arg1" , " default" ) if arguments else " default "
1207
+ arg1_value = ( arguments or {}) .get(" arg1" , " default" )
1217
1208
1218
1209
return types.GetPromptResult(
1219
1210
description = " Example prompt" ,
@@ -1258,11 +1249,7 @@ The low-level server supports structured output for tools, allowing you to retur
1258
1249
1259
1250
<!-- snippet-source examples/snippets/servers/lowlevel/structured_output.py -->
1260
1251
``` python
1261
- """ Low-level server example showing structured output support.
1262
-
1263
- This example demonstrates how to use the low-level server API to return
1264
- structured data from tools, with automatic validation against output schemas.
1265
-
1252
+ """
1266
1253
Run from the repository root:
1267
1254
uv run examples/snippets/servers/lowlevel/structured_output.py
1268
1255
"""
@@ -1283,20 +1270,22 @@ async def list_tools() -> list[types.Tool]:
1283
1270
""" List available tools with structured output schemas."""
1284
1271
return [
1285
1272
types.Tool(
1286
- name = " calculate " ,
1287
- description = " Perform mathematical calculations " ,
1273
+ name = " get_weather " ,
1274
+ description = " Get current weather for a city " ,
1288
1275
inputSchema = {
1289
1276
" type" : " object" ,
1290
- " properties" : {" expression " : {" type" : " string" , " description" : " Math expression " }},
1291
- " required" : [" expression " ],
1277
+ " properties" : {" city " : {" type" : " string" , " description" : " City name " }},
1278
+ " required" : [" city " ],
1292
1279
},
1293
1280
outputSchema = {
1294
1281
" type" : " object" ,
1295
1282
" properties" : {
1296
- " result" : {" type" : " number" },
1297
- " expression" : {" type" : " string" },
1283
+ " temperature" : {" type" : " number" , " description" : " Temperature in Celsius" },
1284
+ " condition" : {" type" : " string" , " description" : " Weather condition" },
1285
+ " humidity" : {" type" : " number" , " description" : " Humidity percentage" },
1286
+ " city" : {" type" : " string" , " description" : " City name" },
1298
1287
},
1299
- " required" : [" result " , " expression " ],
1288
+ " required" : [" temperature " , " condition " , " humidity " , " city " ],
1300
1289
},
1301
1290
)
1302
1291
]
@@ -1305,19 +1294,21 @@ async def list_tools() -> list[types.Tool]:
1305
1294
@server.call_tool ()
1306
1295
async def call_tool (name : str , arguments : dict[str , Any]) -> dict[str , Any]:
1307
1296
""" Handle tool calls with structured output."""
1308
- if name == " calculate" :
1309
- expression = arguments[" expression" ]
1310
- try :
1311
- # WARNING: eval() is dangerous! Use a safe math parser in production
1312
- result = eval (expression)
1313
- structured = {" result" : result, " expression" : expression}
1314
-
1315
- # low-level server will validate structured output against the tool's
1316
- # output schema, and automatically serialize it into a TextContent block
1317
- # for backwards compatibility with pre-2025-06-18 clients.
1318
- return structured
1319
- except Exception as e:
1320
- raise ValueError (f " Calculation error: { str (e)} " )
1297
+ if name == " get_weather" :
1298
+ city = arguments[" city" ]
1299
+
1300
+ # Simulated weather data - in production, call a weather API
1301
+ weather_data = {
1302
+ " temperature" : 22.5 ,
1303
+ " condition" : " partly cloudy" ,
1304
+ " humidity" : 65 ,
1305
+ " city" : city, # Include the requested city
1306
+ }
1307
+
1308
+ # low-level server will validate structured output against the tool's
1309
+ # output schema, and additionally serialize it into a TextContent block
1310
+ # for backwards compatibility with pre-2025-06-18 clients.
1311
+ return weather_data
1321
1312
else :
1322
1313
raise ValueError (f " Unknown tool: { name} " )
1323
1314
0 commit comments