Using Prompts
00:00 MCP servers expose tools, resources, but also prompts. Tools and resources are respectively dynamic and static ways of providing more information, more context to the LLM.
00:13 But prompts are just reusable text instructions that you save as templates so that you can reuse them conveniently later on. For example, if you want to run a specific type of analysis on your sales data often, instead of having to type it by hand every single time or copying it from a random text file you have, you just expose it from your MCP server.
00:37
In code, if you open the file main.py, you need to write a function that returns a string that corresponds to your prompt. For example, if you write a function called get_sales_analysis_prompt() that takes no arguments and returns a string, you can directly return your prompt, open triple quotes so that the prompt can span across multiple lines, and you could say something like, """Determine the total number of sales done across all customers and also tell me the total amount spent by Bob.""" And then you close the triple quotes.
01:20 And this is the prompt that’s going to correspond to your sales analysis. But at this point, this is just a function. You need to tell the MCP server that this corresponds to a prompt that you want to expose.
01:32
To do that, use the decorator @mcp_server.prompt().
01:38
@mcp_server comes from your FastMCP server instance and .prompt() is the decorator that defines prompts. Inside parentheses, you should give this prompt a readable name, something like Sales analysis. This is important for the MCP client that’s going to use the prompt.
01:57 Now to test this, go ahead and open Cursor, and after you’ve opened Cursor, make sure you toggle the MCP server off and on again, which should now show a prompt.
02:09
It should now list a prompt as a resource that’s available. You can see the static resource get_sales, that’s going to be a CSV file, the tool get_sales_from_customer and also the prompt Sales analysis.
02:21 Now if you go to the Cursor menu, View, Command Palette and you type New Chat, you’re going to open a new chat window. Depending on the MCP client, you might have access to prompts in different ways, but in Cursor, it’s enough to type forward slash and then you start typing the name of the MCP server that’s exposing the prompt.
02:45 In this case Sales, and as soon as you type Sales, you can see the interface autocompleting Sales Real Python server slash Sales analysis. You can click that and you can see in yellow the interface is telling you that it’s going to use a prompt from an MCP server.
03:02 Now to avoid wasting tokens in this particular interaction, go ahead and change the interaction mode from Agent to Ask. Believe it or not, a simpler type of interaction here will yield a better result and as soon as you press Enter, the LLM will go off and run the prompt that you provided.
03:21 This might entail interacting with your MCP server further, but in essence, it’s showing you that you did not have to type your prompt by hand, you just needed to specify that you wanted to run a prompt from the MCP server.
03:34 And in here, in the end, you can see the LLM saying that you made a total of five sales and Bob spent a total of 140, which is correct as per the dummy data.
03:45 Now this shows you how to expose a prompt from an MCP server and how to use it from an MCP client. And in the next lesson, you’re going to do a quick recap of everything you’ve learned in this course so far.
Become a Member to join the conversation.
