> ## 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

> Demonstration of application-controlled read-only resources in a shipment-tracker server, showing registration, exposure, and inspection via the MCP Inspector with a fleet summary example.

Welcome back — in this lesson we focus on application-controlled resources in the shipment-tracker server and how they are registered, exposed, and inspected with the MCP Inspector.

We will:

* Start the server (stdio mode for the demo).
* Observe the MCP handshake and the `resources/list` exchange in the Inspector.
* Examine how a resource is declared in the server code and what it returns.

Start the server (stdio is used for this demo):

```bash theme={null}
../.venv/bin/python server.py
```

When the server starts you will see the MCP handshake and initialization sequence in the Inspector. The client requests `resources/list`; the server responds with a resource entry describing a fleet summary resource.

The Inspector displays the resource entry with a short description and a MIME type of `text/plain`. The resource is exposed under a URI like `shipment://fleet/summary`. The server stores a text representation for that URI which the client can read on demand.

Example content returned by the resource (as shown by the Inspector):

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

What do resources actually do?

* Resources are application-controlled, read-only artifacts. The host (your application) decides what to read and when to provide their contents to a model.
* The client asks the server for a list of available resources. The client then fetches a resource and supplies its contents to the model when relevant to the task.
* Resource reads must be side-effect free. If reading something mutates state, it should be implemented as a tool (not a resource).

Resources are read-only, application-controlled artifacts. They provide contextual data to the model but are never automatically accessed by the model itself.

## Inspecting the registration and server configuration

The server registers itself with the MCP runtime using `mcp.json` (stdio example):

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

In `server.py` we register an application resource with a URI using `@server.resource(...)`. Below is the relevant fragment showing the resource registration and the server run logic:

```python theme={null}
# -------------------------------------------------------------------------------
# RESOURCES - application-controlled. The host decides what to read and when;
# the model never reaches for these on its own. Contrast with the tools above,
# which exist precisely so the model CAN reach for them.
#
# A resource read must be side-effect free. If reading it changes something,
# it is a tool wearing the wrong hat; treating a state-changing action as a resource is a mistake.
@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)


if __name__ == "__main__":
    if "--http" in sys.argv:
        # json_response=True returns a plain JSON body instead of an SSE stream,
        # which makes the response readable in curl and in the Inspector's
        # Network tab. SSE is the default and is what a real client negotiates.
        print("shipment-tracker starting on http://127.0.0.1:8000/mcp", file=sys.stderr)
        server.run("streamable-http", host="127.0.0.1", port=8000, json_response=True)
    else:
        print("shipment-tracker ready on stdio", file=sys.stderr)
        server.run("stdio")
```

Why this code matters

* The decorator `@server.resource("shipment://fleet/summary")` registers the resource URI with the server so clients can discover it via `resources/list`.
* `fleet_summary()` iterates over the `SHIPMENTS` fixture, tallies statuses, and returns a plain-text summary. This returned string is exactly what clients (and the Inspector) see when requesting the resource.
* Because the function returns a static/plain text summary (no side effects), it fits the resource model. Any version that mutates state should instead be a tool.

Quick resource summary (at-a-glance)

| Property | Description | Example |
| - | - | - |
| Resource URI | Unique identifier registered by the server | `shipment://fleet/summary` |
| MIME type | How the Inspector/client renders it | `text/plain` |
| Access pattern | How the model/client observes it | Client fetches on demand via `resources/read` |
| Semantics | Behavioral constraints | Read-only, side-effect free |
| Example content | Sample returned body | `Fleet summary\nin_transit: 1\n...` |

Notes about running the server and fetching the resource

* Default (stdio): run without flags to integrate with the MCP Inspector and demo client.
* HTTP mode: run with `--http` to serve MCP over HTTP. This sets `json_response=True` so curl and the Inspector's Network tab show a readable body rather than an SSE stream.

Example: fetch the resource over HTTP (when server started with `--http`):

```bash theme={null}
curl http://127.0.0.1:8000/mcp/resources/read?uri=shipment://fleet/summary
```

Protocol-era recap

* Historically, resources are explicitly application-controlled: the host decides when to fetch them and when to supply them to the model. The model does not autonomously fetch resources from the server — the client mediates access.

Next steps

* In later lessons we'll examine handlers and explore the `trace_journey` tool inserted into `server.py`. Tools are the mechanism the model CAN reach for (they are not passive, read-only artifacts).

Links and references

* [MCP overview and protocol concepts](https://example.com/mcp-docs)
* [SSE (Server-Sent Events) explanation](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events)
* [Python packaging and virtual environments](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/model-context-protocol-associate-mcpa-certification/module/282d97e2-04f7-4beb-8301-695ecb3f3f99/lesson/7876d4ab-0993-47d1-9ea4-14210c99ee25)


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