Sub topic 14
Using resources and prompts from your host
This chapter assumes you have a working ClientSession and have listed tools, as in the earlier chapters. Tools are not the only thing a server can offer. Resources and prompts use the same session, but your host decides when to use them, not the model.
Who controls each primitive
| Primitive | Controlled by | Typical host behavior |
|---|---|---|
| Tool | The model | Sent to the LLM, which decides when to call it |
| Resource | The application | Your code reads it and adds it to the context |
| Prompt | The user | Shown as a menu item or slash command |
A resource is read-only data identified by a URI, such as a file or a record. A prompt is a reusable message template with named arguments.
A server that offers both
Add this to the server.py file from the earlier chapter, before the if __name__ block. The URI template notes://{name} makes a resource template, so one function serves many URIs.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("notes")
NOTES = {"todo": "Write the MCP chapter.", "ideas": "Try a second server."}
@mcp.resource("notes://{name}")
def read_note(name: str) -> str:
"""Return the text of one note."""
return NOTES.get(name, "No such note.")
@mcp.prompt()
def summarize(name: str) -> str:
"""Ask for a short summary of a note."""
return f"Summarize the note called '{name}' in one sentence."
if __name__ == "__main__":
mcp.run(transport="stdio")Reading and using them from the client
Resources with a fixed URI appear in list_resources. Templates appear in list_resource_templates. Resource content arrives as a list of items. Text items have a .text field, and binary items have a .blob field.
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from pydantic import AnyUrl
params = StdioServerParameters(command="python", args=["server.py"])
async def main():
async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
templates = await session.list_resource_templates()
print([t.uriTemplate for t in templates.resourceTemplates])
note = await session.read_resource(AnyUrl("notes://todo"))
note_text = note.contents[0].text
prompts = await session.list_prompts()
print([p.name for p in prompts.prompts])
prompt = await session.get_prompt("summarize", {"name": "todo"})
messages = [
{"role": m.role, "content": m.content.text}
for m in prompt.messages
if m.content.type == "text"
]
messages.append(
{"role": "user", "content": f"Note contents:\n{note_text}"}
)
print(messages)
asyncio.run(main())The messages list is ready for your LLM API call. The prompt supplies the instruction, and the resource supplies the data.
Common errors
- Passing resources to the model as tools. Resources are not tools. Your host must read them and insert the text.
- Assuming every server has them. Check the capabilities returned by
initializebefore callinglist_promptsorlist_resources. Servers that lack them return an error. - Trusting the content. Resource text and prompt text come from the server. Treat them as untrusted input, as with tool output.
Exercise
A server exposes a note through a resource and a summary template through a prompt. Which one should the user choose from a menu, and which one should your code fetch without asking the user?
Show answer
The user chooses the prompt, because prompts are user-controlled. Your application reads the resource with read_resource and adds its text to the messages, because resources are application-controlled.