@@ -105,6 +105,7 @@ _Full example: [examples/snippets/servers/basic_resource.py](https://github.com/
105105
106106Resources with URI parameters (e.g., ` {name} ` ) are registered as templates. When a client reads a templated resource, the URI parameters are extracted and passed to the function:
107107
108+ <!-- snippet-source examples/snippets/servers/resource_templates.py -->
108109``` python
109110from mcp.server.fastmcp import FastMCP
110111
@@ -117,6 +118,9 @@ def get_user_profile(user_id: str) -> str:
117118 return f ' {{ "user_id": " { user_id} ", "name": "User { user_id} " }} '
118119```
119120
121+ _ Full example: [ examples/snippets/servers/resource_templates.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/resource_templates.py ) _
122+ <!-- /snippet-source -->
123+
120124Clients read a template resource by providing a concrete URI:
121125
122126``` python
@@ -137,6 +141,7 @@ def get_readme(owner: str, repo: str) -> str:
137141
138142Resources can return binary data by returning ` bytes ` instead of ` str ` . Set the ` mime_type ` to indicate the content type:
139143
144+ <!-- snippet-source examples/snippets/servers/binary_resources.py -->
140145``` python
141146from mcp.server.fastmcp import FastMCP
142147
@@ -150,14 +155,17 @@ def get_logo() -> bytes:
150155 return f.read()
151156```
152157
158+ _ Full example: [ examples/snippets/servers/binary_resources.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/binary_resources.py ) _
159+ <!-- /snippet-source -->
160+
153161Binary content is automatically base64-encoded and returned as ` BlobResourceContents ` in the MCP response.
154162
155163#### Resource Subscriptions
156164
157165Clients can subscribe to resource updates. Use the low-level server API to handle subscription and unsubscription requests:
158166
167+ <!-- snippet-source examples/snippets/servers/resource_subscriptions.py -->
159168``` python
160- import mcp.types as types
161169from mcp.server.lowlevel import Server
162170
163171server = Server(" Subscription Example" )
@@ -178,6 +186,9 @@ async def handle_unsubscribe(uri) -> None:
178186 subscriptions[str (uri)].discard(" current_session" )
179187```
180188
189+ _ Full example: [ examples/snippets/servers/resource_subscriptions.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/resource_subscriptions.py ) _
190+ <!-- /snippet-source -->
191+
181192When a subscribed resource changes, notify clients with ` send_resource_updated() ` :
182193
183194``` python
@@ -468,6 +479,7 @@ _Full example: [examples/snippets/servers/basic_prompt.py](https://github.com/mo
468479
469480Prompts can include embedded resources to provide file contents or data alongside the conversation messages:
470481
482+ <!-- snippet-source examples/snippets/servers/prompt_embedded_resources.py -->
471483``` python
472484import mcp.types as types
473485from mcp.server.fastmcp import FastMCP
@@ -497,10 +509,14 @@ def review_file(filename: str) -> list[base.Message]:
497509 ]
498510```
499511
512+ _ Full example: [ examples/snippets/servers/prompt_embedded_resources.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/prompt_embedded_resources.py ) _
513+ <!-- /snippet-source -->
514+
500515#### Prompts with Image Content
501516
502517Prompts can include images using ` ImageContent ` or the ` Image ` helper class:
503518
519+ <!-- snippet-source examples/snippets/servers/prompt_image_content.py -->
504520``` python
505521import mcp.types as types
506522from mcp.server.fastmcp import FastMCP
@@ -524,10 +540,14 @@ def describe_image(image_path: str) -> list[base.Message]:
524540 ]
525541```
526542
543+ _ Full example: [ examples/snippets/servers/prompt_image_content.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/prompt_image_content.py ) _
544+ <!-- /snippet-source -->
545+
527546#### Prompt Change Notifications
528547
529548When your server dynamically adds or removes prompts, notify connected clients:
530549
550+ <!-- snippet-source examples/snippets/servers/prompt_change_notifications.py -->
531551``` python
532552from mcp.server.fastmcp import Context, FastMCP
533553from mcp.server.session import ServerSession
@@ -543,6 +563,9 @@ async def update_prompts(ctx: Context[ServerSession, None]) -> str:
543563 return " Prompts updated"
544564```
545565
566+ _ Full example: [ examples/snippets/servers/prompt_change_notifications.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/prompt_change_notifications.py ) _
567+ <!-- /snippet-source -->
568+
546569### Icons
547570
548571MCP servers can provide icons for UI display. Icons can be added to the server implementation, tools, resources, and prompts:
@@ -608,6 +631,7 @@ _Full example: [examples/snippets/servers/images.py](https://github.com/modelcon
608631
609632FastMCP provides an ` Audio ` class for returning audio data from tools, similar to ` Image ` :
610633
634+ <!-- snippet-source examples/snippets/servers/audio_example.py -->
611635``` python
612636from mcp.server.fastmcp import FastMCP
613637from mcp.server.fastmcp.utilities.types import Audio
@@ -627,12 +651,16 @@ def get_audio_from_bytes(raw_audio: bytes) -> Audio:
627651 return Audio(data = raw_audio, format = " wav" )
628652```
629653
654+ _ Full example: [ examples/snippets/servers/audio_example.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/audio_example.py ) _
655+ <!-- /snippet-source -->
656+
630657The ` Audio ` class accepts ` path ` or ` data ` (mutually exclusive) and an optional ` format ` string. Supported formats include ` wav ` , ` mp3 ` , ` ogg ` , ` flac ` , ` aac ` , and ` m4a ` . When using a file path, the MIME type is inferred from the file extension.
631658
632659### Embedded Resource Results
633660
634661Tools can return ` EmbeddedResource ` to attach file contents or data inline in the result:
635662
663+ <!-- snippet-source examples/snippets/servers/embedded_resource_results.py -->
636664``` python
637665from mcp.server.fastmcp import FastMCP
638666from mcp.types import EmbeddedResource, TextResourceContents
@@ -655,13 +683,20 @@ def read_config(path: str) -> EmbeddedResource:
655683 )
656684```
657685
686+ _ Full example: [ examples/snippets/servers/embedded_resource_results.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/embedded_resource_results.py ) _
687+ <!-- /snippet-source -->
688+
658689For binary embedded resources, use ` BlobResourceContents ` with base64-encoded data:
659690
691+ <!-- snippet-source examples/snippets/servers/embedded_resource_results_binary.py -->
660692``` python
661693import base64
662694
695+ from mcp.server.fastmcp import FastMCP
663696from mcp.types import BlobResourceContents, EmbeddedResource
664697
698+ mcp = FastMCP(" Binary Embedded Resource Example" )
699+
665700
666701@mcp.tool ()
667702def read_binary_file (path : str ) -> EmbeddedResource:
@@ -678,10 +713,14 @@ def read_binary_file(path: str) -> EmbeddedResource:
678713 )
679714```
680715
716+ _ Full example: [ examples/snippets/servers/embedded_resource_results_binary.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/embedded_resource_results_binary.py ) _
717+ <!-- /snippet-source -->
718+
681719### Tool Change Notifications
682720
683721When your server dynamically adds or removes tools at runtime, notify connected clients so they can refresh their tool list:
684722
723+ <!-- snippet-source examples/snippets/servers/tool_change_notifications.py -->
685724``` python
686725from mcp.server.fastmcp import Context, FastMCP
687726from mcp.server.session import ServerSession
@@ -700,6 +739,9 @@ async def register_plugin(name: str, ctx: Context[ServerSession, None]) -> str:
700739 return f " Plugin ' { name} ' registered "
701740```
702741
742+ _ Full example: [ examples/snippets/servers/tool_change_notifications.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/tool_change_notifications.py ) _
743+ <!-- /snippet-source -->
744+
703745### Context
704746
705747The Context object is automatically injected into tool and resource functions that request it via type hints. It provides access to MCP capabilities like logging, progress reporting, resource reading, user interaction, and request metadata.
@@ -979,6 +1021,7 @@ The `elicit()` method returns an `ElicitationResult` with:
9791021
9801022To present a dropdown or selection list in elicitation forms, use ` json_schema_extra ` with an ` enum ` key on a ` str ` field. Do not use ` Literal ` -- use a plain ` str ` field with the enum constraint in the JSON schema:
9811023
1024+ <!-- snippet-source examples/snippets/servers/elicitation_enum.py -->
9821025``` python
9831026from pydantic import BaseModel, Field
9841027
@@ -1007,10 +1050,14 @@ async def pick_color(ctx: Context[ServerSession, None]) -> str:
10071050 return " No color selected"
10081051```
10091052
1053+ _ Full example: [ examples/snippets/servers/elicitation_enum.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/elicitation_enum.py ) _
1054+ <!-- /snippet-source -->
1055+
10101056#### Elicitation Complete Notification
10111057
10121058For URL mode elicitations, send a completion notification after the out-of-band interaction finishes. This tells the client that the elicitation is done and it may retry any blocked requests:
10131059
1060+ <!-- snippet-source examples/snippets/servers/elicitation_complete.py -->
10141061``` python
10151062from mcp.server.fastmcp import Context, FastMCP
10161063from mcp.server.session import ServerSession
@@ -1019,9 +1066,7 @@ mcp = FastMCP("Elicit Complete Example")
10191066
10201067
10211068@mcp.tool ()
1022- async def handle_oauth_callback (
1023- elicitation_id : str , ctx : Context[ServerSession, None ]
1024- ) -> str :
1069+ async def handle_oauth_callback (elicitation_id : str , ctx : Context[ServerSession, None ]) -> str :
10251070 """ Called when OAuth flow completes out-of-band."""
10261071 # ... process the callback ...
10271072
@@ -1031,6 +1076,9 @@ async def handle_oauth_callback(
10311076 return " Authorization complete"
10321077```
10331078
1079+ _ Full example: [ examples/snippets/servers/elicitation_complete.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/elicitation_complete.py ) _
1080+ <!-- /snippet-source -->
1081+
10341082### Sampling
10351083
10361084Tools can interact with LLMs through sampling (generating text):
@@ -1102,6 +1150,7 @@ _Full example: [examples/snippets/servers/notifications.py](https://github.com/m
11021150
11031151Clients can request a minimum logging level via ` logging/setLevel ` . Use the low-level server API to handle this:
11041152
1153+ <!-- snippet-source examples/snippets/servers/set_logging_level.py -->
11051154``` python
11061155import mcp.types as types
11071156from mcp.server.lowlevel import Server
@@ -1118,6 +1167,9 @@ async def handle_set_level(level: types.LoggingLevel) -> None:
11181167 current_level = level
11191168```
11201169
1170+ _ Full example: [ examples/snippets/servers/set_logging_level.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/set_logging_level.py ) _
1171+ <!-- /snippet-source -->
1172+
11211173When this handler is registered, the server automatically declares the ` logging ` capability during initialization.
11221174
11231175### Authentication
0 commit comments