Overview
The Resources APIs provide tools for working with MCP resources, including loading files, creating embedded resources, and managing resource content. These utilities make it easy to attach files, images, and other data to your agent conversations.
Resource Loading
load_resource_content()
Load a resource file and determine its MIME type.
from fast_agent.mcp.resource_utils import load_resource_content
from pathlib import Path
content, mime_type, is_binary = load_resource_content(
resource_path = "data.csv" ,
prompt_files = [Path( "prompt_template.txt" )]
)
if is_binary:
# content is base64-encoded
decoded = base64.b64decode(content)
else :
# content is plain text
print (content)
Path to the resource file (relative to prompt files)
List of prompt files used to resolve relative paths
File content (text string or base64-encoded for binary)
MIME type of the resource
True if content is binary (and base64-encoded)
Raises FileNotFoundError if the resource cannot be found relative to any of the prompt files.
Creating Resources
create_embedded_resource()
Create an embedded resource content object for messages.
from fast_agent.mcp.resource_utils import create_embedded_resource
resource = create_embedded_resource(
resource_path = "document.pdf" ,
content = base64_content,
mime_type = "application/pdf" ,
is_binary = True
)
# Use in message
msg = Prompt.user( "Review this document:" , resource)
Path identifier for the resource
Resource content (plain text or base64-encoded)
Whether content is binary (and base64-encoded)
MCP embedded resource object
create_text_resource()
Create an embedded resource for text data.
from fast_agent.mcp.resource_utils import create_text_resource
resource = create_text_resource(
resource_path = "file:///data/config.json" ,
content = json_string,
mime_type = "application/json"
)
URI or path for the resource
Text content of the resource
MIME type (e.g., “text/plain”, “application/json”)
Embedded resource with text content
create_blob_resource()
Create an embedded resource for binary data.
from fast_agent.mcp.resource_utils import create_blob_resource
import base64
# Read binary file
with open ( "image.png" , "rb" ) as f:
binary_data = f.read()
base64_data = base64.b64encode(binary_data).decode( "utf-8" )
resource = create_blob_resource(
resource_path = "file:///images/image.png" ,
content = base64_data,
mime_type = "image/png"
)
URI or path for the resource
Base64-encoded binary content
MIME type (e.g., “image/png”, “application/pdf”)
Embedded resource with binary content
create_image_content()
Create an image content object from base64 data.
from fast_agent.mcp.resource_utils import create_image_content
import base64
# Load and encode image
with open ( "photo.jpg" , "rb" ) as f:
image_data = base64.b64encode(f.read()).decode( "utf-8" )
image = create_image_content(
data = image_data,
mime_type = "image/jpeg"
)
msg = Prompt.user( "What's in this image?" , image)
Base64-encoded image data
Image MIME type (e.g., “image/jpeg”, “image/png”)
Resource References
create_resource_reference()
Create a reference to a resource without embedding its content.
from fast_agent.mcp.resource_utils import create_resource_reference
# Reference external resource
ref = create_resource_reference(
uri = "https://example.com/large-file.pdf" ,
mime_type = "application/pdf"
)
msg = Prompt.user( "Summarize this document:" , ref)
URI for the resource (will be fetched separately)
MIME type of the referenced resource
Resource reference (client will fetch content via URI)
Resource references create an EmbeddedResource that points to the URI. The MCP client will make a separate request to fetch the resource content when needed.
URI Utilities
normalize_uri()
Normalize a URI or filename to ensure it’s a valid URI.
from fast_agent.mcp.resource_utils import normalize_uri
# Convert filename to file:// URI
uri = normalize_uri( "document.pdf" )
# Result: "file:///document.pdf"
# Handle absolute paths
uri = normalize_uri( "/home/user/file.txt" )
# Result: "file:///home/user/file.txt"
# Pass through existing URIs
uri = normalize_uri( "https://example.com/file.pdf" )
# Result: "https://example.com/file.pdf"
URI string or simple filename
Properly formatted URI string
create_resource_uri()
Create a resource URI from a file path.
from fast_agent.mcp.resource_utils import create_resource_uri
uri = create_resource_uri( "data/config.json" )
# Result: resource://fast-agent/config.json
File path to convert to resource URI
Resource URI for the path
Extract a readable title from a URI.
from fast_agent.mcp.resource_utils import extract_title_from_uri
from pydantic import AnyUrl
uri = AnyUrl( "https://example.com/docs/guide.pdf" )
title = extract_title_from_uri(uri)
# Result: "guide.pdf"
uri = AnyUrl( "file:///home/user/report.docx" )
title = extract_title_from_uri(uri)
# Result: "report.docx"
URI to extract title from
Readable title (typically the filename)
MIME Type Detection
guess_mime_type()
Guess MIME type from file path or URL.
from fast_agent.mcp.mime_utils import guess_mime_type
mime = guess_mime_type( "document.pdf" )
# Result: "application/pdf"
mime = guess_mime_type( "image.png" )
# Result: "image/png"
mime = guess_mime_type( "data.json" )
# Result: "application/json"
File path or URL to analyze
Guessed MIME type (defaults to “application/octet-stream”)
is_image_mime_type()
Check if a MIME type represents an image.
from fast_agent.mcp.mime_utils import is_image_mime_type
if is_image_mime_type( "image/jpeg" ):
# Handle as image
pass
True if MIME type is an image type
is_binary_content()
Check if a MIME type represents binary content.
from fast_agent.mcp.mime_utils import is_binary_content
if is_binary_content( "application/pdf" ):
# Read as binary and base64 encode
pass
else :
# Read as text
pass
True if content should be treated as binary
Reading MCP Resources
get_resource_text()
Extract text from a ReadResourceResult.
from fast_agent.mcp import get_resource_text
# Read resource from MCP server
result = await session.read_resource( uri = "file:///data.txt" )
# Extract text content
text = get_resource_text(result, index = 0 )
if text:
print (text)
result
ReadResourceResult
required
Result from reading an MCP resource
Index of content to extract (if multiple contents)
Extracted text content, or None if not text
Raises IndexError if index is out of bounds for the contents list.
Resource Markers
parse_resource_marker()
Parse resource marker text into an embedded resource.
from fast_agent.mcp.resource_utils import parse_resource_marker
# Parse marker text from message
marker_text = """[Resource: file.json, MIME: application/json]
{"key": "value"}"""
resource = parse_resource_marker(marker_text)
if resource:
# Use the embedded resource
pass
Marker text to parse (format: [Resource: path, MIME: type]\ncontent)
Parsed embedded resource, or None if not a valid marker
Example: Complete Resource Workflow
from fast_agent import Prompt
from fast_agent.mcp.resource_utils import (
load_resource_content,
create_embedded_resource,
create_image_content
)
from pathlib import Path
import base64
# Load a text resource
text_content, mime, is_binary = load_resource_content(
resource_path = "config.json" ,
prompt_files = [Path( "prompt.txt" )]
)
text_resource = create_embedded_resource(
resource_path = "config.json" ,
content = text_content,
mime_type = mime,
is_binary = False
)
# Load an image
with open ( "diagram.png" , "rb" ) as f:
image_data = base64.b64encode(f.read()).decode( "utf-8" )
image = create_image_content(
data = image_data,
mime_type = "image/png"
)
# Create message with both resources
message = Prompt.user(
"Analyze this configuration and diagram:" ,
text_resource,
image
)
# Use in agent
response = await agent.run(message)
Example: Resource References for Large Files
from fast_agent import Prompt
from fast_agent.mcp.resource_utils import create_resource_reference
# Reference large external files without embedding
file_ref = create_resource_reference(
uri = "https://cdn.example.com/large-dataset.csv" ,
mime_type = "text/csv"
)
video_ref = create_resource_reference(
uri = "https://example.com/training-video.mp4" ,
mime_type = "video/mp4"
)
# Client will fetch these separately as needed
message = Prompt.user(
"Analyze this dataset and watch the training video:" ,
file_ref,
video_ref
)
Example: Reading from MCP Servers
from fast_agent.mcp.gen_client import connect
from fast_agent.mcp import get_resource_text
# Connect to MCP server
session = await connect( "filesystem" , registry)
# Read resource
result = await session.read_resource(
uri = "file:///workspace/README.md"
)
# Extract text
readme_text = get_resource_text(result)
if readme_text:
# Use in agent
message = Prompt.user(
f "Here's the README: \n\n { readme_text } \n\n Summarize it."
)
response = await agent.run(message)
Best Practices
Use create_resource_reference() for large files to avoid embedding the entire content in messages. The MCP client will fetch the content only when needed.
Always base64-encode binary content before passing it to create_blob_resource() or create_image_content(). Use Python’s base64.b64encode() for this.
When using load_resource_content(), the function automatically determines if content is binary based on MIME type and handles encoding appropriately.
Common MIME Types
File Type MIME Type Text text/plainJSON application/jsonPDF application/pdfJPEG Image image/jpegPNG Image image/pngMP4 Video video/mp4MP3 Audio audio/mpegCSV text/csvHTML text/htmlMarkdown text/markdown
MCP Client Connect to MCP servers
Prompts Create conversation messages