MCP Resources & Prompts 深度實戰 — 進階 MCP 技巧令 AI 更懂你

2026-05-10 · MCP 進階 · Eric Cheung

前言:Tool 以外嘅 MCP 超能力

快速結論(TL;DR)

大部分開發者認識 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

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:幾時用邊個?


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 舉例


總結:MCP 三重奏

記住呢個口訣:Tool 做事、Resource 知事、Prompt 想事。當你嘅 MCP Server 同時具備三者,AI 就由「被動幫手」升級為「主動夥伴」— 佢會自己發現需要咩資訊、用咩工具、跟咩流程嚟完成任務。

Written by Eric Cheung · Learn One AI Thing Every Day · Day 2: MCP Resources & Prompts