> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Three primitives Part 2

> Explains MCP servers exposing resource templates and concrete resources using a shipment tracker example

Welcome back — in this lesson we'll focus on resource templates in an MCP (Model Context Protocol) server. We'll connect to the `shipment-tracker` server, inspect the messages the server exposes, and show how templates let a server represent large families of resources without enumerating every URI.

The typical call sequence starts with a client calling `server/discover`. Discovery is optional — clients can call it whenever helpful. Other inspector messages you may see include `subscriptions/list`, `notifications/subscriptions/acknowledge`, `tools/list`, `prompts/list`, and `resources/templates/list`.

![A web app dashboard for a "shipment-tracker" showing a connected server on the left and a protocol/messages panel with recent event logs and status cards on the right. The interface includes controls to add/export servers and filter or export messages.](https://mintcdn.com/kodekloud-c4ac6d9a/0UyWJzvxepsOrybu/images/Model-Context-Protocol-Associate-MCPA-Certification/Messages-Tools-and-Sessions/Demo-Three-primitives-Part-2/shipment-tracker-dashboard-server-messages-logs.jpg?fit=max\&auto=format\&n=0UyWJzvxepsOrybu\&q=85\&s=f253f03d1e2a0fa7a69cf9509739e430)

Resources exposed by a server are visible via `resources/list`. Below is an example response: it includes a server meta-block and a list of resources (here a single plain resource, `fleet_summary`).

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 0,
  "result": {
    "_meta": {
      "io.modelcontextprotocol/serverInfo": {
        "name": "shipment-tracker",
        "version": "3.1.0"
      }
    },
    "cacheScope": "private",
    "resources": [
      {
        "description": "A one-line status count across every shipment this server knows.",
        "mimeType": "text/plain",
        "name": "fleet_summary",
        "uri": "shipment://fleet/summary"
      }
    ],
    "resultType": "complete",
    "ttlMs": 0
  }
}
```

resource templates vs concrete resources
When you request `resources/templates/list` the server returns templates rather than all concrete URIs. A template is a URI pattern containing placeholders (RFC 6570-style `{...}`) that the server can fill in on demand to produce a resource for a specific identifier.

Example `resources/templates/list` response showing one manifest template:

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 3,
  "result": {
    "_meta": {
      "io.modelcontextprotocol/serverInfo": {
        "name": "shipment-tracker",
        "version": "3.1.0"
      }
    },
    "cacheScope": "private",
    "resourceTemplates": [
      {
        "description": "The carrier manifest for one shipment",
        "mimeType": "text/plain",
        "name": "shipment_manifest",
        "uriTemplate": "shipment://{ref}/manifest"
      }
    ],
    "resultType": "complete",
    "ttlMs": 0
  }
}
```

A template such as `shipment://{ref}/manifest` (placeholder `{ref}`) represents a family of manifest URIs — one per shipment reference. The client requests a concrete resource by calling `resources/read` with a filled-in URI, for example `shipment://PL-4417/manifest`, and the server composes and returns that specific manifest.

Example: a generic fleet summary (plain text)

```text theme={null}
Fleet summary
delivered: 1
held_at_customs: 1
in_transit: 1
```

Example: a composed manifest for a single shipment (when reading `shipment://PL-4417/manifest`)

```text theme={null}
MANIFEST PL-4417
status          in_transit
current location Rotterdam distribution centre
estimated arrival 2026-09-14

journey
1. Shenzhen, collected
2. Singapore, transhipped
3. Suez, in convoy
4. Rotterdam distribution centre

Last updated: 03/11/2026, 10:02:25
```

Server configuration
A minimal `mcp.json` shows how the `shipment-tracker` server is configured for stdio-based invocation:

```json theme={null}
{
  "mcpServers": {
    "shipment-tracker": {
      "type": "stdio",
      "command": "../.venv/bin/python",
      "args": [
        "server.py"
      ],
      "protocolEra": "modern"
    }
  }
}
```

Server implementation (Python)
Below are the key Python snippets from `server.py`. The server defines a plain resource (`shipment://fleet/summary`) and a resource template (`shipment://{ref}/manifest`). Templates use RFC 6570-style curly braces and the parameter name in the function signature must match the placeholder name.

```python theme={null}
from typing import Optional

from mcp.server import MCPServer
from mcp.server.mcpserver.exceptions import ToolError

# Example domain data structures (simplified).
class Shipment:
    def __init__(self, ref: str, status: str, location: str, estimated_delivery: Optional[str] = None):
        self.ref = ref
        self.status = status
        self.location = location
        self.estimated_delivery = estimated_delivery

    def describe(self) -> str:
        return f"{self.ref}: {self.status} at {self.location}"

# In-memory example data (replace with a real datastore in production).
SHIPMENTS: dict[str, Shipment] = {
    "PL-4417": Shipment("PL-4417", "in_transit", "Rotterdam distribution centre", "2026-09-14"),
    # ... other shipments ...
}

JOURNEYS: dict[str, list[str]] = {
    "PL-4417": [
        "Shenzhen, collected",
        "Singapore, transhipped",
        "Suez, in convoy",
        "Rotterdam distribution centre",
    ],
}

def find_shipment(ref: str) -> Optional[Shipment]:
    return SHIPMENTS.get(ref.strip().upper())

server = MCPServer(name="shipment-tracker", version="3.1.0")


@server.resource("shipment://fleet/summary")
def fleet_summary() -> str:
    """A one-line status count across every shipment this server knows."""
    counts: dict[str, int] = {}
    for shipment in SHIPMENTS.values():
        counts[shipment.status] = counts.get(shipment.status, 0) + 1
    lines = [f"{status}: {n}" for status, n in sorted(counts.items())]
    return "Fleet summary\n" + "\n".join(lines)


@server.resource("shipment://{ref}/manifest")
def shipment_manifest(ref: str) -> str:
    """The carrier manifest for one shipment, as plain text."""
    key = ref.strip().upper()
    shipment = SHIPMENTS.get(key)

    if shipment is None:
        # ToolError indicates the requested resource cannot be produced.
        raise ToolError(f"No manifest for {ref}. Known: {', '.join(SHIPMENTS)}.")

    journey = JOURNEYS.get(key, [])
    body = [
        f"MANIFEST {shipment.ref}",
        f"status        {shipment.status}",
        f"current location {shipment.location}",
        f"estimated arrival {shipment.estimated_delivery or 'not committed'}",
        "",
        "journey",
    ]

    body += [f"  {n}. {place}" for n, place in enumerate(journey, 1)]
    return "\n".join(body)


if __name__ == "__main__":
    # Start or run the server. Replace with the appropriate server start method.
    # e.g. server.serve() or server.run() depending on the MCPServer implementation.
    pass
```

When to use plain resources vs templates

| Resource style | When to use | Example |
| - | - | - |
| Plain resource | Fixed, server-specific data at a known URI | `shipment://fleet/summary` |
| Resource template | Families of resources generated on demand for many identifiers | `shipment://{ref}/manifest` |

Resource reads must be side-effect free. If reading a URI changes state, it should be implemented as a tool, not a resource.

Best practices and notes

* Use resource templates to avoid enumerating thousands of concrete URIs; the server can advertise one template and synthesize artifacts on demand.
* Template placeholders use RFC 6570-style curly braces; ensure function parameter names match the placeholder names. See RFC 6570 for URI Template syntax: [https://www.rfc-editor.org/rfc/rfc6570](https://www.rfc-editor.org/rfc/rfc6570).
* Keep resource reads idempotent and side-effect free. If a read would mutate state or trigger workflows, implement it as a tool instead.

This completes the lesson on resource templates and how a simple shipment-tracker server exposes and implements both fixed resources and templated resources.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/model-context-protocol-associate-mcpa-certification/module/282d97e2-04f7-4beb-8301-695ecb3f3f99/lesson/01efce5b-ebec-4483-990b-aeb42ff1b227)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.