Communitygithub.com

mcp-server-patterns

Node/TypeScript SDKを使ってMCPサーバーを構築 — ツール、リソース、プロンプト、Zodバリデーション、stdioとStreamable HTTP。Context7または公式MCPドキュメントで最新APIを確認。MCPサーバーの構築やデバッグ(ツール、リソース、プロンプト、バリデーション、トランスポート選択)に使用。

mcp-server-patterns とは?

このスキルは、Model Context Protocol(MCP)サーバーをNode.js/TypeScript SDKで構築・保守するためのベストプラクティスとパターンを提供します。ツール(モデルが呼び出すアクション)、リソース(モデルが取得する読み取り専用データ)、プロンプト(再利用可能なテンプレート)の登録方法、Zodを使った入力バリデーション、stdio(Claude Desktopなどのローカルクライアント向け)とStreamable HTTP(Cursorやクラウド向け)のトランスポート選択について解説します。SDKのAPIはバージョンによって変化するため、Context7(「MCP」でクエリ)や公式ドキュメントを参照して最新のメソッド名やシグネチャを確認することを推奨します。サーバーロジックをトランスポートから分離し、エントリポイントで簡単に切り替えられる設計が重要です。

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

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

MCPサーバーパターン

Model Context Protocol(MCP)は、AIアシスタントがサーバー上のツールを呼び出したり、リソースを読み取ったり、プロンプトを使用したりできるようにします。このスキルは、MCPサーバーの構築や保守時に使用します。SDKのAPIは進化するため、現在のメソッド名やシグネチャについては、Context7(「MCP」でquery-docs)または公式の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は時間とともに変化しています。必ず現在の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 を含めます。コピーペーストのエラーを避けるため、現在の @modelcontextprotocol/sdk のシグネチャについては公式MCPドキュメントまたはContext7を確認してください。

入力バリデーションにはZod(またはSDKが推奨するスキーマ形式)を使用します。

ベストプラクティス

  • スキーマファースト: すべてのツールに入力スキーマを定義し、パラメータと戻り値の形状を文書化します。
  • エラー: モデルが解釈できる構造化エラーまたはメッセージを返し、生のスタックトレースは避けます。
  • 冪等性: 可能な限り冪等なツールを優先し、リトライが安全になるようにします。
  • レートとコスト: 外部APIを呼び出すツールについては、レート制限とコストを考慮し、ツールの説明に文書化します。
  • バージョン管理: package.jsonでSDKのバージョンを固定し、アップグレード時にはリリースノートを確認します。

公式SDKとドキュメント

  • JavaScript/TypeScript: @modelcontextprotocol/sdk(npm)。現在の登録およびトランスポートパターンについては、ライブラリ名「MCP」でContext7を使用します。
  • 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.

関連スキル