載入中
MCP 伺服器中的提示讓您可以定義預先建置、高品質的指令,客戶端可以直接使用,而不必從頭開始撰寫自己的提示。可以把它們想成是精心製作的範本,能提供比使用者自行想出來更好的結果。
為什麼要使用提示?
假設您想讓 Claude 將文件重新格式化為 markdown。使用者可能只是輸入「將 report.pdf 轉換為 markdown」,並得到還不錯的結果。但如果他們使用的是您專門為文件格式化設計、經過徹底測試的專用提示,他們很可能會得到更好的輸出結果。

關鍵的洞察在於,雖然使用者可以自行完成這些任務,但當他們使用由 MCP 伺服器作者精心設計並測試過的提示時,將會獲得更優異的結果。
提示如何運作
提示定義了一組使用者與助理訊息,客戶端可以直接使用。當客戶端請求某個提示時,您的伺服器會回傳一份可以直接傳送給 Claude 的訊息清單。

基本結構如下所示:
python
@mcp.prompt(
name="format",
description="Rewrites the contents of a document in Markdown format",
)
def format_document(
doc_id: str = Field(description="Id of the document to format"),
) -> list[base.Message]:
# 傳回訊息清單建立格式化命令
這裡有一個實際的範例。我們將建立一個格式化命令,讓使用者可以輸入 /format doc_id 來將任何文件重新格式化為 markdown 語法。
該提示的實作包含給 Claude 的詳細指示:
python
def format_document(
doc_id: str = Field(description="Id of the document to format"),
) -> list[base.Message]:
prompt = f"""
Your goal is to reformat a document to be written with markdown syntax.
The id of the document you need to reformat is:
{doc_id}
Add in headers, bullet points, tables, etc as necessary. Feel free to add in structure.
Use the 'edit_document' tool to edit the document. After the document has been reformatted...
"""
return [
base.UserMessage(prompt)
]測試您的提示
您可以使用 MCP Inspector 來測試提示。導覽至 Prompts 分頁,選取您的提示,並提供任何必要的參數。

檢查器會確切顯示將傳送給 Claude 的訊息內容,包括任何參數是如何內插到提示文字中的。
主要優點
- 品質控管 - 您可以在使用者看到提示之前先進行測試與調整
- 一致性 - 使用者每次都能獲得可靠的結果
- 專業化 - 提示可以針對您伺服器的特定領域量身打造
- 可重複使用性 - 多個客戶端可以使用相同的精心製作提示
實作細節
別忘了匯入訊息類型所需的基礎模組:
python
from mcp.server.fastmcp import FastMCP
from mcp.server.fastmcp.prompts import base提示應該要高品質、經過充分測試,並且與您的 MCP 伺服器的整體用途相關。在我們的文件管理範例中,格式化提示非常合理,因為該伺服器專門處理文件操作。