Communitygithub.com

MCP 伺服器模式

使用 Node/TypeScript SDK 建立 MCP 伺服器 — 工具、資源、提示、Zod 驗證、stdio 與 Streamable HTTP。在建立或除錯 MCP 伺服器時使用此技能 — 涵蓋工具、資源、提示、驗證或傳輸方式的選擇。

MCP 伺服器模式 是什麼?

此技能提供使用 Node/TypeScript SDK 建立與維護模型上下文協議 (MCP) 伺服器的全面指引。它涵蓋了 MCP 的三個核心功能:工具 (模型可調用的動作)、資源 (唯讀資料) 和提示 (可重複使用的提示模板),並說明如何根據 SDK 版本使用正確的註冊方法 (`registerTool()` / `tool()` 等)。該技能詳細比較了兩種傳輸方式:適用於本機客戶端 (如 Claude Desktop) 的 stdio,以及適用於遠端客戶端 (如 Cursor 或雲端服務) 的 Streamable HTTP。它強調使用 Zod 進行輸入驗證,並提供從安裝、伺服器設定到工具註冊與錯誤處理的實際範例。此技能支援多個平台,包含 JavaScript/TypeScript、Go 和 C# 的官方 SDK。為了應對不斷演進的 API,該技能建議查閱官方 MCP 文件或 Context7 以取得最新的方法簽章,確保開發人員能正確建構與除錯 MCP 伺服器,避免常見陷阱。

相容平台✓Claude Code~Codex CLI✓Cursor
npx skills add https://github.com/affaan-m/ECC/tree/main/skills/mcp-server-patterns

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

MCP 伺服器模式 是做什麼的?

模型上下文協議 (MCP) 讓 AI 助手能夠呼叫您伺服器上的工具、讀取資源以及使用提示。在建立或維護 MCP 伺服器時使用此技能。SDK API 會不斷演進;請查閱 Context7 (查詢-docs 關鍵字 "MCP") 或官方 MCP 文件以取得最新的方法名稱與簽章。

關於一個功能應作為規則、技能、MCP 還是單純的 CLI/API 工作流程的更廣泛路由決策,請參閱 docs/capability-surface-selection.md。

使用時機

在以下情況使用:實作新的 MCP 伺服器、新增工具或資源、選擇 stdio 或 HTTP、升級 SDK,或除錯 MCP 註冊與傳輸問題。

運作方式

核心概念

  • 工具:模型可以呼叫的動作 (例如搜尋、執行命令)。根據 SDK 版本使用 registerTool() 或 tool() 註冊。
  • 資源:模型可以擷取的唯讀資料 (例如檔案內容、API 回應)。使用 registerResource() 或 resource() 註冊。處理常式通常會接收一個 uri 參數。
  • 提示:可重複使用、參數化的提示模板,客戶端可以呈現 (例如在 Claude Desktop 中)。使用 registerPrompt() 或等效方法註冊。
  • 傳輸方式:適用於本機客戶端 (例如 Claude Desktop) 的 stdio;遠端 (Cursor、雲端) 則首選 Streamable HTTP。傳統的 HTTP/SSE 僅用於向後相容。

Node/TypeScript SDK 可能會暴露 tool() / resource() 或 registerTool() / registerResource();官方 SDK 的 API 已隨著時間而改變。請務必根據現行的 MCP 文件 或 Context7 進行驗證。

使用 stdio 連線

對於本機客戶端,建立一個 stdio 傳輸物件,並將其傳遞給伺服器的 connect 方法。確切的 API 因 SDK 版本而異 (例如建構子 vs 工廠方法)。請參閱官方 MCP 文件或查詢 Context7 的 "MCP stdio server" 以取得當前模式。

保持伺服器邏輯 (工具 + 資源) 與傳輸方式無關,以便您可以在進入點插入 stdio 或 HTTP。

遠端 (Streamable HTTP)

對於 Cursor、雲端或其他遠端客戶端,請使用 Streamable HTTP (根據當前規範為單一 MCP HTTP 端點)。僅在需要向後相容時才支援傳統的 HTTP/SSE。

範例

安裝與伺服器設定

npm install @modelcontextprotocol/sdk zod
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";

const server = new McpServer({ name: "my-server", version: "1.0.0" });

使用您 SDK 版本所提供的 API 註冊工具和資源:某些版本使用 server.tool(name, description, schema, handler) (位置參數),其他版本則使用 server.tool({ name, description, inputSchema }, handler) 或 registerTool()。資源也是如此 — 當 API 提供 uri 時,請在處理常式中包含它。請查閱官方 MCP 文件或 Context7 以取得目前 @modelcontextprotocol/sdk 的簽章,以避免複製貼上錯誤。

使用 Zod (或 SDK 偏好的模式格式) 進行輸入驗證。

最佳實務

  • 模式優先:為每個工具定義輸入模式;記錄參數和回傳形狀。
  • 錯誤:回傳模型可以解讀的結構化錯誤或訊息;避免原始堆疊追蹤。
  • 冪等性:盡可能偏好冪等工具,以便重試是安全的。
  • 速率與成本:對於呼叫外部 API 的工具,請考量速率限制和成本;在工具描述中記錄。
  • 版本控制:在 package.json 中鎖定 SDK 版本;升級時檢查版本說明。

官方 SDK 與文件

  • JavaScript/TypeScript:@modelcontextprotocol/sdk (npm)。使用 Context7 並將函式庫名稱設為 "MCP" 以取得當前的註冊和傳輸模式。
  • Go:GitHub 上的官方 Go SDK (modelcontextprotocol/go-sdk)。
  • C#:適用於 .NET 的官方 C# SDK。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/content-engine

Create platform-native content systems for X, LinkedIn, TikTok, YouTube, newsletters, and repurposed multi-platform campaigns. Use when the user wants social posts, threads, scripts, content calendars, or one source asset adapted cleanly across platforms.

affaan-m/ECC

Multi-platform content distribution across X, LinkedIn, Threads, and Bluesky. Adapts content per platform using content-engine patterns. Never posts identical content cross-platform. Use when the user wants to distribute content across social platforms.

affaan-m/fal-ai-media

Unified media generation via fal.ai MCP — image, video, and audio. Covers text-to-image (Nano Banana), text/image-to-video (Seedance, Kling, Veo 3), text-to-speech (CSM-1B), and video-to-audio (ThinkSound). Use when the user wants to generate images, videos, or audio with AI.

affaan-m/manim-video

日本語翻訳:このファイルは manim-video 用の日本語翻訳が必要です

affaan-m/remotion-video-creation

Remotion のベストプラクティス - React で動画を作成する。3D、アニメーション、音声、字幕、チャート、トランジションなどをカバーするドメイン固有の29のルール。

affaan-m/video-editing

AI-assisted video editing workflows for cutting, structuring, and augmenting real footage. Covers the full pipeline from raw capture through FFmpeg, Remotion, ElevenLabs, fal.ai, and final polish in Descript or CapCut. Use when the user wants to edit video, cut footage, create vlogs, or build video content.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

agent-self-evaluation

Use after completing any non-trivial task. The agent self-rates its output on 5 axes — accuracy, completeness, clarity, actionability, conciseness — with concrete evidence per criterion. Produces a structured 1-5 scorecard with specific improvement suggestions.

agent-sort

Build an evidence-backed ECC install plan for a specific repo by sorting skills, commands, rules, hooks, and extras into DAILY vs LIBRARY buckets using parallel repo-aware review passes. Use when ECC should be trimmed to what a project actually needs instead of loading the full bundle.

ai-first-engineering

Engineering operating model for teams where AI agents generate a large share of implementation output. Use when setting team process, review gates, or ownership rules for a codebase largely written by agents.

ai-regression-testing

Regression testing strategies for AI-assisted development. Sandbox-mode API testing without database dependencies, automated bug-check workflows, and patterns to catch AI blind spots where the same model writes and reviews code. Use when adding regression coverage to AI-assisted code, or when the same model both wrote and reviewed a change.

android-clean-architecture

Clean Architecture patterns for Android and Kotlin Multiplatform projects — module structure, dependency rules, UseCases, Repositories, and data layer patterns. Use when structuring modules, layers, or data flow in an Android or KMP project.

angular-developer

Generates Angular code and provides architectural guidance. Trigger when creating projects, components, or services, or for best practices on reactivity (signals, linkedSignal, resource), forms, dependency injection, routing, SSR, accessibility (ARIA), animations, styling (component styles, Tailwind CSS), testing, or CLI tooling.

相關技能