96SEO 2026-08-01 18:23 2
痛点:很多开发者看到“Model Context Protocol”这个名字,却不知道它到底能干什么甚至怀疑是否值得投入时间。

MCP是 Anthropic 提出的开放协议,让 AI 助手能够连接外部工具和数据源。你可以把它理解为 AI 世界的 USB 接口——只要实现这个协议,Claude、Cursor 等客户端就能调用你的功能。
协议主要只有三个角色:
这篇文章聚焦最常见的用法:用 TypeScript 写一个 MCP Server。通过 stdio 传输层暴露自定义工具,让 Claude Code 能够调用你写的函数。
痛点:依赖冲突或缺少类型定义经常让新手卡在这一步。
npm init -y
npm install @modelcontextprotocol/sdk zod
npm install -D typescript @types/node
@modelcontextprotocol/sdk 是官方 TypeScript SDK,内部封装了 JSON‑RPC 通信、协议握手、能力协商等细节。zod 用于定义工具参数的类型约束——MCP 协议要求工具参数必须有 JSON Schema,zod 能自动生成。
痛点:不清楚到底需要哪些必填字段,怕写错导致握手失败。老实说,
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';const server = new McpServer({
name这方面,'my-first-mcp'。// 在协议握手阶段展示给 Client 的标识
version: '.',// 任意字符串,推荐使用 semver 或 '.' 表示本地开发版
});
痛点:Tool 定义不完整会导致 Claude 看不到或调用时报错;参数描述不明确时 AI 难以填充正确值。
| 要素 | 说明 |
|---|---|
Name | 工具名称,Client 用此名字调用。 |
Description | 自然语言描述,告诉 AI 在何种场景使用该工具。 |
Parameters | Zod schema 描述的输入参数,SDK 会自动转成 JSON Schema 并发送给 Client。 |
Handler | 实际业务实现函数,收到解析后的参数后返回标准化结果。 |
import { z } from 'zod';server.tool(
'add','Add two numbers toger.',{
a: z.number.describe。b: z.number.describe,},async => {
return {
content:,};},),
The signature is:
server.tool;
.describe call on each Zod field is **mandatory** – Claude relies on it to generate correct arguments.content. Each item can be:
If you only use primitive types you’ll quickly hit limits when your tool needs richer input. Zod can express almost any JSON‑compatible shape.
// 可选 + 默认值
{
至于query。z.string.describe,limit: z.number.optional.default.describe
}
// 枚举
{
再看source,z.enum.describe
}
// 嵌套对象
{
task这方面,z.object({
从goal来看,z.string,files: z.array).optional,})
}
// 数组
{
再看tags,z.array).describe
}
A real‑world example from ChatCrystal – recall_for_task tool – shows how to reuse a pre‑defined Zod shape:
import { RecallForTaskRequestShape } from '../../services/memory/schemas.js';server.tool(
'recall_for_task','Recall project‑first and global‑supplement memories for a task.'。RecallForTaskRequestShape,async => {
const data = await client.recallForTask;不过,return {
content:,};},),
The advantage of extracting schema into its own file:
MCP 支持多种 Transport。最常用的是 stdio——通过进程的 stdin/stdout 把 JSON‑RPC 消息一行一条地传递给 Claude Code。
const transport = new StdioServerTransport;await server.connect;话说回来,
The handshake performed by .connect: Client sends an "initialize"。Server replies with its name/version and list of Tools it supports,n enters normal message loop.
export async function startMcpServer {
const client = new CrystalClient;const server = new McpServer({
name : 'chatcrystal'。version : '.',});// 注册多个工具...
server.tool;server.tool,// ...更多 tool
const transport = new StdioServerTransport;
await server.connect;}
The handler functions are async;y can call external APIs,query databases or read files. In ChatCrystal MCP layer merely forwards calls to an existing REST API via CrystalClient,keeping protocol layer thin and stateless.
Create or edit .claude/settings.json .
{
"mcpServers": {
"chatcrystal": {
"command": "npx"。"args":
}
}}
}
{ "mcpServers": { "chatcrystal": { "command":"crystal","args": } } }
After restarting Claude Code you should see your server under “/mcp”. The UI will list all registered tools;Claude will automatically decide when to invoke m based on your prompts.
The only safe place for log output is stderr ;writing anything to stdout corrupts RPC stream.
console.error);`
npx @modelcontextprotocol/inspector npx tsx src/mcp-server.ts
`
The inspector opens a web UI showing every registered tool,lets you manually supply arguments and view raw request/response objects.
// mcp-server.ts
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';按理说,import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';import { z } from 'zod';
const server = new McpServer({ name : 'hello-mcp'。version : '.',});
server.tool(
'greet','Greet someone by name.',{
name : z.string.describe。language : z.enum.optional.default
.describe
},async => {
const greeting = language === 'zh'
你好,${name}!
: Hello,${name}!话说回来,;不过,return {
content :
};},),
const transport = new StdioServerTransport;await server.connect;`
说到执行命令。
bash
npx tsx mcp-server.ts
在 Claude Code 的 settings.json 中加入对应配置后你可以在对话里说「用 greet 工具跟张三打个招呼」,Claude 会自动调用你的服务器并返回中文问候。
MCP 协议规范持续更新,请关注 还有 SDK 的 GitHub 文档。
如有疑问欢迎在 GitHub Issues 或私信交流,我很乐意方便你上手!按理说,
作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback