Skip to content
NestRS

MCP resources and prompts

The two capabilities beside tools — #[prompt] and #[resource], declared in the same #[tools] block and gated like a tool.

MCP defines two primitives beyond tools: prompts (parameterized templates the user picks) and resources (documents the model can list and read). Both are methods in the same #[tools] block as your tools, and each takes the same posture, guards and filters a tool does, its answer written inside the operation for the caller its site answers.

A prompt is a #[prompt] method beside your tools — one impl, both routers:

crates/features/src/posts/mcp/tool.rs
#[tools]
impl PostsTool {
#[tool(description = "List the post titles the caller may read, most recent first.")]
#[authorize(Read, PostEntity)]
async fn list_posts(&self) -> Result<String, ServiceError> { /* … */ }
#[prompt(description = "Draft a follow-up post, primed with the titles the caller can read.")]
#[authorize(Read, PostEntity)]
async fn draft_follow_up(&self) -> Result<GetPromptResult, ServiceError> { /* … */ }
}

A prompt’s messages are prose the body writes from the rows it read under the caller’s ability: the gate runs first, and what the prose says of a row is the body’s own line.

A resource is read at a URI. #[resource(template = "…")] declares the RFC 6570 template it is read at, and each of the template’s variables is an argument of the method, typed; #[resources] declares the method that lists what can be read:

crates/features/src/posts/mcp/tool.rs (from the demo, abridged)
#[tools]
impl PostsTool {
// …
#[resources]
#[authorize(Read, PostEntity)]
async fn posts(&self) -> Result<Vec<Resource>, ServiceError> {
let rows = self.svc.list().await?;
Ok(rows
.into_iter()
.map(|row| Resource::new(format!("post://{}", row.id), row.title))
.collect())
}
#[resource(
template = "post://{id}",
description = "One post the caller is allowed to read, as JSON."
)]
#[authorize(Read, bind = PostsService)]
async fn post(&self, post: Authorized<Read, PostEntity>) -> Result<Json<Post>, ServiceError> {
Ok(Json(Post::from(&post.into_inner())))
}
}

A client reads post://0192… and the framework does the rest: it finds the template the URI was written from, runs the operation’s guards and its class gate, loads the row id names through PostsService under the caller’s ability, calls post, writes the post judged for that caller and answers it as the contents of the URI it read. resources/templates/list advertises every template with its name (the method’s, unless you state one), its title and description, and the media type its answer is read as; resources/list is the #[resources] method’s answer. The resources capability is derived from the methods present, never written.

  • A template is read back, so it stays level 1. A variable is {name}, named like the parameter that takes it, and two never sit side by side. A value is what RFC 6570’s simple expansion writes — unreserved characters and percent-encoded octets — so post://{id} never reads post://a/b; it is percent-decoded, then parsed with FromStr, and it ends at the first occurrence of the literal after it. An operator or a modifier ({+path}, {id:3}), a variable no parameter takes or a parameter no variable names does not compile.
  • What it answers is read by its type. Json<T> is read as application/json, a String as text/plain, and a ReadResourceResult goes out as you built it, a blob included. Wrap any of them in Option: None is a resource the caller may not read.
  • A resource it cannot read is one answer. A URI no template writes, a variable that does not parse and a None are all MCP’s Resource not found — -32002, or -32602 from protocol 2026-07-28 on — under the NOT_FOUND class, and none quotes the URI. A reader cannot tell a row it was refused from a row that does not exist.
  • bind = Service loads the row its template names, on a template naming exactly one variable: #[authorize(Read, bind = PostsService)] hands the method’s Authorized<Read, E> parameter the row, whatever the parameter is called. A row the caller may not have, one that does not exist and an id that is no v7 are that one answer, and the body never runs. A template naming several variables loads its row in the body, with the service’s access and an Option to answer.
  • The posture is the tool’s. #[authorize(Read, bind = PostsService)] runs the class gate before the load, so a token without posts:read is refused with the scope it lacks rather than handed an empty listing. Answer the #[expose]d type built from the row (Post::from(&post.into_inner())), inside an Option where it answers one: the row was judged for the caller as it was built, a column its grant withholds reading null. A listing’s entries are URIs and names the body writes from what it read — the ambient ability already scopes CrudService::list.

The templates are offered a URI in the order the block declares them, so two templates with the same shape — post://{id} with a Uuid, then post://{slug} with a String — read a URI by the first one whose variables parse. One template written twice in a host does not compile, and two hosts on one path reading one template fail the boot, as a tool name claimed twice does.

Completion, logging levels, subscriptions, the tasks/* extension and custom methods have no decorator. A host serving one writes rmcp’s ServerHandler itself and has no #[tools] block: #[tools] writes the whole handler, so it cannot share one with yours. The protocol types live under nest_rs::mcp::model, the per-operation handles under nest_rs::mcp::service, and everything else — rmcp’s #[tool_router] and #[tool_handler], its transport types — under nest_rs::mcp::rmcp. A request the server sends the client — elicitation, sampling — goes out through the operation’s peer on a session below protocol 2026-07-28; that revision forbids one, so rmcp refuses it there, and the operation answers an InputRequiredResult instead.

Such a host declares no operation for a layer to run on, so #[use_guards], #[use_interceptors], #[use_exception_filters] and #[meta] beside its #[mcp] do not compile: a guard there would gate nothing. Its endpoint is guarded where every MCP request is — the app’s global guards, or EndpointGuards<McpView, G> from a module, as AuthzMcpModule declares them — and every row it reads still comes through the ambient ability, a visitor’s reading nothing. It writes no posture; a wire row it answers is judged for the caller where it builds it, as everywhere. Give it a path of its own, or share one with the hosts that declare their operations: the endpoint offers a completion to each.

  • MCP — the #[tools] block a #[prompt] and a #[resource] live in.
  • Authorization — every capability is gated, not just tools/call.