@@ -338,6 +338,8 @@ to the `@tool` decorator.
338
338
``` python
339
339
""" Example showing structured output with tools."""
340
340
341
+ from typing import TypedDict
342
+
341
343
from pydantic import BaseModel, Field
342
344
343
345
from mcp.server.fastmcp import FastMCP
@@ -365,12 +367,8 @@ def get_weather(city: str) -> WeatherData:
365
367
condition = " sunny" ,
366
368
wind_speed = 5.2 ,
367
369
)
368
- ```
369
370
370
- _ Full example: [ examples/snippets/servers/structured_output.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/structured_output.py ) _
371
- <!-- /snippet-source -->
372
371
373
- ``` python
374
372
# Using TypedDict for simpler structures
375
373
class LocationInfo (TypedDict ):
376
374
latitude: float
@@ -437,6 +435,9 @@ def get_temperature(city: str) -> float:
437
435
# Returns: {"result": 22.5}
438
436
```
439
437
438
+ _ Full example: [ examples/snippets/servers/structured_output.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/structured_output.py ) _
439
+ <!-- /snippet-source -->
440
+
440
441
### Prompts
441
442
442
443
Prompts are reusable templates that help LLMs interact with your server effectively:
@@ -814,21 +815,46 @@ uv run mcp install server.py -f .env
814
815
815
816
For advanced scenarios like custom deployments:
816
817
818
+ <!-- snippet-source examples/snippets/servers/direct_execution.py -->
817
819
``` python
820
+ """ Example showing direct execution of an MCP server.
821
+
822
+ This is the simplest way to run an MCP server directly.
823
+ cd to the `examples/snippets` directory and run:
824
+ uv run direct-execution-server
825
+ or
826
+ python servers/direct_execution.py
827
+ """
828
+
818
829
from mcp.server.fastmcp import FastMCP
819
830
820
831
mcp = FastMCP(" My App" )
821
832
822
- if __name__ == " __main__" :
833
+
834
+ @mcp.tool ()
835
+ def hello (name : str = " World" ) -> str :
836
+ """ Say hello to someone."""
837
+ return f " Hello, { name} ! "
838
+
839
+
840
+ def main ():
841
+ """ Entry point for the direct execution server."""
823
842
mcp.run()
843
+
844
+
845
+ if __name__ == " __main__" :
846
+ main()
824
847
```
825
848
849
+ _ Full example: [ examples/snippets/servers/direct_execution.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/direct_execution.py ) _
850
+ <!-- /snippet-source -->
851
+
826
852
Run it with:
827
853
828
854
``` bash
829
- python server .py
855
+ python servers/direct_execution .py
830
856
# or
831
- uv run mcp run server .py
857
+ uv run mcp run servers/direct_execution .py
832
858
```
833
859
834
860
Note that ` uv run mcp run ` or ` uv run mcp dev ` only supports server using FastMCP and not the low-level server variant.
@@ -1277,9 +1303,30 @@ async def main():
1277
1303
1278
1304
When building MCP clients, the SDK provides utilities to help display human-readable names for tools, resources, and prompts:
1279
1305
1306
+ <!-- snippet-source examples/snippets/clients/display_utilities.py -->
1280
1307
``` python
1308
+ """ Client display utilities example.
1309
+
1310
+ This example shows how to use the SDK's display utilities to show
1311
+ human-readable names for tools, resources, and prompts.
1312
+
1313
+ cd to the `examples/snippets` directory and run:
1314
+ uv run display-utilities-client
1315
+ """
1316
+
1317
+ import asyncio
1318
+ import os
1319
+
1320
+ from mcp import ClientSession, StdioServerParameters
1321
+ from mcp.client.stdio import stdio_client
1281
1322
from mcp.shared.metadata_utils import get_display_name
1282
- from mcp.client.session import ClientSession
1323
+
1324
+ # Create server parameters for stdio connection
1325
+ server_params = StdioServerParameters(
1326
+ command = " uv" , # Using uv to run the server
1327
+ args = [" run" , " server" , " fastmcp_quickstart" , " stdio" ],
1328
+ env = {" UV_INDEX" : os.environ.get(" UV_INDEX" , " " )},
1329
+ )
1283
1330
1284
1331
1285
1332
async def display_tools (session : ClientSession):
@@ -1301,8 +1348,39 @@ async def display_resources(session: ClientSession):
1301
1348
for resource in resources_response.resources:
1302
1349
display_name = get_display_name(resource)
1303
1350
print (f " Resource: { display_name} ( { resource.uri} ) " )
1351
+
1352
+ templates_response = await session.list_resource_templates()
1353
+ for template in templates_response.resourceTemplates:
1354
+ display_name = get_display_name(template)
1355
+ print (f " Resource Template: { display_name} " )
1356
+
1357
+
1358
+ async def run ():
1359
+ """ Run the display utilities example."""
1360
+ async with stdio_client(server_params) as (read, write):
1361
+ async with ClientSession(read, write) as session:
1362
+ # Initialize the connection
1363
+ await session.initialize()
1364
+
1365
+ print (" === Available Tools ===" )
1366
+ await display_tools(session)
1367
+
1368
+ print (" \n === Available Resources ===" )
1369
+ await display_resources(session)
1370
+
1371
+
1372
+ def main ():
1373
+ """ Entry point for the display utilities client."""
1374
+ asyncio.run(run())
1375
+
1376
+
1377
+ if __name__ == " __main__" :
1378
+ main()
1304
1379
```
1305
1380
1381
+ _ Full example: [ examples/snippets/clients/display_utilities.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/clients/display_utilities.py ) _
1382
+ <!-- /snippet-source -->
1383
+
1306
1384
The ` get_display_name() ` function implements the proper precedence rules for displaying names:
1307
1385
1308
1386
- For tools: ` title ` > ` annotations.title ` > ` name `
0 commit comments