Skip to content

A Journey from AI to LLMs and MCP - 8 - Resources in MCP — Serving Relevant Data Securely to LLMs

Published: at 09:00 AM

Free Resources

In the previous post, we explored the architecture of the Model Context Protocol (MCP)—a flexible, standardized way to connect LLMs to tools, data, and workflows. One of MCP’s most powerful capabilities is its ability to expose resources to language models in a structured, secure, and controllable way.

In this post, we’ll dive into:

If you want to give LLMs real, relevant context from your systems—without compromising safety or control—resources are the foundation.

What Are Resources in MCP?

Resources represent data that a model or client can read.

This might include:

Each resource is identified by a URI, and can be read, discovered, and optionally subscribed to for updates.

Resource Discovery

Clients can ask a server to list available resources using: resources/list

The server responds with an array of structured metadata:

{
  "resources": [
    {
      "uri": "file:///logs/app.log",
      "name": "Application Logs",
      "description": "Recent server logs",
      "mimeType": "text/plain"
    }
  ]
}

Clients (or users) can browse these like a menu, selecting what context to send to the model.

Resource Templates

In addition to static lists, servers can expose URI templates using RFC 6570 syntax:

{
  "uriTemplate": "file:///logs/{date}.log",
  "name": "Log by Date",
  "description": "Access logs by date (e.g., 2024-04-01)",
  "mimeType": "text/plain"
}

This allows dynamic access to parameterized content—great for APIs, time-based logs, or file hierarchies.

Reading a Resource

To retrieve the content of a resource, clients use:

resources/read With a payload like:

{
  "uri": "file:///logs/app.log"
}

The server responds with the content in one of two formats:

Text Resource

{
  "contents": [
    {
      "uri": "file:///logs/app.log",
      "mimeType": "text/plain",
      "text": "Error: Timeout on request...\n"
    }
  ]
}

Binary Resource (e.g. image, PDF)

{
  "contents": [
    {
      "uri": "screen://localhost/display1",
      "mimeType": "image/png",
      "blob": "iVBORw0KGgoAAAANSUhEUgAAA..."
    }
  ]
}

Clients can choose how and when to inject these into the model’s prompt, depending on MIME type and length.

Real-Time Updates

Resources aren’t static—they can change. MCP supports subscriptions to keep context fresh.

List Updates

If the list of resources changes, the server can notify the client with:

notifications/resources/list_changed

Useful when new logs, files, or endpoints become available.

Content Updates

Clients can subscribe to specific resource URIs:

resources/subscribe

When the resource changes, the server sends:

notifications/resources/updated

This is ideal for live logs, dashboards, or real-time documents.

Security Best Practices

Exposing resources to models requires careful control. MCP includes flexible patterns for securing access:

Best Practices for Server Developers

Example: Safe Log Server

server.setRequestHandler(ListResourcesRequestSchema, async () => {
  return {
    resources: [
      {
        uri: "file:///logs/app.log",
        name: "App Logs",
        mimeType: "text/plain"
      }
    ]
  };
});

server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
  const uri = request.params.uri;

  if (!uri.startsWith("file:///logs/")) {
    throw new Error("Access denied");
  }

  const content = await readFile(uri); // Add sanitization here
  return {
    contents: [{
      uri,
      mimeType: "text/plain",
      text: content
    }]
  };
});

Why Resources Matter for AI Agents

LLMs are context-hungry. They reason better when they have:

By serving these as resources, MCP gives agents the data they need—on demand, with full user control, and without bloating prompt templates.

Recap: Resources at a Glance

Coming Up Next: Tools in MCP — Giving LLMs the Power to Act

So far, we’ve shown how MCP feeds models with data. But what if we want the model to take action?

In the next post, we’ll explore tools in MCP: