@@ -338,6 +338,8 @@ to the `@tool` decorator.
338338``` python
339339""" Example showing structured output with tools."""
340340
341+ from typing import TypedDict
342+
341343from pydantic import BaseModel, Field
342344
343345from mcp.server.fastmcp import FastMCP
@@ -365,12 +367,8 @@ def get_weather(city: str) -> WeatherData:
365367 condition = " sunny" ,
366368 wind_speed = 5.2 ,
367369 )
368- ```
369370
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 -->
372371
373- ``` python
374372# Using TypedDict for simpler structures
375373class LocationInfo (TypedDict ):
376374 latitude: float
@@ -437,6 +435,11 @@ def get_temperature(city: str) -> float:
437435 # Returns: {"result": 22.5}
438436```
439437
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+
441+
442+
440443### Prompts
441444
442445Prompts are reusable templates that help LLMs interact with your server effectively:
@@ -814,21 +817,46 @@ uv run mcp install server.py -f .env
814817
815818For advanced scenarios like custom deployments:
816819
820+ <!-- snippet-source examples/snippets/servers/direct_execution.py -->
817821``` python
822+ """ Example showing direct execution of an MCP server.
823+
824+ This is the simplest way to run an MCP server directly.
825+ cd to the `examples/snippets` directory and run:
826+ uv run direct-execution-server
827+ or
828+ python servers/direct_execution.py
829+ """
830+
818831from mcp.server.fastmcp import FastMCP
819832
820833mcp = FastMCP(" My App" )
821834
822- if __name__ == " __main__" :
835+
836+ @mcp.tool ()
837+ def hello (name : str = " World" ) -> str :
838+ """ Say hello to someone."""
839+ return f " Hello, { name} ! "
840+
841+
842+ def main ():
843+ """ Entry point for the direct execution server."""
823844 mcp.run()
845+
846+
847+ if __name__ == " __main__" :
848+ main()
824849```
825850
851+ _ Full example: [ examples/snippets/servers/direct_execution.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/direct_execution.py ) _
852+ <!-- /snippet-source -->
853+
826854Run it with:
827855
828856``` bash
829- python server .py
857+ python servers/direct_execution .py
830858# or
831- uv run mcp run server .py
859+ uv run mcp run servers/direct_execution .py
832860```
833861
834862Note that ` uv run mcp run ` or ` uv run mcp dev ` only supports server using FastMCP and not the low-level server variant.
@@ -1277,9 +1305,30 @@ async def main():
12771305
12781306When building MCP clients, the SDK provides utilities to help display human-readable names for tools, resources, and prompts:
12791307
1308+ <!-- snippet-source examples/snippets/clients/display_utilities.py -->
12801309``` python
1310+ """ Client display utilities example.
1311+
1312+ This example shows how to use the SDK's display utilities to show
1313+ human-readable names for tools, resources, and prompts.
1314+
1315+ cd to the `examples/snippets` directory and run:
1316+ uv run display-utilities-client
1317+ """
1318+
1319+ import asyncio
1320+ import os
1321+
1322+ from mcp import ClientSession, StdioServerParameters
1323+ from mcp.client.stdio import stdio_client
12811324from mcp.shared.metadata_utils import get_display_name
1282- from mcp.client.session import ClientSession
1325+
1326+ # Create server parameters for stdio connection
1327+ server_params = StdioServerParameters(
1328+ command = " uv" , # Using uv to run the server
1329+ args = [" run" , " server" , " fastmcp_quickstart" , " stdio" ],
1330+ env = {" UV_INDEX" : os.environ.get(" UV_INDEX" , " " )},
1331+ )
12831332
12841333
12851334async def display_tools (session : ClientSession):
@@ -1301,8 +1350,39 @@ async def display_resources(session: ClientSession):
13011350 for resource in resources_response.resources:
13021351 display_name = get_display_name(resource)
13031352 print (f " Resource: { display_name} ( { resource.uri} ) " )
1353+
1354+ templates_response = await session.list_resource_templates()
1355+ for template in templates_response.resourceTemplates:
1356+ display_name = get_display_name(template)
1357+ print (f " Resource Template: { display_name} " )
1358+
1359+
1360+ async def run ():
1361+ """ Run the display utilities example."""
1362+ async with stdio_client(server_params) as (read, write):
1363+ async with ClientSession(read, write) as session:
1364+ # Initialize the connection
1365+ await session.initialize()
1366+
1367+ print (" === Available Tools ===" )
1368+ await display_tools(session)
1369+
1370+ print (" \n === Available Resources ===" )
1371+ await display_resources(session)
1372+
1373+
1374+ def main ():
1375+ """ Entry point for the display utilities client."""
1376+ asyncio.run(run())
1377+
1378+
1379+ if __name__ == " __main__" :
1380+ main()
13041381```
13051382
1383+ _ Full example: [ examples/snippets/clients/display_utilities.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/clients/display_utilities.py ) _
1384+ <!-- /snippet-source -->
1385+
13061386The ` get_display_name() ` function implements the proper precedence rules for displaying names:
13071387
13081388- For tools: ` title ` > ` annotations.title ` > ` name `
0 commit comments