Resources

Learn how MCP provides AI models with access to data through resources

Resources in MCP

Resources are one of the core features in MCP that allow servers to share data with AI models. Think of resources as files or documents that the AI model can read to get information.

Resources are designed to be application-controlled, meaning that the client application (like Claude Desktop) decides how and when they should be used. This gives users control over what data is shared with the AI.

What Are Resources?

Resources in MCP can represent many different types of data:

  • Text files on your computer
  • Database records
  • API responses
  • Screenshots or images
  • Log files
  • Any other data your MCP server can access

How Resources Work

Resources follow a simple lifecycle:

  1. Discovery: The client asks the server what resources are available
  2. Selection: The user or application chooses which resources to use
  3. Reading: The client requests the content of specific resources
  4. Usage: The AI model uses the resource content in its context
  5. Updates: The server can notify the client when resources change

Resource URIs

Each resource is identified by a unique URI (Uniform Resource Identifier). This follows a familiar format similar to web URLs:

[protocol]://[host]/[path]

For example:

  • file:///home/user/documents/report.pdf
  • database://project/customers/schema
  • github://repo/issues/open

These URIs help identify what kind of resource it is and where it's located.

Types of Resources

MCP resources can contain two types of content:

Text Resources

Text resources contain UTF-8 encoded text data, which is perfect for:

  • Source code
  • Configuration files
  • Log files
  • JSON/XML data
  • Plain text documents

Binary Resources

Binary resources contain data encoded in base64, suitable for:

  • Images
  • PDFs
  • Audio files
  • Video files
  • Other non-text formats

Resource Example

Here's what a resource looks like in MCP:

{
  "uri": "file:///user/documents/report.pdf",
  "name": "Quarterly Report",
  "description": "Q1 2025 Financial Report",
  "mimeType": "application/pdf"
}

And here's what reading a resource might return:

{
  "contents": [
    {
      "uri": "file:///user/documents/report.pdf",
      "mimeType": "application/pdf",
      "blob": "JVBERi0xLjMKJcTl8uXrp/Ln..." // Base64 encoded data
    }
  ]
}

Dynamic Resources with Templates

For resources that change based on inputs (like search results), MCP supports URI templates:

{
  "uriTemplate": "search://results?query={query}&limit={limit}",
  "name": "Search Results",
  "description": "Find relevant documents based on query"
}

These templates follow RFC 6570 and let clients construct valid URIs by filling in the parameters.

Resource Updates

MCP provides two mechanisms for real-time updates to resources:

  1. List Changes: Servers can notify clients when the list of available resources changes
  2. Content Changes: Clients can subscribe to specific resources to be notified when their content changes

This is useful for resources that update frequently, like log files or live data feeds.

Example Implementation

import { Server } from "@modelcontextprotocol/sdk/server";
 
// Create a server with resources capability
const server = new Server({
  name: "example-server",
  version: "1.0.0"
}, {
  capabilities: {
    resources: {}
  }
});
 
// List available resources
server.setRequestHandler("resources/list", async () => {
  return {
    resources: [
      {
        uri: "file:///logs/app.log",
        name: "Application Logs",
        description: "Recent system log entries",
        mimeType: "text/plain"
      }
    ]
  };
});
 
// Read resource contents
server.setRequestHandler("resources/read", async (request) => {
  const uri = request.params.uri;
  
  if (uri === "file:///logs/app.log") {
    // In a real implementation, you would read from the actual log file
    const logContents = "2025-04-26 10:15:30 INFO Server started\n2025-04-26 10:15:35 INFO User connected";
    
    return {
      contents: [{
        uri,
        mimeType: "text/plain",
        text: logContents
      }]
    };
  }
  
  throw new Error("Resource not found");
});

Best Practices

When implementing resources in your MCP server:

  1. Use Clear Names: Give resources descriptive names and URI schemes
  2. Include Metadata: Add descriptions and MIME types when known
  3. Handle Errors Gracefully: Return clear error messages when resources aren't found
  4. Consider Performance: For large resources, support pagination or chunking
  5. Implement Security: Validate URIs and implement access controls
  6. Document Your URI Schemes: Make it clear how your custom URIs work
  7. Update Notifications: Use notifications for frequently changing resources

Security Considerations

When exposing resources through MCP:

  • Validate All URIs: Prevent directory traversal and other security issues
  • Implement Access Controls: Ensure users can only access authorized resources
  • Sanitize File Paths: Be careful with user-supplied paths
  • Encrypt Sensitive Data: Protect confidential information
  • Audit Access: Log resource access for security monitoring
  • Rate Limit Requests: Prevent abuse and resource exhaustion

Remember that resources contain data that will be sent to AI models. Only expose resources that you're comfortable sharing, and be mindful of privacy and security implications.

How is this guide?

On this page