Lesson 3 of 9 · Concepts
Tools, Resources and Prompts
What an MCP server offers: tools the model calls, resources the app reads and prompts users pick, with their JSON messages, results, errors and annotations.
- 5 min read
- Intermediate
- Reviewed
- Explain tools, resources and prompts, and who controls each
- Read the messages for listing and using each feature
- Return structured results and errors from tools
- Use tool annotations, and know why they are only hints
Three features, three controllers
The clearest way to tell the features apart is to ask who decides when they're used:
| Feature | Controlled by | Typical use | Methods |
|---|---|---|---|
| Tools | The model | Actions and lookups during a task | tools/list, tools/call |
| Resources | The application | Data loaded as context: files, records, schemas | resources/list, resources/templates/list, resources/read |
| Prompts | The user | Reusable instructions picked from a menu or slash command | prompts/list, prompts/get |
The messages, from a tiny server
This program plays the server's part for each feature, returning results in the same shapes as real MCP servers, including an error for a method it doesn't know:
import json
NOTES = {"packing": "Umbrella, adapter, tram card.", "trip-ideas": "Lisbon in May. Book trams early."}
# ---- What this tiny server offers ----
TOOLS = [{ # model-controlled: the AI decides when to call them
"name": "search_notes",
"description": "Search all notes for a word. Returns the names of matching notes.",
"inputSchema": {"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]},
}]
RESOURCE_TEMPLATES = [{ # application-controlled: the app decides what to load as context
"uriTemplate": "notes://{name}", "name": "read_note", "mimeType": "text/markdown",
}]
PROMPTS = [{ # user-controlled: the user picks them, e.g. from a menu
"name": "summarise_note", "description": "Summarise a note in three bullet points.",
"arguments": [{"name": "name", "required": True}],
}]
def handle(request):
"""Answer one JSON-RPC request, in the shapes real MCP servers use."""
method, params = request["method"], request.get("params", {})
if method == "tools/list":
result = {"tools": TOOLS}
elif method == "tools/call":
q = params["arguments"]["query"].lower()
found = [n for n, text in NOTES.items() if q in text.lower()]
result = {"content": [{"type": "text", "text": ", ".join(found) or "No matches"}],
"structuredContent": {"result": found}, "isError": False}
elif method == "resources/read":
name = params["uri"].removeprefix("notes://")
result = {"contents": [{"uri": params["uri"], "mimeType": "text/markdown", "text": NOTES[name]}]}
elif method == "prompts/get":
name = params["arguments"]["name"]
result = {"messages": [{"role": "user", "content": {"type": "text",
"text": f"Read the note notes://{name} and summarise it in three short bullet points."}}]}
else:
return {"jsonrpc": "2.0", "id": request["id"], "error": {"code": -32601, "message": f"Method not found: {method}"}}
result["resultType"] = "complete"
return {"jsonrpc": "2.0", "id": request["id"], "result": result}
for req in [
{"jsonrpc": "2.0", "id": 1, "method": "tools/list"},
{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "search_notes", "arguments": {"query": "tram"}}},
{"jsonrpc": "2.0", "id": 3, "method": "resources/read", "params": {"uri": "notes://packing"}},
{"jsonrpc": "2.0", "id": 4, "method": "prompts/get", "params": {"name": "summarise_note", "arguments": {"name": "trip-ideas"}}},
{"jsonrpc": "2.0", "id": 5, "method": "notes/delete_all"},
]:
response = handle(req)
text = json.dumps(response.get("result") or response["error"])
print(req["method"], "->", text if len(text) <= 90 else text[:90] + " ...")A few details worth noticing:
tools/callreturns content (text, images or other blocks, for the model to read) and can also return structuredContent (JSON data that matches an output schema, for code to use).resources/readreturns contents, each with its URI and a MIME type.prompts/getreturns ready-made messages, which the host adds to the conversation.- Every result includes
resultType: "complete". (A server that needs more information first, such as an answer from the user, can instead return an “input required” result, and the client retries with the answer attached.)
Errors: two kinds
- Tool execution errors (bad input, a missing record, a failed API) are returned as a normal result with
isError: trueand a readable message, so the model can see it and correct itself. - Protocol errors (unknown method, malformed request) are JSON-RPC errors with a code, such as
-32601for “method not found”. They're for the client code, not the model.
Tool annotations
Servers can describe how a tool behaves with annotations, which hosts use to decide when to ask the user for approval:
| Annotation | Meaning |
|---|---|
readOnlyHint | The tool doesn't change anything |
destructiveHint | The tool may delete or overwrite things |
idempotentHint | Calling it twice has the same effect as once |
openWorldHint | The tool reaches outside systems, such as the web |
Common mistakes
Build the result a server returns when a tool fails in a way the model can fix.
def tool_error(message):
# Return an MCP tools/call result that reports a failure the model can read:
# content is a list with one text block, isError is True, resultType is "complete".
return {}
result = tool_error("No note called 'budget'. Use search_notes to find note names.")
print(result["isError"], result["resultType"])
print(result["content"][0]["type"], "|", result["content"][0]["text"])
In MCP, who usually decides when a prompt is used?
Summary
- Tools are model-controlled actions; resources are app-controlled data under URIs; prompts are user-controlled templates.
- Tool results carry content for the model and optional structuredContent for code.
- Tool failures are results with isError: true; protocol problems are JSON-RPC errors.
- Annotations help hosts decide on approvals but are only hints.
Next: how MCP messages travel, the transports.
Finished reading? Tick it off to track your progress.