Skip to main content

MCP Prompts

mcpfy provides a declarative API for defining and registering MCP prompts. Prompts allow an MCP server to expose reusable prompt templates that MCP clients can discover and request with structured arguments. mcpfy simplifies prompt registration while keeping the underlying MCP prompt model intact.

1. Overview

A prompt in mcpfy consists of:
  • A unique name
  • An optional title
  • An optional description
  • An optional Zod schema for prompt arguments
  • A callback that generates the prompt result
The basic structure is:
The prompt is then available to MCP clients through the standard MCP prompt operations.

2. Importing Prompt APIs

The prompt types are available from:
For schemas, use Zod:

3. Registering a Prompt

Prompts are registered using:
Example:

4. Prompt Definition

The prompt definition has the following structure:

Properties

A callback must be supplied either through the second argument of .prompt() or through the cb property.

5. Prompt Callback

A prompt callback receives:
where:
  • params contains the validated prompt arguments.
  • ctx is the mcpfy ToolContext.
The callback type is:
This means prompt callbacks can return either a standard MCP GetPromptResult or the same simplified content-helper results supported by tools.

6. Using a Zod Schema

A Zod object can be used to describe prompt arguments.
For multiple arguments:
The resulting arguments are available inside the callback:

7. Example: Simple Prompt

A client can request the prompt with:

8. Standard GetPromptResult

The most direct way to return a prompt is to return a standard MCP GetPromptResult.
The messages array contains the messages that make up the generated prompt.

9. Prompt Messages

A prompt result uses MCP PromptMessage objects. For example:
Multiple messages can be returned:

10. Using text()

mcpfy also allows prompt callbacks to return the same content helpers used by tools. For example:
A prompt can return:
mcpfy converts the returned content into the standard MCP prompt format.

11. Using markdown()

Markdown content can also be returned:
Example:
mcpfy converts this content into a prompt message.

12. Using object()

Structured content can also be produced using the object() helper when appropriate.
Example:
mcpfy converts content-helper results into prompt messages.

13. Callback Defined Inside the Definition

Instead of passing the callback as the second argument, it can be specified using cb.
Both callback styles are supported.

14. Callback as the Second Argument

The callback can instead be separated from the definition:
This style is useful when keeping configuration and implementation separate.

15. Using ToolContext

The prompt callback receives a ToolContext as its second parameter.
The context is built using the same server context mechanism used by tool callbacks. This allows prompt implementations to access the contextual capabilities provided by mcpfy. For details about available context functionality, see the server/tool documentation.

16. Prompt Without Arguments

A prompt does not require a schema.
Because no schema is provided, the prompt is registered with an empty argument schema.

17. Prompt With Multiple Arguments

The MCP client can then request:

18. How Prompt Registration Works

Internally, server.prompt() delegates registration to the prompt registration layer. The process is:
mcpfy therefore provides a simpler developer-facing API while registering the prompt with the official MCP server implementation.

19. Automatic Result Conversion

Prompt callbacks can return either:
  1. A standard GetPromptResult
  2. A typed tool-style result
  3. A ToolContentResult
If the callback already returns a GetPromptResult, mcpfy uses it directly. For content-helper results, mcpfy converts the returned content into prompt messages. Conceptually:
Each returned content item becomes a user prompt message.

20. Missing Callback Error

Every registered prompt must have a callback. This is valid:
This is also valid:
But the following is invalid:
mcpfy throws an error indicating that the prompt is missing a callback.

21. Discovering Prompts from a Client

Once registered, prompts can be discovered using the mcpfy client:
Example:
The client receives the prompt metadata exposed by the MCP server.

22. Requesting a Prompt from a Client

After discovering a prompt:
The server executes the registered callback and returns the generated prompt.

23. Complete Example

Server

Client


24. Best Practices

Use descriptive prompt names

Prefer:
over:

Provide descriptions

Descriptions help MCP clients understand when a prompt should be used.

Use schemas for arguments

If a prompt requires input, define it explicitly:

Keep callbacks focused

A prompt callback should primarily construct the prompt rather than contain unnecessary application logic.

Use content helpers when appropriate

For simple prompts, helpers such as:
can make the implementation shorter and easier to read.

25. API Summary

server.prompt()

Registers an MCP prompt.

PromptDefinition

PromptCallback

Client prompt methods


26. Summary

mcpfy makes MCP prompt development straightforward by providing a declarative .prompt() API with:
  • Prompt metadata
  • Zod-based argument schemas
  • Typed callback parameters
  • ToolContext access
  • Standard MCP GetPromptResult support
  • text(), markdown(), and object() content-helper support
  • Automatic conversion of content results into MCP prompt messages
The typical implementation is:
The resulting prompt can then be discovered and requested by any compatible MCP client.