Sub topic 3
Tools, resources and prompts
This chapter assumes you know what an MCP server is and how host, client and server relate (the previous chapter). You should also be comfortable reading JSON and basic Python. Here you will learn the three primitives a server can offer, who controls each one, and how to choose among them when you design a capability.
Why there are three primitives
An MCP server does not just expose "functions". It can offer three kinds of things, and each kind answers a different question: who decides when it is used?
| Primitive | What it is | Who decides to use it | Typical effect |
|---|---|---|---|
| Tool | A function the model can call with arguments | The model (the host usually asks the user to approve) | Runs code, may change things |
| Resource | Read-only data identified by a URI | The application or the user | Adds content to the context |
| Prompt | A reusable message template with parameters | The user, often through a slash command or menu | Starts a structured conversation |
This split is called control. Tools are model-controlled, resources are application-controlled, and prompts are user-controlled. The control model matters for safety and for design. If the model can trigger something on its own, that thing needs a clear description and a narrow effect.
In practice, assistants differ in how much of this they support. Almost every coding assistant supports tools. Support for resources and prompts is less even, so check your assistant's documentation before you depend on them.
Tools
A tool is a named operation with an input schema. The model sees the name, a description and a JSON Schema for the arguments. When it decides the tool helps, it asks the host to call it.
A client discovers tools with the tools/list request. Each entry looks like this:
{
"name": "add",
"description": "Add two integers.",
"inputSchema": {
"type": "object",
"properties": {
"a": { "type": "integer" },
"b": { "type": "integer" }
},
"required": ["a", "b"]
}
}To run it, the client sends tools/call:
{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 2, "b": 3}}}The server answers with a result that contains a list of content items, usually text, and a flag that says whether the call failed:
{"jsonrpc": "2.0", "id": 1, "result": {"content": [{"type": "text", "text": "5"}], "isError": false}}Writing a tool in Python
The official Python SDK includes a helper called FastMCP. It builds the schema from your function signature.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calculator")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two integers."""
return a + b
if __name__ == "__main__":
mcp.run() # stdio transport by defaultThe function name becomes the tool name. The type hints become the input schema. The docstring becomes the description the model reads. Because the model chooses tools by reading that description, a vague docstring leads to wrong or missed calls.
Two kinds of failure
Tool errors come in two forms, and they behave differently.
- A protocol error is a JSON-RPC error, for example an unknown tool name or arguments that do not match the schema. It means the request itself was wrong.
- A tool execution error is a normal result with
"isError": trueand an explanation in the content. It means the tool ran but the operation failed, such as a file that does not exist.
Return execution errors as results with a clear message. The model can read that message and try again with different arguments. A raw stack trace is less useful than "File not found: notes.txt. Use list_files to see what exists."
Annotations and side effects
Tools may carry optional hints, such as whether a tool only reads data or may delete things. Hosts can use these hints to decide what to auto-approve. Treat them as hints only. A client should not trust the hints from a server it does not trust, because the server writes them.
Resources
A resource is data the server can provide, identified by a URI. Examples are a file, a database schema, or the current build log. Unlike a tool, reading a resource is not supposed to change anything and does not take arguments the model invents.
A client lists resources with resources/list and reads one with resources/read:
{"jsonrpc": "2.0", "id": 2, "method": "resources/read", "params": {"uri": "file:///project/README.md"}}The response contains the content, along with a MIME type so the host knows how to handle it:
{"jsonrpc": "2.0", "id": 2, "result": {"contents": [{"uri": "file:///project/README.md", "mimeType": "text/markdown", "text": "# Project\n"}]}}Resource templates
Listing every possible item is not always practical. A server can instead publish a resource template, a URI with placeholders such as logs://{date}. The client fills in the placeholder to read a specific item. In FastMCP, a function decorated with a templated URI does this:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("notes")
NOTES = {"todo": "Write the chapter.", "ideas": "Try a resource template."}
@mcp.resource("notes://{name}")
def read_note(name: str) -> str:
"""Return the text of one note."""
return NOTES.get(name, "No such note.")
if __name__ == "__main__":
mcp.run()Subscriptions
If the server declares support, a client can subscribe to a resource. The server then sends a notifications/resources/updated message when the content changes. This suits things like a log file. Many servers skip it, so do not assume it exists.
Prompts
A prompt is a template for a message or a short conversation. It has a name, optional arguments, and produces messages that the host puts into the chat. Think of it as a saved, parameterized instruction that the server author wrote well once.
The client lists prompts with prompts/list and expands one with prompts/get:
{"jsonrpc": "2.0", "id": 3, "method": "prompts/get", "params": {"name": "review_code", "arguments": {"language": "python"}}}The result holds a description and a list of messages:
{"jsonrpc": "2.0", "id": 3, "result": {"description": "Review code", "messages": [{"role": "user", "content": {"type": "text", "text": "Review the following python code for bugs and style."}}]}}Many hosts show prompts as slash commands. The user picks one, fills in the arguments, and the expanded messages are sent. The model does not choose prompts by itself, which makes prompts a good place for workflows that the user wants to trigger on purpose.
A prompt can also embed a resource, so the template can pull in a file or a schema as part of the messages it returns.
Choosing the right primitive
When you design a capability, ask three questions in order.
- Does it change something, or does it need arguments the model works out? Use a tool.
- Is it passive data that a person or the app might want to attach as context? Use a resource.
- Is it a repeatable workflow the user starts on purpose? Use a prompt.
Some capabilities fit more than one. Reading a file could be a tool (read_file) or a resource (file:///...). If the assistant only supports tools, the tool version is the only one that works. If it supports resources, the resource version lets the user attach a file without the model spending a turn to fetch it. Many servers offer both for this reason.
Common mistakes
- Exposing a write action as a resource. Resources should be safe to read. Anything with side effects belongs in a tool, where the host can ask for approval.
- Writing one huge tool with a
modeargument. The model handles several small, clearly named tools better than one that does everything. - Returning very large output. Everything a tool returns goes into the model's context. Trim, paginate, or return a summary with a way to fetch more.
- Vague descriptions. "Does stuff with files" gives the model nothing to decide on.
Capabilities and discovery
A server does not have to offer all three primitives. During initialization it declares which ones it supports, for example tools, resources and prompts, and the client only sends the matching requests. A server may also declare listChanged. Then it sends a notification such as notifications/tools/list_changed when its set of tools changes, and the client should call tools/list again.
If your assistant shows no resources or prompts from a server, the likely causes are that the server does not offer them or that the host does not implement them. Neither is a bug in your configuration.
Exercise
You are building a server for a project's database. You want (a) a way for the model to run a read-only query it writes itself, (b) the current table schema that a user can attach to a chat, and (c) a "write a migration" workflow the user starts from a menu. Which primitive fits each, and why?
Show answer
(a) A tool, because the model decides when to run it and supplies the query as an argument. (b) A resource, because the schema is passive data that the app or user attaches as context. (c) A prompt, because it is a reusable workflow that the user triggers on purpose. The tool should also enforce read-only access itself, not rely only on its description.
Exercise
A tool returns an error because the requested file does not exist. Should the server send a JSON-RPC error or a normal result with isError set to true, and why?
Show answer
A normal result with isError set to true. The call itself was valid and the tool ran, so this is a tool execution error. A message in the result content lets the model read the problem and retry. JSON-RPC errors are for malformed or unknown requests.