Skip to content
mentoor

Topic 3 of 5 · 5 min read

Exposing resources, resource templates and prompts

Goal: Decide which server capabilities belong in resources, templates or prompts and write a resource template for a parameterised lookup.

Use a tool when the model should decide to act, a resource when the application should load context, and a prompt when the user should pick a ready-made workflow. MCP has three server features, and each has a different controller.

Check yourself · 1 of 3

Who normally decides to use an MCP prompt?

Choose one answer

Why it matters

If everything is a tool, the model has to find and call things that are really just files or records, and the tool list grows until the model picks badly. If a risky action is hidden inside a resource, a client may fetch it without anyone deciding to run it.

The three features

  1. Resources are read-only data identified by a URI, such as file:///reports/q3.md or orders://ORD-1042. The client or user chooses what to attach to the conversation. A resource has a name, an optional MIME type and content as text or base64.
  2. Resource templates describe a family of resources with a URI template, such as orders://{orderId}. The client fills in the variable. Use one when there are too many items to list individually.
  3. Prompts are reusable message templates with named arguments, for example review-pr with a url argument. Clients usually show them as slash commands that the user triggers. The server returns messages to put into the conversation.

How to decide

  • Does it change something or call a system with side effects? Tool.
  • Is it data the app might attach as context, readable without side effects? Resource, or a template for many items.
  • Is it a workflow the user starts on purpose? Prompt.
  • A tool can also return a resource link in its result, which points at data without pasting all of it into the model's context. Use it for large results.

Design the URI

Pick a stable scheme and readable path. Put identifiers in the path, not in a query that changes the meaning. Never build a URI that lets a caller escape your intended root, such as files://{path} with no check for ../.

Before and after

# Weak: every order becomes a tool call the model must discover
@mcp.tool()
def read_order_file(order_id: str) -> str: ...

# Better: read-only data as a template resource
@mcp.resource("orders://{order_id}")
def order(order_id: str) -> str:
    """Order record as JSON."""
    return json.dumps(lookup(order_id))

What goes wrong

  • Resources with side effects. Reads may happen automatically, so they must be safe to repeat.
  • Path traversal in templates. Validate every variable the way you would validate user input.
  • Listing ten thousand resources. Use a template and let the client supply the id.
  • Prompts that hide instructions. A prompt the user triggers should show what it will send.

Examples

Template resource and a prompt

server.registerResource(
  "order",
  new ResourceTemplate("orders://{orderId}", { list: undefined }),
  { title: "Order record", mimeType: "application/json" },
  async (uri, { orderId }) => ({
    contents: [{ uri: uri.href, text: JSON.stringify(await db.find(String(orderId))) }],
  })
);

server.registerPrompt(
  "summarise-order",
  { description: "Summarise one order for a support agent", argsSchema: { orderId: z.string() } },
  ({ orderId }) => ({
    messages: [{ role: "user", content: { type: "text", text: `Summarise order ${orderId} in three lines.` } }],
  })
);

A safe file template

A docs server exposes docs://{page}. The handler resolves the page against a fixed docs/ directory, rejects any value containing .. or a leading slash, and returns a not-found error for everything else. Without that check, docs://..%2F..%2Fetc%2Fpasswd reads files the server was never meant to share.

Key points

  • Tools are model-controlled actions, resources are application-chosen context and prompts are user-chosen workflows.
  • A resource template with a URI such as orders://{orderId} covers a large family of items without listing each one.
  • Resource reads must be safe to repeat, because a client may fetch them without an explicit decision to act.
  • A tool can return a resource link instead of the full data, which keeps large results out of the model's context.
  • Validate every template variable as untrusted input, especially when it maps to a file path.

Common mistakes

  • Turning every read-only lookup into a tool and letting the tool list grow past what a model can choose from.
  • Putting actions with side effects behind a resource that clients may read automatically.
  • Passing template variables straight into file paths or queries without validation.
  • Returning the full contents of a large file in a tool result instead of a resource link.

Check yourself · 2 of 3

A server holds 50,000 customer records the client may attach one at a time. What should it expose?

Choose one answer

Check yourself · 3 of 3

A template docs://{page} reads files under docs/. What must the handler do with the page value?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic