This example demonstrates MCP resource support for providing context data to agents. Resources allow MCP servers to expose data that agents can read and use as context for their responses.
MCP resources are named, URI-addressable pieces of data that can be read by agents. Unlike prompts (which generate instructions) or tools (which perform actions), resources provide static or dynamic context data that agents can reference.
Common use cases:
- Configuration files and settings
- API documentation
- System metrics and logs
- Knowledge base articles
- Templates and examples
uv run python examples/mcp/resource_server/main.pyThe example server provides four resources:
config://app/settings- Application configurationdocs://api/overview- API documentationdata://metrics/summary- System metricsdocs://security/guidelines- Security best practices
Each resource is defined using the @mcp.resource() decorator:
@mcp.resource("config://app/settings")
def get_app_settings() -> str:
"""Application configuration settings"""
return """# Application Settings
...
"""The client demonstrates several ways to use resources:
- List available resources using
list_resources() - Read resource content using
read_resource(uri) - Provide resources as context to agents
Example usage:
# List resources
resources_result = await server.list_resources()
for resource in resources_result.resources:
print(f"Resource: {resource.name} - {resource.uri}")
# Read a specific resource
resource_result = await server.read_resource("config://app/settings")
content = resource_result.contents[0].text
# Use resource content in agent instructions
agent = Agent(
name="Config Assistant",
instructions=f"You are a helpful assistant. Configuration: {content}",
mcp_servers=[server],
)- Configuration Assistant - Answers questions about app configuration
- API Documentation Assistant - Helps users understand the API
- Security Reviewer - Reviews code using security guidelines
- Metrics Analyzer - Analyzes system performance metrics
Resources can use any URI scheme. Common patterns:
file://- File system resourceshttp://orhttps://- Web resourcesconfig://- Configuration resourcesdocs://- Documentation resourcesdata://- Data resources- Custom schemes for domain-specific resources
Try modifying the example to:
- Add new resources (e.g., error codes, examples, schemas)
- Use different URI schemes
- Provide dynamic content (e.g., current timestamp, system status)
- Combine multiple resources in agent context
- Implement resource templates for parameterized content