Sub topic 3
Tools, resources and prompts
This chapter covers the three primitives an MCP server can offer: tools, resources and prompts. It assumes you are comfortable with async Python, JSON Schema, and the idea of LLM tool calling. The roles of host, client and server are covered in the previous chapter. Here the focus is on what each primitive is, who decides when it is used, and how to pick the right one.
Three primitives, three control models
An MCP server describes what it offers in terms of three primitives. They differ mainly in who decides when the item is used.
| Primitive | What it is | Who controls it | Typical host behavior |
|---|---|---|---|
| Tool | A function the model can ask to run | The model, usually with user approval | Passed to the LLM as a tool definition |
| Resource | Read-only data identified by a URI | The application | Fetched by the host and added to context |
| Prompt | A reusable message template with arguments | The user | Shown as a command or menu item |
The control model matters for design. A tool can have side effects, so the host must decide how much to trust the model's choice to call it. A resource is data the host chooses to load, so the model never triggers it directly unless the host offers its own tool for that. A prompt is something a person picks, such as a "summarize this file" command.
Tools
A tool is a named operation with a description and an input schema written in JSON Schema. The model reads the name, description and schema, then decides whether to call it and with what arguments. The client sends a tools/call request, and the server returns a result.
The server in this tutorial exposes one tool. The SDK builds the schema from the type hints and the description from the docstring.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calc")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two integers."""
return a + b
if __name__ == "__main__":
mcp.run(transport="stdio")Save this as server.py. The generated definition for add has the name add, the description Add two integers., and an inputSchema requiring integer properties a and b.
What a tool result contains
A tool result is a list of content blocks, such as text, images or embedded resources. It also carries an isError flag. When a tool fails, the server should set isError to true and describe the problem in the content, instead of failing the whole protocol request. The model can then read the message and try again or explain the failure. Later chapters cover this in detail. For now, remember that a failed tool call and a failed protocol request are different things.
Tool descriptions are prompts
The model chooses tools based on text. A vague description such as "does stuff" leads to wrong or missed calls. Good descriptions say what the tool does, what its arguments mean, and when not to use it. Treat description text as part of your application's prompt, and keep it accurate.
Resources
A resource is data that a server exposes under a URI, for example file:///project/README.md or db://customers/schema. Resources are meant for reading. Each has a URI, a name, and usually a MIME type that tells the host how to interpret the content.
Servers expose resources in two ways:
- Direct resources have a fixed URI and appear in the
resources/listresponse. - Resource templates use a URI pattern with parameters, such as
logs://{date}, so one definition covers many items.
The host calls resources/read with a URI and receives the contents as text or binary data. The host then decides what to do with them. It might attach a file to the conversation, show a picker to the user, or index the contents for search.
A server can also support subscriptions, where the client asks to be notified when a resource changes. This is optional and negotiated during initialization.
Here is a resource added to the same server:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calc")
@mcp.resource("calc://history/{n}")
def history(n: int) -> str:
"""Return the n-th entry in the calculation history."""
return f"entry {n}: (no history recorded yet)"
if __name__ == "__main__":
mcp.run(transport="stdio")The {n} in the URI makes this a template. The SDK passes the matched value to the function.
Prompts
A prompt is a named template that produces one or more messages. It can take arguments. The host lists prompts with prompts/list and fetches one with prompts/get, passing argument values. The server returns messages with roles, ready to be placed in a conversation.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calc")
@mcp.prompt()
def explain_sum(a: int, b: int) -> str:
"""Ask the model to explain how a sum is computed."""
return f"Explain step by step how to add {a} and {b}."
if __name__ == "__main__":
mcp.run(transport="stdio")Prompts are intended for user-driven workflows. A chat interface might show them as slash commands. Your application is free to decide how to present them. The key point is that a person, not the model, usually chooses to run one.
Choosing the right primitive
Use these questions when designing a server, or when deciding which of its features your host should use.
- Does the operation do something, or change state? Make it a tool.
- Is it data the application should load into context, such as a document? Make it a resource.
- Is it a reusable instruction that a person triggers? Make it a prompt.
The boundaries are not strict. A server can offer a "read file" tool and also list the same files as resources. The tool lets the model pull data when it decides it needs it. The resource lets the host or user attach data up front. Many servers expose only tools, and many hosts support only tools. That is why the rest of this tutorial concentrates on tools first.
Common mistakes
- Using a tool for static reference data, which makes the model spend a call and a round trip to get it.
- Using a resource for an action with side effects. Reads should be safe to repeat.
- Assuming every server supports all three primitives. A server declares its capabilities during initialization, and your host should check them before calling
resources/listorprompts/list. - Writing a prompt that hides a long instruction the user never sees. Hosts should show users what a prompt will send.
What the host needs to do for each primitive
Supporting each primitive costs a different amount of host code.
- Tools need the most: list them, convert them to the model's tool format, run the loop, handle errors, and ask for approval when needed.
- Resources need a listing and reading step, plus a decision about where the content goes and how large it can be.
- Prompts need a listing and fetching step, plus a user interface to choose and fill in arguments.
Exercise
A server offers a function that deletes a temporary file, a read-only file listing under a URI, and a "write release notes" template that a person chooses. Which primitive fits each, and who normally triggers each one?
Show answer
The delete function is a tool, triggered by the model, ideally with user approval because it has side effects. The file listing is a resource, loaded by the host application. The release notes template is a prompt, chosen by the user.
Exercise
Why should a host check server capabilities before calling prompts/list?
Show answer
A server only supports the primitives it declares during initialization. Calling a method for an undeclared capability can return an error, so the host should read the declared capabilities first and skip unsupported primitives.