Overview
The Prompt APIs provide powerful tools for constructing conversation messages with rich MCP content including text, images, and resources. These APIs simplify working with multi-modal content in agent conversations.Prompt Class
Creating Messages
ThePrompt class provides static methods to create messages with various content types.
Prompt.user()
Create a user message with various content types.Content items to include in the message. Accepts:
- Strings (converted to TextContent)
- Image file paths (converted to ImageContent)
- Other file paths (converted to EmbeddedResource)
- ContentBlock objects
- ResourceContents objects
- ReadResourceResult objects
- PromptMessage or PromptMessageExtended objects
User message with provided content
Prompt.assistant()
Create an assistant message with optional tool calls and stop reason.Content items to include in the message
Reason the assistant stopped generating (e.g., “end_turn”, “max_tokens”)
Tool calls made by the assistant
Message phase (e.g., “thinking”, “responding”)
Assistant message with provided content and metadata
Prompt.message()
Create a message with explicit role specification.Content items to include in the message
Role for the message
Message with specified role and content
Prompt.conversation()
Create a conversation from multiple messages.Messages to include in the conversation. Accepts:
- PromptMessageExtended objects
- PromptMessage objects
- Message dictionaries with ‘role’ and ‘content’ keys
- Lists of messages
List of prompt messages forming a conversation
Loading Prompts
load_prompt()
Load a prompt from a file with full conversation state.Path to the prompt file. Supports:
.jsonfiles: Full serialization format- Other files: Template-based format with resource loading
List of messages with full conversation state
JSON Format
JSON files preserve complete conversation state including tool calls, stop reasons, and custom channels:Template Format
Template files use delimiters for role sections and support resource loading:@include are automatically loaded and embedded.
History Management
load_history_into_agent()
Load conversation history directly into an agent without triggering LLM calls.Agent instance to restore history into
Path to saved history file (JSON or template format)
Optional notice string if usage state cannot be fully restored
The agent’s history is cleared before loading. Provider diagnostic history will be updated on the next API call.
Content Helpers
Utility functions for working with MCP content.get_text()
Extract text from a content block.Content block to extract text from
Extracted text, or None if content has no text
get_image_data()
Extract image data (base64) from a content block.Content block to extract image from
Base64-encoded image data, or None if not an image
get_resource_uri()
Extract resource URI from an embedded resource.Content block to extract URI from
Resource URI, or None if not an embedded resource
Type Guards
Type checking functions for content blocks:Resource Links
Create resource links for external content.resource_link()
Create a generic resource link with automatic MIME type inference.URL to the resource
Resource name (defaults to filename from URL)
MIME type (inferred from extension if not provided)
Description of the resource
Resource link object
image_link()
Create a resource link for an image URL.URL to the image
Image name (defaults to filename)
MIME type (defaults to image/jpeg)
Description of the image
Resource link with image MIME type
video_link()
Create a resource link for a video URL.URL to the video (supports YouTube URLs)
Video name (defaults to filename)
MIME type (defaults to video/mp4)
Description of the video
Resource link with video MIME type
audio_link()
Create a resource link for an audio URL.URL to the audio file
Audio name (defaults to filename)
MIME type (defaults to audio/mpeg)
Description of the audio
Resource link with audio MIME type
Message Search
Search and filter messages in conversations.search_messages()
Search messages by role or content.Messages to search through
Filter by message role
Search pattern for message content
Messages matching the search criteria
extract_first() / extract_last()
Extract first or last message from a list.Messages to search
Filter by message role
First or last matching message, or None if not found
Example: Complete Prompt Workflow
Best Practices
Template files with
@include directives automatically resolve resources relative to the template file location. This makes it easy to bundle prompts with their referenced files.Related
MCP Client
Connect to MCP servers
Resources
Work with MCP resources
