Learn › MCP: Model Context Protocol — Complete Developer Guide › Resources: Giving the Model Context

Resources: Giving the Model Context

Intermediate 🕐 25 min Lesson 5 of 11
What you'll learn
  • Explain the difference between application-controlled resources and model-controlled tools
  • Describe how URI templates extend resources to cover dynamically addressed content
  • Identify the three resource annotation fields and what each signals to the host

What Resources Are

Resources are contextual data that a server exposes for the model to read. Unlike tools, resources do not perform actions — they provide information. A filesystem server might expose the contents of local files as resources. A database server might expose its schema. A news server might expose the day's top stories. The model reads resources to understand context; it calls tools to take action.

The key distinction from tools is control. Tools are model-controlled — the LLM decides when to call them. Resources are application-controlled — the host application decides when to include them in the model's context. The host might show the user a file picker and include the selected files as resources. It might automatically include a project's README whenever the model is working in that project's directory. Or it might use a heuristic to identify relevant resources based on what the user is asking about.

This design gives the application control over context window management. Including too many resources inflates the context and raises cost; including too few leaves the model without the information it needs. The host is in the best position to make this tradeoff, because it knows the user's intent and the available context budget.

Identifying Resources with URIs

Every resource is identified by a URI conforming to RFC 3986. The URI is both the identifier and the address — servers use it to locate the resource, and clients use it to request it.

MCP defines conventions for common URI schemes:

  • file:///home/user/notes.md — a file on the local filesystem. The path after file:// is an absolute filesystem path.
  • https://api.example.com/data — only used when the client can fetch the content itself directly from that URL. If the server fetches it on behalf of the client, a custom scheme is more appropriate.
  • git://repo/main/src/index.ts — a file at a specific Git reference. This scheme is conventional for Git integration but not formally standardized.
  • Custom schemes — any scheme you invent (db://schemas/users, jira://tickets/open) is valid as long as it follows RFC 3986 syntax. Custom schemes make it clear that the client cannot resolve the URI independently; it must ask the server to read it.

When designing URIs for your server, prefer schemes that make the resource's origin obvious. A URI of file:///Users/jab/projects/api/schema.graphql is immediately understandable; a URI of resource://1234 is not.

Listing Resources

Clients discover a server's resources by calling resources/list. The response is a paginated array of resource objects. Each resource in the list includes:

  • uri (required) — the resource's unique identifier
  • name (required) — a short human-readable label
  • title (optional) — a longer display name for richer UIs
  • description (optional) — explains what the resource contains and when to include it
  • mimeType (optional) — the content type, e.g. text/plain, application/json, image/png
  • size (optional) — approximate byte size, useful for context budget decisions
  • annotations (optional) — audience and priority hints for the host

Like tools, resources should be listed in a deterministic order. If a server declares "resources": {"listChanged": true}, it may send a notifications/resources/list_changed notification when the list changes, and clients should re-fetch.

URI Templates

Not all resources can be enumerated in advance. A filesystem server cannot list every possible file path. A database server cannot pre-generate a URI for every possible query. For these cases, MCP supports URI templates conforming to RFC 6570.

Clients discover URI templates via resources/templates/list. Each template has the same metadata fields as a regular resource, plus a uriTemplate string with placeholders in curly braces:

{
  "uriTemplate": "file:///{path}",
  "name": "Local File",
  "description": "Any file on the local filesystem by absolute path"
}

The host resolves a template by substituting values for the placeholders. It can also offer auto-completion for template arguments, making it easier for users to discover and specify resources. For example, a file browser UI might show a list of paths that complete the {path} placeholder as the user types.

Reading a Resource

To read a resource, the client sends a resources/read request with the resource's URI:

{
  "jsonrpc": "2.0",
  "id": "7",
  "method": "resources/read",
  "params": { "uri": "file:///Users/jab/projects/api/schema.graphql" }
}

The server responds with a contents array. Each element is a content block with a uri field (which may differ from the requested URI, for example if the server resolved a symlink) and either a text field or a blob field (base64-encoded binary content):

{
  "contents": [
    {
      "uri": "file:///Users/jab/projects/api/schema.graphql",
      "mimeType": "text/plain",
      "text": "type Query { users: [User] }
type User { id: ID name: String }"
    }
  ]
}

A single read can return multiple content blocks. A directory read might return one block per file. A database schema read might return one block per table. This allows servers to represent composite resources naturally.

If a resource does not exist, the server must return a -32602 error — not an empty contents array. An empty array is ambiguous (the resource exists but is empty vs. the resource does not exist); an error code is unambiguous.

Subscriptions and Change Notifications

For resources that change over time — a live log file, a database view, a monitored API endpoint — MCP supports subscriptions. If a server declares "resources": {"subscribe": true}, clients can subscribe to specific resources and receive notifications when they change.

Subscriptions are set up through subscriptions/listen with the resource URIs the client cares about. When a subscribed resource changes, the server sends a notifications/resources/updated notification. The client should then re-fetch the resource to get the latest content — the notification contains no content itself.

The spec says delivery is "best effort." Clients should not rely on notifications for correctness — they should poll periodically for freshness in addition to listening for notifications. Notifications are an optimization (avoid unnecessary polls) rather than a guarantee.

Annotations

Resource annotations give the host hints about how to present and prioritize resources. Three annotation fields are defined:

  • audience — an array of "user" and/or "assistant". A resource annotated ["user"] is meant for the user to see; ["assistant"] is meant for the model; both means either. This lets the host decide where to surface the resource in its UI.
  • priority — a float from 0.0 (least important) to 1.0 (most important). The host uses this when it needs to trim context to fit within a budget. Higher-priority resources are dropped last.
  • lastModified — an ISO 8601 timestamp indicating when the resource was last changed. The host can use this to prefer fresher resources when multiple options are available.

Like tool annotations, resource annotations are advisory. They are written by the server author and reflect their intent, not an enforced contract.

Security

Resources carry significant security responsibilities, particularly for filesystem and database servers.

  • Path traversal: Always validate that a requested URI resolves within an expected root. A client that passes file:///../../../etc/passwd as a URI is attempting a path traversal attack. Use an allowlist of permitted roots and reject any path that escapes it.
  • Access controls: Enforce authorization before reading. A multi-user server should check that the requesting user has permission to read the requested resource. Never expose all resources to all users by default.
  • Binary encoding: Correctly base64-encode binary content. Corrupted encoding will cause the client to receive malformed data silently.
  • Sensitive data: Be cautious about what you expose as resources. API keys, private keys, and personal data should not be readable as resources unless the server has strong authorization in front of them.
Key takeaways
  • Resources are application-controlled — the host decides when to include them, not the model
  • A single resources/read call can return multiple content blocks, making composite resources natural to model
  • Path traversal validation is mandatory for filesystem servers — always check that a URI resolves within a permitted root
Learn more every week Get 5 hand-picked prompts + new AI tools every Monday. Free.