Communitygithub.com

MCP 服务器模式

使用 Node/TypeScript SDK 构建 MCP 服务器,支持工具、资源、提示词、Zod 验证,以及 stdio 与 Streamable HTTP 传输方式。在构建或调试 MCP 服务器时使用此技能。

MCP 服务器模式 是什么?

该技能提供了一套全面的指南,用于使用 Node/TypeScript 构建和维护 MCP (Model Context Protocol) 服务器。它涵盖了 MCP 的核心概念,包括工具(模型可调用的动作)、资源(模型可获取的只读数据)、提示词(可复用的参数化提示模板)以及传输方式(stdio 用于本地客户端,Streamable HTTP 用于远程客户端如 Cursor)。技能详细说明了如何根据 SDK 版本注册这些能力,并强调了使用 Zod 进行模式验证的最佳实践。它指导开发人员根据用例选择合适的传输方式,保持服务器逻辑与传输无关,并返回结构化错误而非原始堆栈跟踪。该技能支持主要平台(Claude Desktop, Cursor, 云环境),并建议开发者通过 Context7 或官方 MCP 文档验证当前 API 签名,以确保兼容性。

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

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

MCP 服务器模式 是做什么的?

Model Context Protocol (MCP) 允许 AI 助手调用工具、读取资源以及使用您服务器上的提示词。在构建或维护 MCP 服务器时使用此技能。SDK API 会不断演进;请使用 Context7(查询 query-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() 或等效方法进行注册。
  • 传输方式:stdio 用于本地客户端(例如 Claude Desktop);Streamable HTTP 更适用于远程客户端(Cursor, 云环境)。旧版 HTTP/SSE 仅用于向后兼容。

Node/TypeScript SDK 可能会暴露 tool() / resource() 或 registerTool() / registerResource();官方 SDK 的接口随时间发生了变化。始终根据当前的 MCP 文档 或 Context7 进行验证。

使用 stdio 连接

对于本地客户端,创建一个 stdio 传输对象并将其传递给服务器的 connect 方法。确切的 API 因 SDK 版本而异(例如构造函数 vs 工厂方法)。请参考官方 MCP 文档或查询 Context7 中 "MCP stdio 服务器" 的模式。

保持服务器逻辑(工具 + 资源)与传输无关,以便在入口点中轻松接入 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)。使用库名称 "MCP" 查询 Context7 以获取当前的注册和传输模式。
  • Go:官方 Go SDK 在 GitHub 上 (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.

相关技能