What are Resources?
Resources are contextual data sources in MCP that AI models can read to gather information. Unlike tools (which perform actions) and prompts (which generate text), resources provide static or dynamic data like configuration, user profiles, or application state. In xmcp, resources use file-system routing where the file path determines the resource URI.File-System Routing
Resources are automatically registered based on their location in thesrc/resources/ directory:
Routing Rules
Static Routes
Static Routes
Files without dynamic segments create static resource URIs:
src/resources/(config)/app.ts→resource://app-configsrc/resources/(data)/settings.ts→resource://settings
Dynamic Routes
Dynamic Routes
Files with
[param] segments create parameterized resources:src/resources/(users)/[userId]/profile.ts→resource://user-profile/{userId}src/resources/(posts)/[postId]/comments.ts→resource://comments/{postId}
Route Groups
Route Groups
Directories wrapped in parentheses
(name) are omitted from the URI:(config)groups configuration resources but doesn’t appear in the URI(users)organizes user-related resources without affecting the URI
Resource Structure
Every resource consists of two main exports:1. Metadata Export
Themetadata export defines the resource’s identity:
ResourceMetadata Type
TheResourceMetadata interface from ~/workspace/source/packages/xmcp/src/types/resource.ts:3-15 includes:
Unique identifier for the resource (must match the URI pattern)
Human-readable title for the resource
Description of what the resource provides
MIME type of the resource content (e.g.,
"application/json", "text/plain", "text/html+skybridge")Size of the resource in bytes
Metadata for the resource. Supports nested OpenAI metadata and other vendor extensions:
2. Default Export (Handler)
The default export is the function that returns the resource data:Real Examples
Static Resource: App Config
From~/workspace/source/examples/http-transport/src/resources/(config)/app.ts:1-12:
src/resources/(config)/app.tsResource URI:
resource://app-config
Dynamic Resource: User Profile
From~/workspace/source/examples/stdio-transport/src/resources/(users)/[userId]/profile.ts:1-17:
src/resources/(users)/[userId]/profile.tsResource URI:
resource://user-profile/{userId}
Dynamic resources use a
schema export (just like tools and prompts) to define the parameters extracted from the URI.HTML Resource: Widget
From~/workspace/source/examples/open-ai/src/resources/(ui)/widget/pizza-map.ts:1-16:
Creating Resources with CLI
Use the xmcp CLI to quickly scaffold a new resource:src/resources/my-resource.ts with the basic structure:
Resource Patterns
Configuration Resources
Store application settings and configuration:User Data Resources
Provide user-specific data:Documentation Resources
Provide contextual documentation:Return Values
Resources can return:- Strings - Plain text or JSON strings
- Objects - Automatically serialized to JSON
- Structured content - MCP content format:
Best Practices
Use Descriptive Names
Choose clear names that describe the resource’s content:
user-profile, app-config, api-docs.Set MIME Types
Specify
mimeType for non-text resources:"application/json"for JSON data"text/markdown"for documentation"text/html+skybridge"for OpenAI widgets
Organize with Route Groups
Use parentheses to group related resources:
(config)/for configuration(users)/for user data(docs)/for documentation
Handle Async Data
Resources can be async to fetch data from databases or APIs:
Validate Dynamic Parameters
Use Zod schemas to validate URI parameters:
Resources vs Tools vs Prompts
Resources
Provide dataRead-only contextual information that models can reference.Example: User profiles, configuration, documentation
Tools
Perform actionsExecutable functions that models can call to accomplish tasks.Example: Create user, send email, fetch data
Prompts
Generate textReusable templates that help models generate consistent responses.Example: Code review, documentation generation
Next Steps
Building Tools
Learn how to create executable tools
Building Widgets
Create React components for OpenAI-compatible UIs