Communitygithub.com

mcp-server-patterns

使用 Node/TypeScript SDK 建置 MCP 伺服器 — 工具、資源、提示、Zod 驗證、stdio 與 Streamable HTTP。在建立或除錯 MCP 伺服器時,查閱 Context7 或官方 MCP 文件以取得最新 API。

mcp-server-patterns 是什麼?

此技能旨在協助開發者使用 Node/TypeScript SDK 建置及維護 MCP(Model Context Protocol)伺服器。MCP 允許 AI 助手從你的伺服器調用工具、讀取資源及使用提示模板。技能涵蓋了核心概念如工具(可讓模型執行的動作)、資源(唯讀資料)和提示(可重用的參數化提示模板),以及傳輸層的選擇:stdio 適用於本地客戶端(如 Claude Desktop),而 Streamable HTTP 則推薦用於遠端客戶端(如 Cursor 或雲端服務)。技能詳細說明了如何使用 Zod 進行輸入驗證、如何設定伺服器及註冊工具和資源,並提供了最佳實務,包括 schema 優先設計、錯誤處理、冪等性及版本管理。此外,它還強調了 SDK API 會隨時間演進,建議開發者隨時參考官方 MCP 文件或使用 Context7 查詢最新的註冊與傳輸模式,以避免因 API 變更而導致的錯誤。

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

在你喜歡的 AI 中提問

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

說明文件

MCP 伺服器模式

模型上下文協議 (MCP) 可讓 AI 助手從您的伺服器調用工具、讀取資源及使用提示。在建立或維護 MCP 伺服器時,請使用此技能。SDK API 會持續演進;請查閱 Context7(查詢文件 "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() 或等效方法註冊。
  • 傳輸方式:stdio 用於本地客戶端(例如 Claude Desktop);對於遠端客戶端(Cursor、雲端),Streamable HTTP 是首選。傳統的 HTTP/SSE 僅用於向後相容。

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

使用 stdio 連線

對於本地客戶端,建立一個 stdio 傳輸並將其傳遞給伺服器的連線方法。確切的 API 因 SDK 版本而異(例如建構子與工廠方法的差別)。請參閱官方 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 偏好的 schema 格式)進行輸入驗證。

最佳實務

  • Schema 優先:為每個工具定義輸入 schema;記錄參數和回傳形狀。
  • 錯誤處理:回傳結構化的錯誤或模型可解讀的訊息;避免原始堆疊追蹤。
  • 冪等性:盡可能偏好冪等工具,以便重試安全。
  • 速率與成本:對於呼叫外部 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/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

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.

相關技能