MCP Resources & Prompts 深度實戰 — 進階 MCP 技巧令 AI 更懂你
2026-05-10 · MCP 進階 · Eric Cheung
前言:Tool 以外嘅 MCP 超能力
快速結論(TL;DR)
- Tool 以外,MCP 仲有兩個被低估嘅功能:Resources 俾 AI 自動發現資料源,Prompts 提供預設嘅思維框架
- Resources 係被動暴露機制,定義好檔案、DB schema 或者 API endpoints 之後,AI 自己會主動去讀,唔使下下指明
- Prompts 係模板化提示系統,支援 variable placeholder 同多步驟工作流,例如預設一個 code_review 審查模板
- 口訣係「Tool 做事、Resource 知事、Prompt 想事」,三者齊備嘅 MCP Server 先可以令 AI 由被動幫手變成主動夥伴
大部分開發者認識 MCP,都係由 Tool 開始 — 註冊幾個工具俾 Claude Code 用。但 MCP 嘅能力遠超 Tool。今日我哋深入兩個經常被忽略但極其強大嘅功能:Resources(資源自動暴露)同 Prompts(預設提示模板)。
簡單嚟講:Tool 係「AI 問你攞嘢」、Resource 係「你主動話俾 AI 知有咩」、Prompt 係「你預先教 AI 點樣問」。三者加埋,先係真正嘅 AI Context 自動化。
Part 1:MCP Resources — 令 AI 自動發現資料源
Resources 係咩?
MCP Resources 係一個被動暴露機制。你定義好有咩資料可以用(檔案、Database tables、API endpoints),AI 就可以自己瀏覽同讀取,唔使每次都等你明確指定。
想像一下:你個 project 有成百個檔案、幾十個 API endpoints。以前你要同 AI 講「請睇下 src/utils/auth.js」— 而家 AI 自己就知道呢個檔案存在,可以主動去睇。
Resources 類型
- File Resources — 暴露檔案系統內容(project files, config files, logs)
- Database Resources — 暴露 DB schema、table list、query results
- API Resources — 暴露 REST/GraphQL endpoints 同佢哋嘅 schema
- Dynamic Resources — 用 URI template 支援動態參數(e.g. file://{path})
實戰:定義 File Resources
server.setRequestHandler(ListResourcesRequestSchema, async () => ({
resources: [
{
uri: "file:///project/CLAUDE.md",
name: "CLAUDE.md",
description: "Project instructions & conventions",
mimeType: "text/markdown",
},
{
uri: "file:///project/src/config.ts",
name: "App Config",
description: "Application configuration file",
mimeType: "text/typescript",
},
],
}));
實戰:Read Resource Handler
server.setRequestHandler(ReadResourceRequestSchema, async (req) => {
const uri = req.params.uri;
// Map URI to local file path
const filePath = uri.replace("file:///project/", "./");
const content = await fs.readFile(filePath, "utf-8");
return {
contents: [{ uri, mimeType: "text/plain", text: content }],
};
});
Resources vs Tools:幾時用邊個?
- 用 Resource — 當 AI 需要「瀏覽」大量靜態資料(文件列表、DB schema、API docs)
- 用 Tool — 當 AI 需要「執行」操作(寫入檔案、call API、run command)
- 兩個一齊用 — Resource 俾 AI 知道有咩可以用,Tool 俾 AI 實際去用
Part 2:MCP Prompts — 預設提示模板引擎
Prompts 係咩?
MCP Prompts 係一個模板化嘅提示系統。你可以預先定義好常用嘅對話模板,包含 variable placeholder、context injection、甚至 multi-step workflow。當 AI 需要執行特定任務時,可以直接調用對應嘅 prompt template。
關鍵差異:Tool 係俾 AI「做嘢」、Resource 係俾 AI「睇嘢」、Prompt 係俾 AI「諗嘢」嘅框架。
實戰:定義 Prompt Templates
server.setRequestHandler(ListPromptsRequestSchema, async () => ({
prompts: [
{
name: "code_review",
description: "Review code with specific focus areas",
arguments: [
{ name: "language", description: "Programming language",
required: true },
{ name: "focus", description: "Security/Performance/Readability",
required: false },
],
},
{
name: "generate_changelog",
description: "Generate CHANGELOG from git history",
arguments: [
{ name: "from_tag", required: true },
{ name: "to_tag", required: true },
],
},
],
}));
實戰:Get Prompt Handler
server.setRequestHandler(GetPromptRequestSchema, async (req) => {
if (req.params.name === "code_review") {
const lang = req.params.arguments?.language || "TypeScript";
const focus = req.params.arguments?.focus || "overall quality";
return {
messages: [{
role: "user",
content: {
type: "text",
text: `You are a senior ${lang} developer. Review the
following code focusing on: ${focus}.
Provide: 1) Issues found 2) Suggested fixes 3) Code quality score`
},
}],
};
}
throw new Error("Unknown prompt: " + req.params.name);
});
Part 3:三合一實戰 — Tool + Resource + Prompt
真正嘅力量嚟自三者整合。以下係一個 project 級 MCP Server 嘅完整 capabilities 聲明:
const server = new Server({ name: "project-assistant", version: "2.0" },
{
capabilities: {
tools: {}, // AI 可以做嘢(執行操作)
resources: {}, // AI 可以睇嘢(自動發現資料)
prompts: {}, // AI 可以諗嘢(用預設思維框架)
},
}
);
日常 Workflow 舉例
- AI 啟動時 → 自動經 Resources 掃描 CLAUDE.md、package.json、config files
- 寫 Code 時 → 經 Tool call eslint/formatter,經 Resource check type definitions
- Code Review 時 → 經 Prompt "code_review" 模板,自動注入審查標準
- Deploy 時 → 經 Tool trigger CI/CD,經 Resource 讀 build logs
總結:MCP 三重奏
記住呢個口訣:Tool 做事、Resource 知事、Prompt 想事。當你嘅 MCP Server 同時具備三者,AI 就由「被動幫手」升級為「主動夥伴」— 佢會自己發現需要咩資訊、用咩工具、跟咩流程嚟完成任務。
Written by Eric Cheung · Learn One AI Thing Every Day · Day 2: MCP Resources & Prompts